# egg-onerror

> error handler for egg

Latest version **2.4.0** (published 2024-10-13) · 0 weekly downloads

## Install

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

## Health

**Score 30/100 (F)** — status: maintenance-mode.

Positive: no vulnerabilities; has provenance.

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

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.4.0 |
| Published | 2024-10-13 |
| First published | 2016-05-26 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=8.0.0 |
| Dependencies | 4 |
| Unpacked size | 49.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 57 |
| Author | dead_horse |
| Maintainers | atian25, dead_horse, wanghx, fengmk2, popomore, eggjs-admin |
| Keywords | egg, egg-plugin, onerror |

## Links

- npm: https://www.npmjs.com/package/egg-onerror
- Repository: https://github.com/eggjs/egg-onerror
- Homepage: https://github.com/eggjs/egg-onerror#readme
- Issues: https://github.com/eggjs/egg-onerror/issues
- npm.io page: https://npm.io/package/egg-onerror

## Dependencies (4)

- [cookie](https://npm.io/package/cookie.md) ^0.7.2
- [mustache](https://npm.io/package/mustache.md) ^2.3.0
- [koa-onerror](https://npm.io/package/koa-onerror.md) ^4.0.0
- [stack-trace](https://npm.io/package/stack-trace.md) ^0.0.10

## 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

- 2.4.0 (latest) — 2024-10-13
- 2.3.1 — 2024-10-13
- 2.3.0 — 2024-10-13
- 2.2.0 — 2022-12-11
- 2.1.1 — 2022-08-18
- 2.1.0 — 2018-06-12
- 2.0.0 — 2017-11-13
- 1.6.0 — 2017-11-13
- 1.5.0 — 2017-07-20
- 1.4.6 — 2017-06-20
- 1.4.5 — 2017-06-20
- 1.4.4 — 2017-06-12
- 1.4.3 — 2017-06-04
- 1.4.2 — 2017-06-01
- 1.4.1 — 2017-05-31
- … 9 more at https://npm.io/package/egg-onerror/versions

## README

# egg-onerror

[![NPM version][npm-image]][npm-url]
[![Node.js CI](https://github.com/eggjs/egg-onerror/actions/workflows/nodejs.yml/badge.svg)](https://github.com/eggjs/egg-onerror/actions/workflows/nodejs.yml)
[![Test coverage][codecov-image]][codecov-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url]

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

Default error handling plugin for egg.

## Install

```bash
npm i egg-onerror
```

## Usage

`egg-onerror` is on by default in egg. But you still can configure its properties to fits your scenarios.

- `errorPageUrl: String or Function` - If user request html pages in production environment and unexpected error happened, it will redirect user to `errorPageUrl`.
- `accepts: Function` - detect user's request accept `json` or `html`.
- `all: Function` - customize error handler, if `all` present, negotiation will be ignored.
- `html: Function` - customize html error handler.
- `text: Function` - customize text error handler.
- `json: Function` - customize json error handler.
- `jsonp: Function` - customize jsonp error handler.

```js
// config.default.js
// errorPageUrl support function
exports.onerror = {
  errorPageUrl: (err, ctx) => ctx.errorPageUrl || '/500',
};

// an accept detect function that mark all request with `x-requested-with=XMLHttpRequest` header accepts json.
function accepts(ctx) {
  if (ctx.get('x-requested-with') === 'XMLHttpRequest') return 'json';
  return 'html';
}
```

## Questions & Suggestions

Please open an issue [here](https://github.com/eggjs/egg/issues).

## License

[MIT](https://github.com/eggjs/egg-onerror/blob/master/LICENSE)

## Contributors

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

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

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