# express-async-errors

> Async/await error handling support for expressjs

Latest version **3.1.1** (published 2018-10-12) · ISC license · 0 weekly downloads

## Install

```sh
npm install express-async-errors
pnpm add express-async-errors
yarn add express-async-errors
bun add express-async-errors
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.1.1 |
| Published | 2018-10-12 |
| First published | 2016-12-07 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 55 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 904 |
| Author | david@banham.id.au |
| Maintainers | davidbanham |
| Keywords | expressjs, async/await, async, await, es6 |

## Links

- npm: https://www.npmjs.com/package/express-async-errors
- Repository: https://github.com/davidbanham/express-async-errors
- Homepage: https://github.com/davidbanham/express-async-errors#readme
- Issues: https://github.com/davidbanham/express-async-errors/issues
- npm.io page: https://npm.io/package/express-async-errors

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 3.1.1 (latest) — 2018-10-12
- 2.1.1 (assume-promise) — 2017-11-01
- 3.1.0 — 2018-10-12
- 3.0.0 — 2018-06-03
- 2.1.2 — 2018-02-15
- 2.1.0 — 2017-11-01
- 2.0.0 — 2016-12-07

## README

# ExpressJS Async Errors

[![Build Status](https://travis-ci.org/davidbanham/express-async-errors.svg?branch=master)](https://travis-ci.org/davidbanham/express-async-errors)

A dead simple ES6 async/await support hack for [ExpressJS](http://expressjs.com)

Shamelessly copied from [express-yields](https://github.com/MadRabbit/express-yields)

This has been lightly reworked to handle async rather than generators.

## Usage

```
npm install express-async-errors --save
```

Then require this script somewhere __before__ you start using it:

Async functions already work fine in Express.

```js
const express = require('express');
require('express-async-errors');
const User = require('./models/user');
const app = express();

app.get('/users', async (req, res) => {
  const users = await User.findAll();
  res.send(users);
});
```

This library is about what happens when you hit an error.

## A Notice About Calling `next`

As we all know express sends a function called `next` into the middleware, which
then needs to be called with or without error to make it move the request handling
to the next middleware. It still works, but in case of an async function, you
don't need to do that. If you want to pass an error, just throw a normal exception:

```js
app.use(async (req, res) => {
  const user = await User.findByToken(req.get('authorization'));

  if (!user) throw Error("access denied");
});

app.use((err, req, res, next) => {
  if (err.message === 'access denied') {
    res.status(403);
    res.json({ error: err.message });
  }

  next(err);
});
```

## How Does This Work?

This is a very minimalistic and unintrusive hack. Instead of patching all methods
on an express `Router`, it wraps the `Layer#handle` property in one place, leaving
all the rest of the express guts intact.

The idea is that you require the patch once and then use the `'express'` lib the
usual way in the rest of your application.

## License

All code in this repository is released under the terms of the ISC license.

---
_Source: https://npm.io/package/express-async-errors · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
