{"id":13670999,"url":"https://github.com/koajs/onerror","last_synced_at":"2026-01-30T10:28:50.506Z","repository":{"id":16130682,"uuid":"18875955","full_name":"koajs/onerror","owner":"koajs","description":"an error handler for koa, hack ctx.onerror","archived":false,"fork":false,"pushed_at":"2022-02-09T05:26:04.000Z","size":75,"stargazers_count":140,"open_issues_count":4,"forks_count":20,"subscribers_count":10,"default_branch":"master","last_synced_at":"2024-10-29T15:32:17.576Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/koajs.png","metadata":{"files":{"readme":"README.md","changelog":"History.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-04-17T11:23:47.000Z","updated_at":"2024-07-24T09:34:58.000Z","dependencies_parsed_at":"2022-08-02T16:01:17.476Z","dependency_job_id":null,"html_url":"https://github.com/koajs/onerror","commit_stats":null,"previous_names":["node-modules/koa-error-handler"],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Fonerror","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Fonerror/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Fonerror/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Fonerror/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/koajs","download_url":"https://codeload.github.com/koajs/onerror/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247601475,"owners_count":20964869,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T09:00:55.438Z","updated_at":"2026-01-30T10:28:50.467Z","avatar_url":"https://github.com/koajs.png","language":"JavaScript","readme":"# koa-onerror\n\n[![NPM version][npm-image]][npm-url]\n[![Test coverage][codecov-image]][codecov-url]\n[![Known Vulnerabilities][snyk-image]][snyk-url]\n[![npm download][download-image]][download-url]\n[![Node.js Version](https://img.shields.io/node/v/koa-onerror.svg?style=flat)](https://nodejs.org/en/download/)\n[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://makeapullrequest.com)\n\n[npm-image]: https://img.shields.io/npm/v/koa-onerror.svg?style=flat\n[npm-url]: https://npmjs.org/package/koa-onerror\n[codecov-image]: https://codecov.io/gh/koajs/onerror/branch/master/graph/badge.svg\n[codecov-url]: https://codecov.io/gh/koajs/onerror\n[snyk-image]: https://snyk.io/test/npm/koa-onerror/badge.svg?style=flat-square\n[snyk-url]: https://snyk.io/test/npm/koa-onerror\n[download-image]: https://img.shields.io/npm/dm/koa-onerror.svg?style=flat-square\n[download-url]: https://npmjs.org/package/koa-onerror\n\nan error handler for koa, hack ctx.onerror.\n\ndifferent with [koa-error](https://github.com/koajs/error):\n\n- we can not just use try catch to handle all errors, steams' and events'\nerrors are directly handle by `ctx.onerror`, so if we want to handle all\nerrors in one place, the only way i can see is to hack `ctx.onerror`.\n- it is more customizable.\n\n## install\n\n```bash\nnpm install koa-onerror\n```\n\n## Usage\n\n```js\nconst fs = require('fs');\nconst Koa = require('koa');\nconst { onerror } = require('koa-onerror');\n\nconst app = new Koa();\n\nonerror(app);\n\napp.use(ctx =\u003e {\n  // foo();\n  ctx.body = fs.createReadStream('not exist');\n});\n```\n\n## Options\n\n```js\nonerror(app, options);\n```\n\n- **all**: if `options.all` exist, ignore negotiation\n- **text**: text error handler\n- **json**: json error handler\n- **html**: html error handler\n- **redirect**: if accept `html` or `text`, can redirect to another error page\n\ncheck out default handler to write your own handler.\n\n## Status and Headers\n\n`koa-onerror` will automatic set `err.status` as response status code, and `err.headers` as response headers.\n\n## License\n\n[MIT](LICENSE)\n\n## Contributors\n\n[![Contributors](https://contrib.rocks/image?repo=koajs/onerror)](https://github.com/koajs/onerror/graphs/contributors)\n\nMade with [contributors-img](https://contrib.rocks).\n","funding_links":[],"categories":["Middleware","仓库","JavaScript"],"sub_categories":["中间件"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkoajs%2Fonerror","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkoajs%2Fonerror","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkoajs%2Fonerror/lists"}