@acruzjr/express-http-errors
v1.0.2
Published
The easiest way to handle errors in Express.
Downloads
13
Maintainers
Readme
Express HTTP Errors Handler
A simple node.js module that handle HTTP Errors for express. You can handle HTTP errors easier using object-orientation.
Install
npm install @acruzjr/express-http-errors
Usage
Initialize your express app, add your routes and make it uses ExpressErrorHandle after ALL middlewares and routes. Using ExpressErrorHandle before routes declaration will not work. Throw your exception inside express NextFunction.
var app = require("express")();
var { ExpressErrorHandler } = require("@acruzjr/express-http-errors");
app.get(
'/test400',
middleware400
);
// curl -s localhost:3000/test400
// Expect BadRequest Error
function middleware400(req, res, next) {
return next(new BadRequestError('You typed something wrong. It\'s never late to fix it.'))
}
app.get(
'/test404',
middleware404
);
// curl -s localhost:3000/test404
// Expect NotFound Error
function middleware404(req, res, next) {
return next(new NotFoundError('You are beyond borders'))
}
app.get(
'/test500',
middleware500
);
// curl -s localhost:3000/test500
// Expect InternalServerError
function middleware500(req, res, next) {
return next(new InternalServerError('Well, this is embarrassing...'))
}
// Make the app use error handler middleware AFTER ALL middlewares and routes uses.
app.use(ExpressErrorHandler);
app.listen(3000);
API
HTTPErrorName(message, error)
message - string
error - any, error object
new BadRequestError(message: string, error: unknown)
Examples
Examples are available here
Test
npm test