Express Async Error Handler
Table of Contents
Installation
npm i @kramerdev/express-async-error-handler
About
This package implements a async error handler.
When you have unhandled promises inside Express routes, if no handler is used, you're server will probably crash if they throw an error. In those cases, you can use this package.
Setup
Middleware
First you need to create a generic handler function with the following signature:
(error: Error, req: Request, res: Response, next: NextFunction) => any
Example:
const handleErrorFunction = (error, req, res) => {
res.status(500).send({error: error.message})
}
Then you need to setup the middleware, as follows:
app.use(AsyncErrorHandlerMiddleware(handleErrorFunction))
Wrapper
If you're not using classes for your routes, you can use as a wrapper function, example:
app.get("/", AsyncErrorHandler(
async (req, res) => {
res.send("OK")
}
))
Decorator
If you're using classes for your routes, you can use as a decorator, example:
class Controller {
@AsyncErrorHandler()
async home(req, res) {
res.send("OK")
}
}