{"id":18317927,"url":"https://github.com/idiocc/http-errors","last_synced_at":"2025-06-24T14:33:12.622Z","repository":{"id":57113701,"uuid":"227921695","full_name":"idiocc/http-errors","owner":"idiocc","description":"Creates HTTP Errors For Goa Apps.","archived":false,"fork":false,"pushed_at":"2019-12-14T18:38:10.000Z","size":83,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-06-15T12:59:34.526Z","etag":null,"topics":["errors","goa","http","idio"],"latest_commit_sha":null,"homepage":"https://www.idio.cc","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"agpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/idiocc.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"COPYING","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-12-13T21:02:31.000Z","updated_at":"2019-12-14T18:38:12.000Z","dependencies_parsed_at":"2022-08-22T05:31:08.747Z","dependency_job_id":null,"html_url":"https://github.com/idiocc/http-errors","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":"idiocc/package","purl":"pkg:github/idiocc/http-errors","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/idiocc%2Fhttp-errors","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/idiocc%2Fhttp-errors/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/idiocc%2Fhttp-errors/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/idiocc%2Fhttp-errors/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/idiocc","download_url":"https://codeload.github.com/idiocc/http-errors/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/idiocc%2Fhttp-errors/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261694198,"owners_count":23195530,"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":["errors","goa","http","idio"],"created_at":"2024-11-05T18:07:54.184Z","updated_at":"2025-06-24T14:33:11.881Z","avatar_url":"https://github.com/idiocc.png","language":"JavaScript","readme":"# @goa/http-errors\n\n[![npm version](https://badge.fury.io/js/%40goa%2Fhttp-errors.svg)](https://www.npmjs.com/package/@goa/http-errors)\n\n`@goa/http-errors` is Creates HTTP Errors For Goa Apps.\n\n```sh\nyarn add @goa/http-errors\n```\n\n## Table Of Contents\n\n- [Table Of Contents](#table-of-contents)\n- [API](#api)\n  * [`HttpError`](#type-httperror)\n- [`httpErrors(status: number, message: string, props: string): !Error`](#httperrorsstatus-numbermessage-stringprops-string-error)\n- [new createError.ErrorType([msg]))](#new-createerrorerrortypemsg)\n- [Copyright \u0026 License](#copyright--license)\n\n\u003cp align=\"center\"\u003e\u003ca href=\"#table-of-contents\"\u003e\n  \u003cimg src=\"/.documentary/section-breaks/0.svg?sanitize=true\"\u003e\n\u003c/a\u003e\u003c/p\u003e\n\n## API\n\nThe package is available by importing its default function and named class:\n\n```js\nimport httpErrors, { HTTPError } from '@goa/http-errors'\n```\n\n__\u003ca name=\"type-httperror\"\u003e`HttpError`\u003c/a\u003e__: The error constructor that extends Error.\n\n|      Name      |       Type       |                                                                      Description                                                                       |\n| -------------- | ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ |\n| __status__     | \u003cem\u003enumber\u003c/em\u003e  | The status message.                                                                                                                                    |\n| __statusCode__ | \u003cem\u003estring\u003c/em\u003e  | The status code.                                                                                                                                       |\n| __headers__    | \u003cem\u003e*\u003c/em\u003e       | Can be an object of header names to values to be sent to the client, defaulting to `undefined`. When defined, the key names should all be lower-cased. |\n| __message__    | \u003cem\u003estring\u003c/em\u003e  | The traditional error message, which should be kept short and all single line.                                                                         |\n| __expose__     | \u003cem\u003eboolean\u003c/em\u003e | Whether to expose the error to the client.\u003cbr/\u003eFor client errors the default is `true`, for server errors (status \u0026gt;= 500) is `false`.               |\n\n\u003cp align=\"center\"\u003e\u003ca href=\"#table-of-contents\"\u003e\n  \u003cimg src=\"/.documentary/section-breaks/1.svg?sanitize=true\"\u003e\n\u003c/a\u003e\u003c/p\u003e\n\n## \u003ccode\u003e\u003cins\u003ehttpErrors\u003c/ins\u003e(\u003c/code\u003e\u003csub\u003e\u003cbr/\u003e\u0026nbsp;\u0026nbsp;`status: number,`\u003cbr/\u003e\u0026nbsp;\u0026nbsp;`message: string,`\u003cbr/\u003e\u0026nbsp;\u0026nbsp;`props: string,`\u003cbr/\u003e\u003c/sub\u003e\u003ccode\u003e): \u003ci\u003e!Error\u003c/i\u003e\u003c/code\u003e\nCreate a new error object with the given message msg. The error object inherits from createError.HttpError.\n\n - \u003ckbd\u003e\u003cstrong\u003estatus*\u003c/strong\u003e\u003c/kbd\u003e \u003cem\u003e`number`\u003c/em\u003e: The status code as number.\n - \u003ckbd\u003e\u003cstrong\u003emessage*\u003c/strong\u003e\u003c/kbd\u003e \u003cem\u003e`string`\u003c/em\u003e: The message. By default, will look up in the status code table.\n - \u003ckbd\u003e\u003cstrong\u003eprops*\u003c/strong\u003e\u003c/kbd\u003e \u003cem\u003e`string`\u003c/em\u003e: Additional custom properties to attach to object.\n\n```js\nimport { aqt } from 'rqt'\nimport httpErrors from '@goa/http-errors'\nimport Goa from '@goa/koa'\n\nconst goa = new Goa()\n\ngoa.use(() =\u003e {\n  throw httpErrors(401, 'Please login to view this page.')\n})\ngoa.listen(async function() {\n  const url = `http://localhost:${this.address().port}`\n  const res = await aqt(url)\n  console.log(res)\n  this.close()\n})\n```\n```js\n{ body: 'Please login to view this page.',\n  headers: \n   { 'content-type': 'text/plain; charset=utf-8',\n     'content-length': '31',\n     date: 'Sat, 14 Dec 2019 18:32:50 GMT',\n     connection: 'close' },\n  statusCode: 401,\n  statusMessage: 'Unauthorized' }\n```\n\nAnother example that extends the given error. _Koa_ will automatically set status **404** for errors with `ENOENT` code.\n\n```js\nimport { aqt } from 'rqt'\nimport { readFile } from 'fs'\nimport createError from '@goa/http-errors'\nimport Goa from '@goa/koa'\nimport { join } from 'path'\n\nconst goa = new Goa()\n\ngoa.use(async (ctx) =\u003e {\n  await new Promise((r, j) =\u003e {\n    readFile(join('example', ctx.path), (err) =\u003e {\n      let httpError\n      if (err.code == 'ENOENT') {\n        httpError = createError(404, err, { expose: false })\n      } else {\n        httpError = createError(500, err)\n      }\n      j(httpError)\n    })\n  })\n})\ngoa.listen(async function() {\n  // 404\n  console.log('Request missing file')\n  let url = `http://localhost:${this.address().port}/missing.txt`\n  let res = await aqt(url)\n  console.log(res)\n  // 500\n  console.log('\\nRequest a dir')\n  url = `http://localhost:${this.address().port}/dir`\n  res = await aqt(url)\n  console.log(res)\n  this.close()\n})\n```\n```js\nRequest missing file\n{ body: 'Not Found',\n  headers: \n   { 'content-type': 'text/plain; charset=utf-8',\n     'content-length': '9',\n     date: 'Sat, 14 Dec 2019 18:32:50 GMT',\n     connection: 'close' },\n  statusCode: 404,\n  statusMessage: 'Not Found' }\n\nRequest a dir\n{ body: 'Internal Server Error',\n  headers: \n   { 'content-type': 'text/plain; charset=utf-8',\n     'content-length': '21',\n     date: 'Sat, 14 Dec 2019 18:32:50 GMT',\n     connection: 'close' },\n  statusCode: 500,\n  statusMessage: 'Internal Server Error' }\n```\n\nThe app will write to _stderr_ on internal error:\n\n```\nError: EISDIR: illegal operation on a directory, read\n```\n\n\u003cp align=\"center\"\u003e\u003ca href=\"#table-of-contents\"\u003e\n  \u003cimg src=\"/.documentary/section-breaks/2.svg?sanitize=true\"\u003e\n\u003c/a\u003e\u003c/p\u003e\n\n## new createError.ErrorType([msg]))\n\nA new error could be created from a name or code, like so:\n\n```js\nimport createError from '@goa/http-errors'\n\nconst err = new createError.NotFound()\nconsole.log(err)\n```\n```js\n{ NotFoundError: Not Found\n    at Object.\u003canonymous\u003e (example/constructor.js:3:13)\n    at Module._compile (module.js:653:30)\n    at Module.p._compile (node_modules/documentary/node_modules/alamode/compile/depack.js:49:18)\n    at Module._extensions..js (module.js:664:10)\n    at Object.k.(anonymous function).y._extensions.(anonymous function) [as .js] (node_modules/documentary/node_modules/alamode/compile/depack.js:51:7)\n    at Module.load (module.js:566:32)\n    at tryModuleLoad (module.js:506:12)\n    at Function.Module._load (module.js:498:3)\n    at Module.require (module.js:597:17)\n    at require (internal/module.js:11:18)\n  message: 'Not Found',\n  status: 404,\n  statusCode: 404,\n  expose: true,\n  headers: null,\n  name: 'NotFoundError' }\n```\n\nIt's not possible to import specific errors as they are properties on the exported function, and not exports themselves.\n\n**List of all constructors**\n\n|Status Code|Constructor Name             |\n|-----------|-----------------------------|\n|400        |BadRequest                   |\n|401        |Unauthorized                 |\n|402        |PaymentRequired              |\n|403        |Forbidden                    |\n|404        |NotFound                     |\n|405        |MethodNotAllowed             |\n|406        |NotAcceptable                |\n|407        |ProxyAuthenticationRequired  |\n|408        |RequestTimeout               |\n|409        |Conflict                     |\n|410        |Gone                         |\n|411        |LengthRequired               |\n|412        |PreconditionFailed           |\n|413        |PayloadTooLarge              |\n|414        |URITooLong                   |\n|415        |UnsupportedMediaType         |\n|416        |RangeNotSatisfiable          |\n|417        |ExpectationFailed            |\n|418        |ImATeapot                    |\n|421        |MisdirectedRequest           |\n|422        |UnprocessableEntity          |\n|423        |Locked                       |\n|424        |FailedDependency             |\n|425        |UnorderedCollection          |\n|426        |UpgradeRequired              |\n|428        |PreconditionRequired         |\n|429        |TooManyRequests              |\n|431        |RequestHeaderFieldsTooLarge  |\n|451        |UnavailableForLegalReasons   |\n|500        |InternalServerError          |\n|501        |NotImplemented               |\n|502        |BadGateway                   |\n|503        |ServiceUnavailable           |\n|504        |GatewayTimeout               |\n|505        |HTTPVersionNotSupported      |\n|506        |VariantAlsoNegotiates        |\n|507        |InsufficientStorage          |\n|508        |LoopDetected                 |\n|509        |BandwidthLimitExceeded       |\n|510        |NotExtended                  |\n|511        |NetworkAuthenticationRequired|\n\n## Copyright \u0026 License\n\nGNU Affero General Public License v3.0\n\n[Original work](https://github.com/jshttp/http-errors) by Jonathan Ong and Douglas Christopher Wilson under MIT license found in [COPYING](COPYING).\n\n\u003ctable\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003cimg src=\"https://avatars2.githubusercontent.com/u/40834161?s=100\u0026amp;v=4\" alt=\"idiocc\"\u003e\u003c/td\u003e\n    \u003ctd\u003e© \u003ca href=\"https://www.idio.cc\"\u003eIdio™\u003c/a\u003e 2019\u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\n\u003cp align=\"center\"\u003e\u003ca href=\"#table-of-contents\"\u003e\n  \u003cimg src=\"/.documentary/section-breaks/-1.svg?sanitize=true\"\u003e\n\u003c/a\u003e\u003c/p\u003e","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fidiocc%2Fhttp-errors","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fidiocc%2Fhttp-errors","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fidiocc%2Fhttp-errors/lists"}