{"id":13657314,"url":"https://github.com/koajs/json-error","last_synced_at":"2025-04-13T00:48:30.431Z","repository":{"id":16555906,"uuid":"19309625","full_name":"koajs/json-error","owner":"koajs","description":"Error handler for pure-JSON apps","archived":false,"fork":false,"pushed_at":"2025-03-20T06:37:59.000Z","size":49,"stargazers_count":92,"open_issues_count":0,"forks_count":3,"subscribers_count":7,"default_branch":"develop","last_synced_at":"2025-04-13T00:48:26.305Z","etag":null,"topics":["error-handler","json","koa"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"mldbai/deepteach","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/koajs.png","metadata":{"files":{"readme":"README.md","changelog":null,"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-30T10:06:16.000Z","updated_at":"2025-01-11T00:18:04.000Z","dependencies_parsed_at":"2022-09-05T15:20:30.194Z","dependency_job_id":null,"html_url":"https://github.com/koajs/json-error","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Fjson-error","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Fjson-error/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Fjson-error/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Fjson-error/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/koajs","download_url":"https://codeload.github.com/koajs/json-error/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248650437,"owners_count":21139672,"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":["error-handler","json","koa"],"created_at":"2024-08-02T05:00:40.682Z","updated_at":"2025-04-13T00:48:30.411Z","avatar_url":"https://github.com/koajs.png","language":"JavaScript","readme":"# Koa JSON Error\n\n[![NPM version][npm-image]][npm-url]\n[![Build status][travis-image]][travis-url]\n[![Test coverage][codecov-image]][codecov-url]\n[![Dependency Status][david-image]][david-url]\n[![License][license-image]][license-url]\n[![Downloads][downloads-image]][downloads-url]\n\nError handler for pure [Koa](https://koajs.com) `\u003e=2.0.0` JSON apps where showing the stack trace is _cool!_\n\n```sh\nnpm install --save koa-json-error\n```\n\n\u003e Versions `\u003e=3.0.0` support Koa `^2.0.0`. For earlier versions of Koa, _please use previous releases_.\n\n## Requirements\n- node `\u003e=6.0.0`\n- koa `\u003e=2.2.0`\n\n\u003e Starting from `3.2.0`, this package supports node `\u003e=6.0.0` to match [Koa requirements][koa-requirements].\n\n\n## API\n\n```js\n'use strict';\nconst koa = require('koa');\nconst error = require('koa-json-error')\n\nlet app = new Koa();\napp.use(error())\n```\n\nIf you don't really feel that showing the stack trace is _that_ cool, you can customize the way errors are shown on responses. There's a **basic** and more **advanced**, granular approach to this.\n\n### Basic usage\nYou can provide a _single formatter function_ as an argument on middleware initialization. It receives the original raised error and it is expected to return a formatted response.\n\nHere's a simple example:\n\n```js\n'use strict';\nconst koa = require('koa');\nconst error = require('koa-json-error')\n\nfunction formatError(err) {\n    return {\n        // Copy some attributes from\n        // the original error\n        status: err.status,\n        message: err.message,\n\n        // ...or add some custom ones\n        success: false,\n        reason: 'Unexpected'\n    }\n}\n\nlet app = new Koa();\napp.use(error(formatError));\n```\n\nThis basic configuration is essentially the same (and serves as a shorthand for) the following:\n\n```js\n'use strict';\nlet app = new Koa();\napp.use(error({\n    preFormat: null,\n    format: formatError\n}));\n```\n\nSee section below.\n\n### Advanced usage\nYou can also customize errors on responses through a series of _three formatter functions_, specified in an `options` object. They receive the raw error object and return a formatted response. This gives you fine-grained control over the final output and allows for different formats on various environments.\n\nYou may pass in the `options` object as argument to the middleware. These are the available settings.\n\n#### `options.preFormat (Function)`\nPerform some task before calling `options.format`. Must be a function with the original `err` as its only argument.\n\nDefaults to:\n\n```js\n(err) =\u003e Object.assign({}, err)\n```\n\nWhich sets all enumerable properties of `err` onto the formatted object.\n\n#### `options.format (Function)`\nRuns inmediatly after `options.preFormat`. It receives two arguments: the original `err` and the output of `options.preFormat`. It should `return` a newly formatted error.\n\nDefaults to adding the following non-enumerable properties to the output:\n\n```js\nconst DEFAULT_PROPERTIES = [\n  'name',\n  'message',\n  'stack',\n  'type'\n];\n```\n\nIt also defines a `status` property like so:\n\n```js\nobj.status = err.status || err.statusCode || 500;\n```\n\n#### `options.postFormat (Function)`\nRuns inmediatly after `options.format`. It receives two arguments: the original `err` and the output of `options.format`. It should `return` a newly formatted error.\n\nThe default is a no-op (final output is defined by `options.format`).\n\nThis option is useful when you want to preserve the default functionality and extend it in some way.\n\nFor example,\n```js\n'use strict';\nconst _ = require('lodash');\nconst koa = require('koa');\nconst error = require('koa-json-error')\n\nlet options = {\n    // Avoid showing the stacktrace in 'production' env\n    postFormat: (e, obj) =\u003e process.env.NODE_ENV === 'production' ? _.omit(obj, 'stack') : obj\n};\nlet app = new Koa();\napp.use(error(options));\n```\n\n\u003e Modifying the error inside the `*format` functions will mutate the original object. Be aware of that if any other Koa middleware runs after this one.\n\n[npm-image]: https://img.shields.io/npm/v/koa-json-error.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/koa-json-error\n[travis-image]: https://img.shields.io/travis/koajs/json-error/master.svg?style=flat-square\n[travis-url]: https://travis-ci.org/koajs/json-error\n[codecov-image]: https://img.shields.io/codecov/c/github/koajs/json-error/master.svg?style=flat-square\n[codecov-url]: https://codecov.io/github/koajs/json-error\n[david-image]: http://img.shields.io/david/koajs/json-error.svg?style=flat-square\n[david-url]: https://david-dm.org/koajs/json-error\n[license-image]: http://img.shields.io/npm/l/koa-json-error.svg?style=flat-square\n[license-url]: LICENSE\n[downloads-image]: http://img.shields.io/npm/dm/koa-json-error.svg?style=flat-square\n[downloads-url]: https://npmjs.org/package/koa-json-error\n[koa-requirements]: https://github.com/koajs/koa/blob/master/package.json#L61\n\n","funding_links":[],"categories":["JavaScript","Middleware","仓库"],"sub_categories":["中间件"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkoajs%2Fjson-error","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkoajs%2Fjson-error","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkoajs%2Fjson-error/lists"}