Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

an error handler for koa, hack ctx.onerror

License

NotificationsYou must be signed in to change notification settings

koajs/onerror

Repository files navigation

NPM versionTest coverageKnown Vulnerabilitiesnpm downloadNode.js VersionPRs Welcome

an error handler for koa, hack ctx.onerror.

different withkoa-error:

  • we can not just use try catch to handle all errors, steams' and events'errors are directly handle byctx.onerror, so if we want to handle allerrors in one place, the only way i can see is to hackctx.onerror.
  • it is more customizable.

install

npm install koa-onerror

Usage

constfs=require('fs');constKoa=require('koa');const{ onerror}=require('koa-onerror');constapp=newKoa();onerror(app);app.use(ctx=>{// foo();ctx.body=fs.createReadStream('not exist');});

Options

onerror(app,options);
  • all: ifoptions.all exist, ignore negotiation
  • text: text error handler
  • json: json error handler
  • html: html error handler
  • redirect: if accepthtml ortext, can redirect to another error page

check out default handler to write your own handler.

Status and Headers

koa-onerror will automatic seterr.status as response status code, anderr.headers as response headers.

License

MIT

Contributors

Contributors

Made withcontributors-img.

About

an error handler for koa, hack ctx.onerror

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Contributors6


[8]ページ先頭

©2009-2025 Movatter.jp