@valbo/error-handler-middleware
v1.0.5
Published
Express error middleware that sends the error as a json response
Downloads
2
Readme
@valbo/error-handler-middleware
Express error middleware that sends the error as a json response.
Install
npm install @valbo/error-handler-middleware
Usage
Add it to express at the end of the middleware chain to capture the error thrown and send it as a JSON response:
import { errorHandlerMiddleware } from '@valbo/error-handler-middleware';
// at the end of the middleware chain
app.use(errorHandlerMiddleware);
This error response will have the following format:
{
"error" : {
"status": 400,
"name": "BadRequestError",
"message": ".find should NOT have additional properties"
}
}
The status will be error.status, error.statusCode or 500 as a default.
The name will be error.name or InternalServerError as a default.
It will also store the error sent in response.locals.responseError if you want to do something else with it, like logging.