Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/koajs/onerror
an error handler for koa, hack ctx.onerror
https://github.com/koajs/onerror
Last synced: 29 days ago
JSON representation
an error handler for koa, hack ctx.onerror
- Host: GitHub
- URL: https://github.com/koajs/onerror
- Owner: koajs
- License: mit
- Created: 2014-04-17T11:23:47.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2022-02-09T05:26:04.000Z (over 2 years ago)
- Last Synced: 2024-04-14T13:08:40.587Z (7 months ago)
- Language: JavaScript
- Size: 73.2 KB
- Stars: 141
- Watchers: 9
- Forks: 19
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Changelog: History.md
- License: LICENSE
Awesome Lists containing this project
- awesome-github-star - onerror
- awesome-koa - onerror - an error handler for koa, hack ctx.onerror (Middleware)
- awesome-koa - koa-onerror - Koa 的错误处理程序,hack ctx.onerror。 ![](https://img.shields.io/github/stars/koajs/onerror.svg?style=social&label=Star) ![](https://img.shields.io/npm/dm/koa-onerror.svg?style=flat-square) (仓库 / 中间件)
README
koa-onerror
=================[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][codecov-image]][codecov-url]
[![David deps][david-image]][david-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url][npm-image]: https://img.shields.io/npm/v/koa-onerror.svg?style=flat
[npm-url]: https://npmjs.org/package/koa-onerror
[travis-image]: https://img.shields.io/travis/koajs/onerror.svg?style=flat
[travis-url]: https://travis-ci.org/koajs/onerror
[codecov-image]: https://codecov.io/gh/koajs/onerror/branch/master/graph/badge.svg
[codecov-url]: https://codecov.io/gh/koajs/onerror
[david-image]: https://img.shields.io/david/koajs/onerror.svg?style=flat
[david-url]: https://david-dm.org/koajs/onerror
[snyk-image]: https://snyk.io/test/npm/koa-onerror/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/koa-onerror
[download-image]: https://img.shields.io/npm/dm/koa-onerror.svg?style=flat-square
[download-url]: https://npmjs.org/package/koa-onerroran error handler for koa, hack ctx.onerror.
different with [koa-error](https://github.com/koajs/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
```bash
npm install koa-onerror
```## Usage
```js
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
```js
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 pagecheck 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](LICENSE)
## Contributors
[![](https://ergatejs.implements.io/badges/contributors/koajs/onerror.svg?size=96)](https://github.com/koajs/onerror/graphs/contributors)