{"id":25566521,"url":"https://github.com/pdmlab/hapi-http-problem-details","last_synced_at":"2025-08-03T01:04:14.625Z","repository":{"id":34778386,"uuid":"183539566","full_name":"PDMLab/hapi-http-problem-details","owner":"PDMLab","description":"Create HTTP Problem Details (RFC7807) for hapi application errors","archived":false,"fork":false,"pushed_at":"2023-04-30T23:50:44.000Z","size":251,"stargazers_count":3,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-06T15:45:52.757Z","etag":null,"topics":["api","error-handling","errors","hapi","hapi-plugin","hapijs","hypermedia","rest","rfc7807"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/hapi-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":null,"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-26T02:06:59.000Z","updated_at":"2022-02-12T10:27:55.000Z","dependencies_parsed_at":"2024-11-07T03:03:00.400Z","dependency_job_id":null,"html_url":"https://github.com/PDMLab/hapi-http-problem-details","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PDMLab%2Fhapi-http-problem-details","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PDMLab%2Fhapi-http-problem-details/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PDMLab%2Fhapi-http-problem-details/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PDMLab%2Fhapi-http-problem-details/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PDMLab","download_url":"https://codeload.github.com/PDMLab/hapi-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","errors","hapi","hapi-plugin","hapijs","hypermedia","rest","rfc7807"],"created_at":"2025-02-20T22:33:00.960Z","updated_at":"2025-04-12T10:51:31.027Z","avatar_url":"https://github.com/PDMLab.png","language":"TypeScript","readme":"# HTTP Problem Details for hapi\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 for your hapi application.\n\n## Installation\n\n```\nnpm install http-problem-details http-problem-details-mapper hapi-http-problem-details\n```\n\nor\n\n```\nyarn add http-problem-details http-problem-details-mapper hapi-http-problem-details\n```\n\n## Usage\n\n`hapi-http-problem-details` provides a plugin 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### TypeScript\n\nThe typical workflow in TypeScript with `hapi-http-problem-details` is this:\n\nFirst, you implement an Error\n\n```typescript\nclass NotFoundError extends Error {\n  public constructor (options: { type: string, id: string }) {\n    const { type, id } = options\n    super()\n    Error.captureStackTrace(this, this.constructor)\n    this.name = 'NotFoundError'\n    this.message = `${type} with id ${id} could not be found.`\n  }\n}\n```\n\nNext, you implement an `IErrorMapper` (noticed we cheated? Instead of the `ErrorMapper` class, in TypeScript you can use an `interface`):\n\n```typescript\nclass NotFoundErrorMapper implements IErrorMapper {\n  public error: string = NotFoundError.name;\n\n  public mapError (error: Error): ProblemDocument {\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 `DefaultMappingStrategy` and register everything in your `app`.\n\n```typescript\nimport * as Hapi from \"@hapi/hapi\";\nimport { DefaultMappingStrategy } from 'hapi-http-problem-details-mapper'\nimport { HttpProblemDetailsPlugin } from 'hapi-http-problem-details'\n\nconst strategy = new DefaultMappingStrategy(\n    new MapperRegistry()\n        .registerMapper(new NotFoundErrorMapper()));\n\nconst init = async () =\u003e {\n\n  const server = Hapi.server({\n    port: 3000,\n    host: 'localhost'\n  });\n\n  await server.register([{\n    plugin: HttpProblemDetailsPlugin,\n    options: { strategy }\n  }]);\n\n  server.route({\n    method: 'GET',\n    path: '/',\n    handler: (request, h) =\u003e {\n      throw new NotFoundError({type: 'customer', id: '123'})\n    }\n  });\n\n  await server.start();\n  console.log('Server running on %s', server.info.uri);\n};\n\nprocess.on('unhandledRejection', (err) =\u003e {\n\n  console.log(err);\n  process.exit(1);\n});\n\ninit();\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\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 h.response().code(500)`, 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\n\n{\n    \"status\": 500,\n    \"title\": \"Internal Server Error\",\n    \"type\": \"about:blank\"\n}\n\n```\n\n### JavaScript / ES2015\n\nThe typical workflow in JavaScript/ES2015 with `hapi-http-problem-details` 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    this.name = 'NotFoundError'\n    this.message = `${type} with id ${id} could not be found.`\n  }\n}\n```\n\nNext, you extend the  `ErrorMapper` class:\n\n```js\nclass NotFoundErrorMapper extends ErrorMapper {\n  constructor() {\n    this.error = NotFoundError.name;\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 `DefaultMappingStrategy` and register everything in your `app`.\n\n```js\nconst Hapi = require('@hapi/hapi');\nconst MapperRegistry = require('http-problem-details-mapper').MapperRegistry;\nconst DefaultMappingStrategy = require('http-problem-details-mapper').DefaultMappingStrategy;\nconst plugin = require('hapi-http-problem-details').HttpProblemDetailsPlugin;\nconst strategy = new DefaultMappingStrategy(new MapperRegistry());\n\nconst init = async () =\u003e {\n\n  const server = Hapi.server({\n    port: 3000,\n    host: 'localhost'\n  });\n\n  await server.register([{\n    plugin: plugin,\n    options: { strategy }\n  }]);\n\n  server.route({\n    method: 'GET',\n    path: '/',\n    handler: (request, h) =\u003e {\n      throw new NotFoundError({type: 'customer', id: '123'})\n    }\n  });\n\n  await server.start();\n  console.log('Server running on %s', server.info.uri);\n};\n\nprocess.on('unhandledRejection', (err) =\u003e {\n\n  console.log(err);\n  process.exit(1);\n});\n\ninit();\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\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 h.response().code(500)`, 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\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\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%2Fhapi-http-problem-details","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpdmlab%2Fhapi-http-problem-details","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpdmlab%2Fhapi-http-problem-details/lists"}