A collection of error types, handlers and middleware for use with Meanie Express Seed projects
You can install this package using npm
.
npm install express-error-handling --save
//Load library
const errors = require('express-error-handling');
//Define some custom middleware
const myHandler = function(error, req, res, next) { ... };
//Register it with the error handler
errors.register('my-handler', myHandler);
//Specify default middleware stack to use
errors.use([
'normalize', 'track-with-sentry', 'my-handler', 'log-to-console',
]);
Load stack of pre-registered error handling middleware:
const stack = errors.middleware();
Use it as express middleware:
stack.forEach(handler => app.use(handler));
Run an error through error handling middleware stack:
someRoute(req, res, next) {
doSomething()
.then(() => {
//End response
res.end();
//Do something else async which won't be part of the request
//error handling stack, but catch and process the errors anyway.
doSomethingElse()
.catch(error => errors.handler(error, req));
})
.catch(next);
}
Use some of the pre defined error types:
//Load error
const BadRequestError = errors.BadRequestError;
//Use in route
someRoute(req, res, next) {
if (somethingBad()) {
return next(new BadRequestError('Bad things'))
}
next();
}
See the Meanie Express Seed for usage.
Please report any bugs, issues, suggestions and feature requests in the express-error-handling issue tracker.
Pull requests are welcome! If you would like to contribute to Meanie, please check out the Meanie contributing guidelines.
- Meanie logo designed by Quan-Lin Sim
(MIT License)
Copyright 2016-2017, Adam Reis