Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

koa-onerror

koajs115.1kMIT5.0.1TypeScript support: included

koa error handler, hack ctx.onerror

koa, middleware, error

readme

koa-onerror

NPM version Test coverage Known Vulnerabilities npm download Node.js Version PRs Welcome

an error handler for koa, hack ctx.onerror.

different with koa-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

npm install koa-onerror

Usage

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

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

Contributors

Contributors

Made with contributors-img.

changelog

Changelog

5.0.1 (2025-02-02)

Bug Fixes

5.0.0 (2025-02-02)

⚠ BREAKING CHANGES

  • drop Node.js < 18.19.0 support

part of https://github.com/eggjs/egg/issues/3644

https://github.com/eggjs/egg/issues/5257

Features

  • support cjs and esm both by tshy (#38) (8177649)

4.2.0 / 2022-02-09

features

4.1.0 / 2018-08-19

fixes

4.0.1 / 2018-08-19

fixes

4.0.0 / 2017-11-09

others

3.1.0 / 2017-03-02

  • feat: can reach err.headerSent in app error listener (#23)
  • feat: non-error wrapper support status and headers (#22)

3.0.2 / 2017-02-16

  • fix: try to restore non Error instance properties (#20)
  • fix: change the koa-error url (#17)

3.0.1 / 2016-10-21

  • fix: use absolute path (#16)

3.0.0 / 2016-10-21

  • fix: Send default text/plain body if message is undefined
  • refactor: remove nunjucks

2.1.0 / 2016-10-19

  • fix: don't throw when non-error object passed (#15)
  • Return reference to app (#7)

2.0.0 / 2016-07-04

  • refactor: use nunjucks instead of swig

1.3.1 / 2016-03-21

  • fix: only unset text headers

1.3.0 / 2016-03-10

  • feat: support set err.headers

1.2.1 / 2015-05-13

  • Merge pull request #5 from koajs/fix-test-iojs
  • fix: test run on iojs and upgrade copy-to

1.2.0 / 2014-08-08

  • fix status in on error

1.1.0 / 2014-08-05

  • fix link
  • Merge pull request #4 from koajs/custom-accepts
  • update links and add coveralls
  • Support options.accepts custom detect function
  • fix readme

1.0.3 / 2014-04-25

  • Merge pull request #2 from koajs/redirect
  • Allow options.redirect = 'http://example/500.html'.

1.0.2 / 2014-04-25

  • use path.join

1.0.1 / 2014-04-25

  • add assert error type
  • update repo

1.0.0 / 2014-04-24

  • refine readme, bump dependencies
  • fix status

0.0.2 / 2014-04-18

  • all do not set type

0.0.1 / 2014-04-18

  • add test
  • fix status
  • rename to koa-onerror
  • refactor
  • update readme
  • update readme
  • error handler by hack ctx.onerror
  • Initial commit