名称 最后更新
..
History.md 正在载入提交数据...
LICENSE 正在载入提交数据...
README.md 正在载入提交数据...
error.html 正在载入提交数据...
index.js 正在载入提交数据...
package.json 正在载入提交数据...

koa-onerror

NPM version build status Test coverage Gittip David deps

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

var fs = require('fs');
var koa = require('koa');
var onerror = require('koa-onerror');
var app = koa();

onerror(app);

app.use(function *(){
  // foo();
  this.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
  • template: default html error handler template path
  • 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