Express Exceptions Handler Middleware
Overview
The Express Exceptions Handler Middleware package simplifies error management in Express.js applications. It provides a library of predefined HTTP exceptions and a centralized error-handling middleware for structured and consistent error responses.
Why Use Express Exceptions Handler Middleware?
-
Predefined Exceptions: Reduce repetitive code by leveraging ready-to-use exception classes.
-
Centralized Error Handling: A single middleware to manage all application errors.
-
Customizable: Extend predefined exceptions or create your own.
-
Standardized Responses: Ensures consistency across your APIs.
Features
-
Comprehensive HTTP Exceptions: Built-in support for common HTTP status codes like 401 Unauthorized
, 404 Not Found
, etc.
-
Middleware Integration: Plug-and-play middleware for Express.js.
-
Custom Exception Support: Extend functionality with your own exception classes.
-
Structured Error Responses: Returns detailed error objects for better debugging and client interaction.
Installation
Install the package via npm or yarn:
npm install express-exceptions-handler
Getting Started
Example Usage
Here is a basic example of how to integrate the Express Exceptions Handler Middleware into your Express.js application:
Code Example:
const express = require('express');
const { UnauthorizedException, HttpException, errorHandlerMiddleware } = require('express-exceptions-handler');
const app = express();
// Example route
app.get('/', (req, res, next) => {
try {
throw new UnauthorizedException('Invalid credentials');
} catch (error) {
return next(new HttpException(error?.message, error?.status));
}
});
// Add error-handling middleware
app.use(errorHandlerMiddleware);
app.listen(3000, () => {
console.log('Server running on http://localhost:3000');
});
Predefined Exceptions
The package includes various built-in exceptions corresponding to HTTP status codes. Each exception extends the HttpException
base class and includes a default status code and message.
Example:
const { NotFoundException, BadRequestException } = require('express-exceptions-handler');
app.get('/example', (req, res, next) => {
try {
throw new NotFoundException('Resource not found');
} catch (error) {
return next(error);
}
});
Error-Handling Middleware
The errorHandlerMiddleware
is a robust Express.js middleware that catches and processes all thrown exceptions in your application.
Default JSON Response Format:
{
"data": null,
"message": "Error message",
"statusCode": 500,
"isSuccess": false
}
Explanation of Response Fields:
-
data: Always null
for errors.
-
message: The specific error message.
-
statusCode: The HTTP status code of the error.
-
isSuccess: Always false
for errors.
Adding Custom Exceptions
To define custom exceptions, extend the HttpException
class as follows:
const { HttpException } = require('express-exceptions-handler');
class CustomException extends HttpException {
constructor(message = 'Custom error occurred', status = 400) {
super(message, status);
}
}
app.get('/custom', (req, res, next) => {
try {
throw new CustomException('This is a custom error', 422);
} catch (error) {
return next(error);
}
});
Predefined Exceptions List
Here is the list of available predefined exceptions with their corresponding HTTP status codes:
Exception Status Code
BadRequestException
400
UnauthorizedException
401
ForbiddenException
403
NotFoundException
404
MethodNotAllowedException
405
NotAcceptableException
406
RequestTimeoutException
408
ConflictException
409
GoneException
410
PreconditionFailedException
412
PayloadTooLargeException
413
UnsupportedMediaTypeException
415
ImATeapotException
418
MisdirectedException
421
UnprocessableEntityException
422
TooManyRequestsException
429
InternalServerErrorException
500
NotImplementedException
501
BadGatewayException
502
ServiceUnavailableException
503
GatewayTimeoutException
504
HttpVersionNotSupportedException
505
Contributing
We welcome contributions! If you find bugs or have feature requests, please open an issue or submit a pull request on the GitHub repository.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Feedback and Support
If you encounter any issues or have suggestions for improvement, feel free to open an issue on GitHub. Thank you for using Express Exceptions Handler Middleware! 🚀# Express Exceptions Handler Middleware