# koa-onerror

> koa error handler, hack ctx.onerror

Latest version **5.0.1** (published 2025-02-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install koa-onerror
pnpm add koa-onerror
yarn add koa-onerror
bun add koa-onerror
```

## Health

**Score 45/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; has provenance; high quality score.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 5.0.1 |
| Published | 2025-02-02 |
| First published | 2014-04-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >= 18.19.0 |
| Dependencies | 2 |
| Unpacked size | 38.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 141 |
| Author | dead_horse |
| Maintainers | dead_horse, fengmk2 |
| Keywords | koa, middleware, error |

## Links

- npm: https://www.npmjs.com/package/koa-onerror
- Repository: https://github.com/koajs/onerror
- Issues: https://github.com/koajs/onerror/issues
- npm.io page: https://npm.io/package/koa-onerror

## Dependencies (2)

- [escape-html](https://npm.io/package/escape-html.md) ^1.0.3
- [stream-wormhole](https://npm.io/package/stream-wormhole.md) ^2.0.1

## Alternatives

- [@sentry/react-native](https://npm.io/package/@sentry/react-native.md) — 2.6M weekly downloads
- [@ardatan/aggregate-error](https://npm.io/package/@ardatan/aggregate-error.md) — 708.1K weekly downloads
- [custom-error-generator](https://npm.io/package/custom-error-generator.md) — 2.0K weekly downloads
- [@technik-sde/prosemirror-recreate-transform](https://npm.io/package/@technik-sde/prosemirror-recreate-transform.md) — 1.5K weekly downloads
- [@suchipi/error-utils](https://npm.io/package/@suchipi/error-utils.md) — 78 weekly downloads

## Recent versions

- 5.0.1 (latest) — 2025-02-02
- 5.0.0 — 2025-02-02
- 4.2.0 — 2022-02-09
- 4.1.0 — 2018-08-19
- 4.0.1 — 2018-08-18
- 4.0.0 — 2017-11-09
- 3.1.0 — 2017-03-02
- 3.0.2 — 2017-02-16
- 3.0.1 — 2016-10-21
- 3.0.0 — 2016-10-21
- 2.1.0 — 2016-10-19
- 2.0.0 — 2016-07-04
- 1.3.1 — 2016-03-21
- 1.3.0 — 2016-03-10
- 1.2.1 — 2015-05-13
- … 9 more at https://npm.io/package/koa-onerror/versions

## README

# koa-onerror

[![NPM version][npm-image]][npm-url]
[![Test coverage][codecov-image]][codecov-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url]
[![Node.js Version](https://img.shields.io/node/v/koa-onerror.svg?style=flat)](https://nodejs.org/en/download/)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://makeapullrequest.com)

[npm-image]: https://img.shields.io/npm/v/koa-onerror.svg?style=flat
[npm-url]: https://npmjs.org/package/koa-onerror
[codecov-image]: https://codecov.io/gh/koajs/onerror/branch/master/graph/badge.svg
[codecov-url]: https://codecov.io/gh/koajs/onerror
[snyk-image]: https://snyk.io/test/npm/koa-onerror/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/koa-onerror
[download-image]: https://img.shields.io/npm/dm/koa-onerror.svg?style=flat-square
[download-url]: https://npmjs.org/package/koa-onerror

an error handler for koa, hack ctx.onerror.

different with [koa-error](https://github.com/koajs/error):

- we can not just use try catch to handle all errors, steams' and events'
errors are directly handle by `ctx.onerror`, so if we want to handle all
errors in one place, the only way i can see is to hack `ctx.onerror`.
- it is more customizable.

## install

```bash
npm install koa-onerror
```

## Usage

```js
const fs = require('fs');
const Koa = require('koa');
const { onerror } = require('koa-onerror');

const app = new Koa();

onerror(app);

app.use(ctx => {
  // foo();
  ctx.body = fs.createReadStream('not exist');
});
```

## Options

```js
onerror(app, options);
```

- **all**: if `options.all` exist, ignore negotiation
- **text**: text error handler
- **json**: json error handler
- **html**: html error handler
- **redirect**: if accept `html` or `text`, can redirect to another error page

check out default handler to write your own handler.

## Status and Headers

`koa-onerror` will automatic set `err.status` as response status code, and `err.headers` as response headers.

## License

[MIT](LICENSE)

## Contributors

[![Contributors](https://contrib.rocks/image?repo=koajs/onerror)](https://github.com/koajs/onerror/graphs/contributors)

Made with [contributors-img](https://contrib.rocks).

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