Error Middleware for Express
This package handles errors on the express code and returns it with a fixed pattern, without leaks. It works with syncronous and asyncronous route functions.
Installation
npm install error-middleware
or
yarn add error-middleware
Error Types
Currently the middleware has exposed the following error types:
BadRequestError
ForbiddenError
NotFoundError
UnauthorizedError
Validation Error
But you can also use
CustomError
Error response format
The consumer will receive a JSON object with code
, type
and error
as folows:
{
code: <error code>,
type: <error type>,
error: <error message>
}
Example of expected responses
Code | Response |
---|---|
|
|
|
|
|
|
|
|
How to use
-
src/index.js
Where you declare all your high level routes. The middleware should be last to be able to catch all errors.routerrouter -
src/routes.js
routerrouter// If no matches found, return 404router
Check examples for more