@kramerdev/express-async-error-handler
TypeScript icon, indicating that this package has built-in type declarations

1.2.2 • Public • Published

Express Async Error Handler

Node.js CI License: MIT NPM

Table of Contents

About
Setup

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")
  }

}

Package Sidebar

Install

npm i @kramerdev/express-async-error-handler

Weekly Downloads

1

Version

1.2.2

License

MIT

Unpacked Size

10.2 kB

Total Files

11

Last publish

Collaborators

  • kramerdev