Gathering detailed insights and metrics for express-exceptions-handler
Gathering detailed insights and metrics for express-exceptions-handler
Gathering detailed insights and metrics for express-exceptions-handler
Gathering detailed insights and metrics for express-exceptions-handler
express-exception
Exprss middleware to handle exceptions and errors, contains collection of exception classes and middleware to handle them.
node-http-exceptions
a top level exception handle package for nodejs
catch-async-wrapper-express
middleware for handling exceptions inside of async express routes and passing them to your express or custom error handlers
@joellesenne/express-async-handler
Simple Middleware to manage exceptions within express routes in asynchronous
npm install express-exceptions-handler
Typescript
Module System
Node Version
NPM Version
71.1
Supply Chain
95.5
Quality
76.3
Maintenance
100
Vulnerability
100
License
TypeScript (93.34%)
JavaScript (6.66%)
Total Downloads
328
Last Day
1
Last Week
1
Last Month
13
Last Year
328
1 Stars
7 Commits
1 Watchers
5 Branches
1 Contributors
Updated on Jan 29, 2025
Minified
Minified + Gzipped
Latest Version
1.0.3
Package Id
express-exceptions-handler@1.0.3
Unpacked Size
126.54 kB
Size
11.22 kB
File Count
106
NPM Version
9.8.1
Node Version
18.18.0
Published on
Jan 08, 2025
Cumulative downloads
Total Downloads
Last Day
0%
1
Compared to previous day
Last Week
0%
1
Compared to previous week
Last Month
18.2%
13
Compared to previous month
Last Year
0%
328
Compared to previous year
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.
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.
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.
Install the package via npm or yarn:
npm install express-exceptions-handler
Here is a basic example of how to integrate the Express Exceptions Handler Middleware into your Express.js application:
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');
});
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.
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);
}
});
The errorHandlerMiddleware
is a robust Express.js middleware that catches and processes all thrown exceptions in your application.
{
"data": null,
"message": "Error message",
"statusCode": 500,
"isSuccess": false
}
data: Always null
for errors.
message: The specific error message.
statusCode: The HTTP status code of the error.
isSuccess: Always false
for errors.
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);
}
});
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
We welcome contributions! If you find bugs or have feature requests, please open an issue or submit a pull request on the GitHub repository.
This project is licensed under the MIT License. See the LICENSE file for details.
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
No vulnerabilities found.
No security vulnerabilities found.