{"id":22096491,"url":"https://github.com/uphold/koa-error-mapper","last_synced_at":"2025-07-24T22:32:04.417Z","repository":{"id":28449533,"uuid":"31964997","full_name":"uphold/koa-error-mapper","owner":"uphold","description":"koa error mapper","archived":false,"fork":false,"pushed_at":"2023-07-18T21:49:50.000Z","size":793,"stargazers_count":1,"open_issues_count":16,"forks_count":4,"subscribers_count":61,"default_branch":"master","last_synced_at":"2024-11-16T16:23:46.736Z","etag":null,"topics":["error","koa2","mapper"],"latest_commit_sha":null,"homepage":"","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/uphold.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-03-10T15:24:32.000Z","updated_at":"2021-05-25T13:04:36.000Z","dependencies_parsed_at":"2024-11-16T16:23:18.287Z","dependency_job_id":"4f988ae2-e2f0-4b44-a47e-e2f92e47374a","html_url":"https://github.com/uphold/koa-error-mapper","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uphold%2Fkoa-error-mapper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uphold%2Fkoa-error-mapper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uphold%2Fkoa-error-mapper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uphold%2Fkoa-error-mapper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/uphold","download_url":"https://codeload.github.com/uphold/koa-error-mapper/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227482486,"owners_count":17779968,"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","koa2","mapper"],"created_at":"2024-12-01T04:11:13.245Z","updated_at":"2024-12-01T04:11:13.952Z","avatar_url":"https://github.com/uphold.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# koa-error-mapper\n\n`koa-error-mapper` is a middleware that handles application errors by mapping them to a custom format.\n\n## Status\n\n[![npm version][npm-image]][npm-url] [![build status][travis-image]][travis-url]\n\n## Installation\n\nInstall the package via `yarn`:\n\n```sh\n❯ yarn add koa-error-mapper\n```\n\nor via `npm`:\n\n```sh\n❯ npm install koa-error-mapper --save\n```\n\n## Usage\n\n### Mappers\n\nMappers are responsible for handling errors and normalizing the response. A generic fallback mapper is included in case no custom mapper is available.\n\nThe only interface for a mapper is a `map()` function. A mapper should return `undefined` if it is not capable or responsible for mapping a specific error. It must return an object with `status` and `body`. If not returned on the object, the value on the response will be `undefined` for precaution. It may also contain a `headers` property.\n\n```javascript\nmodule.exports = {\n  map(e) {\n    if (!(e instanceof CustomError)) {\n      return;\n    }\n\n    // Add your own custom logic here.\n    return { status: e.code, body: { message: e.message }, headers: { Foo: 'Bar' }};\n  }\n};\n```\n\nNow use the error mapper and register `CustomMapper`:\n\n```javascript\n'use strict';\n\nconst CustomError = require('path/to/my/custom/error');\nconst Koa = require('koa');\nconst app = new Koa();\nconst customMapper = require('path/to/my/custom/mapper');\n\napp.use(errorMapper([customMapper]);\n\napp.get('/', async () =\u003e {\n  throw new CustomError(401, 'Ah-ah!');\n});\n\napp.listen(3000);\n```\n\nResult:\n\n```sh\nGET /\n\nHTTP/1.1 401 Unauthorized\nFoo: Bar\n\n{ \"message\": \"Ah-ah!\" }\n```\n\n## Tests\n\n```sh\n❯ yarn test\n```\n\n## Release\n\n```sh\n❯ npm version [\u003cnew version\u003e | major | minor | patch] -m \"Release %s\"\n```\n\n## License\n\nMIT\n\n[npm-image]: https://img.shields.io/npm/v/koa-error-mapper.svg?style=flat-square\n[npm-url]: https://www.npmjs.com/package/koa-error-mapper\n[travis-image]: https://img.shields.io/travis/uphold/koa-error-mapper.svg?style=flat-square\n[travis-url]: https://img.shields.io/travis/uphold/koa-error-mapper\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuphold%2Fkoa-error-mapper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fuphold%2Fkoa-error-mapper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuphold%2Fkoa-error-mapper/lists"}