1. koa-onerror
koa error handler, hack ctx.onerror
koa-onerror
Package: koa-onerror
Created by: koajs
Last modified: Fri, 21 Jul 2023 15:34:04 GMT
Version: 4.2.0
License: MIT
Downloads: 109,810
Repository: https://github.com/koajs/onerror

Install

npm install koa-onerror
yarn add koa-onerror

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

Contributors

RELATED POST

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin