{"id":25566538,"url":"https://github.com/pdmlab/http-problem-details-mapper","last_synced_at":"2025-06-30T16:33:38.559Z","repository":{"id":35490499,"uuid":"180035485","full_name":"PDMLab/http-problem-details-mapper","owner":"PDMLab","description":"Map your Node.js errors to HTTP Problem details (RFC7807) by convention","archived":false,"fork":false,"pushed_at":"2024-11-18T20:59:09.000Z","size":1046,"stargazers_count":12,"open_issues_count":6,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-17T14:57:55.459Z","etag":null,"topics":["error-handling","http","node","nodejs","rfc7807"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/http-problem-details-mapper","language":"TypeScript","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/PDMLab.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":"2019-04-07T23:18:57.000Z","updated_at":"2025-01-24T12:12:25.000Z","dependencies_parsed_at":"2024-08-26T17:15:35.473Z","dependency_job_id":"4c003129-79cb-4358-9b3f-89d9357e4277","html_url":"https://github.com/PDMLab/http-problem-details-mapper","commit_stats":{"total_commits":59,"total_committers":5,"mean_commits":11.8,"dds":0.6101694915254237,"last_synced_commit":"7e6c210e4624c29d4dcc46c78f4e8b1d33cb479a"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/PDMLab/http-problem-details-mapper","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PDMLab%2Fhttp-problem-details-mapper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PDMLab%2Fhttp-problem-details-mapper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PDMLab%2Fhttp-problem-details-mapper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PDMLab%2Fhttp-problem-details-mapper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PDMLab","download_url":"https://codeload.github.com/PDMLab/http-problem-details-mapper/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PDMLab%2Fhttp-problem-details-mapper/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262810889,"owners_count":23368000,"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-handling","http","node","nodejs","rfc7807"],"created_at":"2025-02-20T22:33:03.711Z","updated_at":"2025-06-30T16:33:38.509Z","avatar_url":"https://github.com/PDMLab.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org) [![](https://img.shields.io/codecov/c/github/pdmlab/http-problem-details-mapper)](https://codecov.io/gh/PDMLab/http-problem-details-mapper) [![Join the chat at https://gitter.im/pdmlab/http-problem-details-mapper](https://badges.gitter.im/pdmlab/http-problem-details-mapper.svg)](https://gitter.im/pdmlab/http-problem-details-mapper?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge) \n\n# HTTP Problem Details Mapper\n\nBased on `http-problem-details` ([repository](https://github.com/PDMLab/http-problem-details) | [npm](https://www.npmjs.com/package/http-problem-details)), this library allows you to map your Node.js errors to HTTP Problem details by convention.\n\n## Installation\n\n```\nnpm install http-problem-details-mapper\n```\n\nor\n\n```\nyarn add http-problem-details-mapper\n```\n\nMake sure to have the peer dependency `http-problem-details` installed as well.\n\n## Usage\n\n### Architecture\n\n`http-problem-details-mapper` is part of a set of libraries you can use to create HTTP Problem Details documents (by means of `http-problem-details` (RFC 7807) and map Errors (or literally everything) into an HTTP Problem Document.\n`http-problem-details-mapper` can be used to build a mapping middleware or plugin for your HTTP library of choice.\nThere's already a mapping middleware available for `express`: `express-http-problem-details`.\n\n`http-problem-details-mapper` provides several classes you need to use:\n\n- `MapperRegistry` which holds an arbitrary number of `ErrorMapper` instances you implement\n- `MappingStrategy` which has a `MapperRegistry` containing the `ErrorMapper` instances\n- The `ErrorMapper` itself maps an object (typically one of your `Error` types) to a `ProblemDocument`\n\n### Example\n\nThe typical workflow with `http-problem-details-mapper` is this:\n\nFirst, you implement an Error\n\n```js\nclass NotFoundError extends Error {\n  constructor (options) {\n    const { type, id } = options\n    super()\n    Error.captureStackTrace(this, this.constructor)\n\n    this.message = `${type} with id ${id} could not be found.`\n  }\n}\n```\n\nNext, you implement an `ErrorMapper` (in TypeScript you can use an `IErrorMapper` interface to implement a mapper from scratch):\n\n```js\nimport { ErrorMapper } from 'http-problem-details-mapper'\nimport { ProblemDocument } from 'http-problem-details'\n\nclass NotFoundErrorMapper extends ErrorMapper {\n  constructor () {\n    super(NotFoundError)\n  }\n\n  mapError (error) {\n    return new ProblemDocument({\n      status: 404,\n      title: error.message,\n      type: 'http://tempuri.org/NotFoundError'\n    })\n  }\n}\n```\n\nThen, create the `IMappingStrategy` implementation:\n\n```js\nclass MyMappingStrategy {\n  constructor (registry) {\n    this.registry = registry\n  }\n\n  map (error) {\n    const err = error\n    const errorMapper = this.registry.getMapper(error)\n    if (errorMapper) {\n      return errorMapper.mapError(err)\n    }\n    \n    // alternatively, return a generic problem document\n    throw new Error('Could not map error')\n  }\n}\n```\n\nFinally, create an instance of `MyMappingStrategy` and map an registered error type.\n\n```js\nimport { MapperRegistry } from 'http-problem-details-mapper'\n\nconst strategy = new MyMappingStrategy(\n    new MapperRegistry()\n      .registerMapper(new NotFoundErrorMapper()))\n\nconst error = new NotFoundError({ type: 'customer', id: '123' })\nconst problem = strategy.map()\n\nconsole.log(problem)\n```\n\nThe result will be like this:\n\n```json\n{\n    \"status\": 404,\n    \"title\": \"customer with id 123 could not be found.\",\n    \"type\": \"http://tempuri.org/NotFoundError\"\n}\n```\n\n`MapperRegistry` also by default has a mapper named `DefaultErrorMapper` which maps generic `Error` instances to HTTP status code 500 problem documents. `MapperRegistry` also has an option `useDefaultErrorMapper` of type `boolean` which allows you to disable the `DefaultErrorMapper` so you can register your own `IErrorMapper` for `Error`.\n\nThere's another mapper named `StatusCodeErrorMapper` which simply acts as a factory for `ProblemDocuments` where you only want to provide an HTTP error status code:\n\n```js\nimport { StatusCodeErrorMapper } from 'http-problem-details-mapper'\n\nconst problem = StatusCodeErrorMapper.mapStatusCode(400)\n```\n\nSimilar to the `DefaultErrorMapper` there's also a `DefaultMappingStrategy` which you can use if you have no specific requirements regarding the mapping behavior.\n\nIt can be used like this:\n\n```js\nimport { MapperRegistry, DefaultMappingStrategy } from 'http-problem-details-mapper'\n\nconst strategy = new DefaultMappingStrategy(\n    new MapperRegistry()\n      .registerMapper(new NotFoundErrorMapper()))\n\nconst error = new NotFoundError({ type: 'customer', id: '123' })\nconst problem = strategy.map()\n\nconsole.log(problem)\n```\n\n## Running the tests\n\n```\nnpm test\n```\n\nor\n\n```\nyarn test\n```\n\n## Want to help?\n\nThis project is just getting off the ground and could use some help with cleaning things up and refactoring.\n\nIf you want to contribute - we'd love it! Just open an issue to work against so you get full credit for your fork. You can open the issue first so we can discuss and you can work your fork as we go along.\n\nIf you see a bug, please be so kind as to show how it's failing, and we'll do our best to get it fixed quickly.\n\nBefore sending a PR, please [create an issue](https://github.com/PDMLab/http-problem-details/issues/new) to introduce your idea and have a reference for your PR.\n\nWe're using [conventional commits](https://www.conventionalcommits.org), so please use it for your commits as well.\n\nAlso please add tests and make sure to run `npm run lint-ts` or `yarn lint-ts`.\n\n## License\n\nMIT License\n\nCopyright (c) 2019 PDMLab\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpdmlab%2Fhttp-problem-details-mapper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpdmlab%2Fhttp-problem-details-mapper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpdmlab%2Fhttp-problem-details-mapper/lists"}