an error handler for koa, hack ctx.onerror
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.
npm install koa-onerror
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'); });
onerror(app, options);
check out default handler to write your own handler.
koa-onerrorwill automatic set
err.statusas response status code, and
err.headersas response headers.