express-async-catch
TypeScript icon, indicating that this package has built-in type declarations

2.0.4 • Public • Published

express-async-catch

Simple wrapper for Express route handlers, which propagates errors to the centralized error handler.

Install

npm install express-async-catch

Usage

  1. Import the wrapper function

    EcmaScript Modules (ESM)

    import { asyncCatch } from 'express-async-catch';

    CommonJS Modules (CJS):

    const asyncCatch = require('express-async-catch');
  2. Wrap your route handlers:

    app.get(
      '/api/product/list',
      asyncCatch(async (req, res, next) => {
        // ...
      }),
    );

Notes

  • This wrapper will catch thrown Errors.
  • This wrapper will catch promise rejections, when the await keyword is used.
  • It cannot catch errors that generated by un-awaited promises.
  • It cannot catch errors that are handled via callbacks.
  • It cannot be used to wrap the error handler (signature err, req, res, next) because it expects the wrapped route handler to have the signature (req, res) or (req, res, next).

Package Sidebar

Install

npm i express-async-catch

Weekly Downloads

1

Version

2.0.4

License

MIT

Unpacked Size

4.48 kB

Total Files

6

Last publish

Collaborators

  • merlin4