{"id":25566520,"url":"https://github.com/pdmlab/express-http-problem-details","last_synced_at":"2025-04-12T10:51:32.217Z","repository":{"id":34747652,"uuid":"183093860","full_name":"PDMLab/express-http-problem-details","owner":"PDMLab","description":"Express middleware to create HTTP Problem Details (RFC7807) by convention","archived":false,"fork":false,"pushed_at":"2023-01-07T22:00:32.000Z","size":1071,"stargazers_count":17,"open_issues_count":3,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-12T04:47:15.788Z","etag":null,"topics":["api","error-handling","express","express-middleware","expressjs","http","hypermedia","node","node-typescript","nodejs","rest","rfc7807"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/express-http-problem-details","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}},"created_at":"2019-04-23T20:48:59.000Z","updated_at":"2024-04-20T15:28:42.000Z","dependencies_parsed_at":"2023-01-15T08:56:41.593Z","dependency_job_id":null,"html_url":"https://github.com/PDMLab/express-http-problem-details","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PDMLab%2Fexpress-http-problem-details","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PDMLab%2Fexpress-http-problem-details/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PDMLab%2Fexpress-http-problem-details/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PDMLab%2Fexpress-http-problem-details/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PDMLab","download_url":"https://codeload.github.com/PDMLab/express-http-problem-details/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248557844,"owners_count":21124165,"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":["api","error-handling","express","express-middleware","expressjs","http","hypermedia","node","node-typescript","nodejs","rest","rfc7807"],"created_at":"2025-02-20T22:33:00.882Z","updated_at":"2025-04-12T10:51:32.188Z","avatar_url":"https://github.com/PDMLab.png","language":"TypeScript","readme":"[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org) [![Join the chat at https://gitter.im/pdmlab/express-http-problem-details](https://badges.gitter.im/pdmlab/express-http-problem-details.svg)](https://gitter.im/pdmlab/express-http-problem-details?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge)\n\u003cimg src=\"https://img.shields.io/circleci/project/github/PDMLab/express-http-problem-details.svg\" /\u003e \u003ca href=\"https://join.slack.com/t/pdmlab-oss/shared_invite/enQtNjEyMjQ0MDY3NTczLTg1ZDc0YjQxMGE3MTcyYTdkODU1YjFmMTBiODE2ZTZiNDFkNjc1MWE4OTE4NWY0Y2YyMWYzYmNhZGY0NDAyYWY\"\u003e\u003cimg src=\"https://img.shields.io/badge/Slack-join-green.svg?logo=slack\" /\u003e\u003c/a\u003e\n\n# HTTP Problem Details for express\n\nBased on `http-problem-details` ([repository](https://github.com/PDMLab/http-problem-details) | [npm](https://www.npmjs.com/package/http-problem-details)) and `http-problem-details-mapper` ([repository](https://github.com/PDMLab/http-problem-details-mapper) | [npm](https://www.npmjs.com/package/http-problem-details-mapper)), this library allows you to map your Node.js errors to HTTP Problem details according to [RFC7807](https://tools.ietf.org/html/rfc7807) by convention.\n\n## Installation\n\n```\nnpm install express express-http-problem-details\n```\n\nor\n\n```\nyarn add express express-http-problem-details\n```\n\n## Usage\n\n`express-http-problem-details` provides a middleware which allows you to map custom `Error` instances to HTTP Problem Details documents according to RFC7807.\n\nThe details of the mapping itself are described in `http-problem-details-mapper` ([repository](https://github.com/PDMLab/http-problem-details-mapper) | [npm](https://www.npmjs.com/package/http-problem-details-mapper))\n\n### Example\n\nThe typical workflow in JavaScript/ES2015 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 extend the  `ErrorMapper` class:\n\n```js\nimport { ProblemDocument } from 'http-problem-details'\nimport { ErrorMapper } from 'http-problem-details-mapper'\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\nFinally, create an instance of `ExpressMappingStrategy` to hold the mappers and register everything in your `app`.\nNotice that the `HttpProblemResponse` must come last. A global error logger would\nprecede it and forward the error to the `next` function.\n\n```js\nimport { HttpProblemResponse } from 'express-http-problem-details'\nimport { DefaultMappingStrategy, MapperRegistry } from 'http-problem-details-mapper'\n\nconst strategy = new DefaultMappingStrategy(\n    new MapperRegistry()\n      .registerMapper(new NotFoundErrorMapper()))\n\n\nconst server = express()\n\nserver.get('/', async (req, res) =\u003e {\n  return res.send(new NotFoundError({type: 'customer', id: '123' }))\n})\nserver.use(function logErrors (err, req, res, next) {\n  console.error(err.stack)\n  next(err)\n})\nserver.use(HttpProblemResponse({strategy}))\n\nserver.listen(3000)\n```\n\nWhen GETting localhost:3000, the result will be like this:\n\n```bash\nHTTP/1.1 404 Not Found\nConnection: keep-alive\nContent-Length: 107\nContent-Type: application/problem+json; charset=utf-8\nDate: Wed, 24 Apr 2019 23:48:27 GMT\nETag: W/\"6b-dSoRnzOA0Ls+QaHyomC8H+uv7GQ\"\nX-Powered-By: Express\n\n{\n    \"status\": 404,\n    \"title\": \"customer with id 123 could not be found.\",\n    \"type\": \"http://tempuri.org/NotFoundError\"\n}\n\n```\n\nWhen just returning a `return res.status(500).send();`, you'll get a response like this:\n\n```bash\nHTTP/1.1 500 Internal Server Error\nConnection: keep-alive\nContent-Length: 67\nContent-Type: application/problem+json; charset=utf-8\nDate: Thu, 25 Apr 2019 00:01:48 GMT\nETag: W/\"43-U3E8vFCP1+XTg1JqRHkrjQWiN60\"\nX-Powered-By: Express\n\n{\n    \"status\": 500,\n    \"title\": \"Internal Server Error\",\n    \"type\": \"about:blank\"\n}\n\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","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpdmlab%2Fexpress-http-problem-details","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpdmlab%2Fexpress-http-problem-details","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpdmlab%2Fexpress-http-problem-details/lists"}