Need help with onerror?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

koajs
136 Stars 17 Forks MIT License 63 Commits 5 Opened issues

Description

an error handler for koa, hack ctx.onerror

Services available

!
?

Need anything else?

Contributors list

koa-onerror

NPM version build status Test coverage David deps Known Vulnerabilities npm download

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 accepct html, 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

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.