{"id":29608613,"url":"https://github.com/tlaanemaa/http-error-classes","last_synced_at":"2025-07-20T20:00:19.163Z","repository":{"id":57268133,"uuid":"404832749","full_name":"tlaanemaa/http-error-classes","owner":"tlaanemaa","description":"HTTP error classes for JavaScript or TypeScript applications","archived":false,"fork":false,"pushed_at":"2021-09-17T20:34:08.000Z","size":127,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-10T00:44:46.821Z","etag":null,"topics":["error","express","http","http-errors","nodejs"],"latest_commit_sha":null,"homepage":"","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/tlaanemaa.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}},"created_at":"2021-09-09T18:39:59.000Z","updated_at":"2024-05-18T15:08:43.000Z","dependencies_parsed_at":"2022-09-02T05:41:06.593Z","dependency_job_id":null,"html_url":"https://github.com/tlaanemaa/http-error-classes","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/tlaanemaa/http-error-classes","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tlaanemaa%2Fhttp-error-classes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tlaanemaa%2Fhttp-error-classes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tlaanemaa%2Fhttp-error-classes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tlaanemaa%2Fhttp-error-classes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tlaanemaa","download_url":"https://codeload.github.com/tlaanemaa/http-error-classes/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tlaanemaa%2Fhttp-error-classes/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266189575,"owners_count":23890056,"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","express","http","http-errors","nodejs"],"created_at":"2025-07-20T20:00:14.518Z","updated_at":"2025-07-20T20:00:19.135Z","avatar_url":"https://github.com/tlaanemaa.png","language":"TypeScript","readme":"# HTTP error classes\n\n[![Node.js CI](https://github.com/tlaanemaa/http-error-classes/actions/workflows/node.js.yml/badge.svg?branch=main)](https://github.com/tlaanemaa/http-error-classes/actions/workflows/node.js.yml)\n\nThis package provides error classes for every HTTP 4xx and 5xx error code. This makes it easy to throw HTTP errors in your application without having to build the classes yourself for that, just a small win.\n\nYou can also check if an error is a HttpError by checking if it's an instance of the `HttpError` class from this package, all errors here extend that class.\n\nThe error classes allow you to provide a message and a context object, status codes are built into the errors. You can also just use the `HttpError` class to provide your own status code.\n\n## Example\n\n### Throwing an error with some context\n\nHere we have a function to calculate the Fibonacci value at `n` steps. If the `n` we receive is not a number then we'll throw a `BadRequestError` and give the `n` we received as context.\n\n```js\nconst { BadRequestError } = require(\"http-error-classes\");\n\nconst fibonacci = (n) =\u003e {\n  if (typeof n !== \"number\") {\n    throw new BadRequestError(\"n must be a number!\", { n });\n  }\n  // Calculate fibonacci\n};\n```\n\n### Filtering for HttpErrors\n\nHere we have an express error middleware that handles how errors get returned to the client. We don't want to expose errors that are not HttpErrors, so we mask them with a generic `InternalServerError`. It's also important to explicitly map the error to the returned object because HttpErrors, like regular Errors, contain the stack trace which should not be exposed.\n\n```js\nconst { HttpError, InternalServerError } = require(\"http-error-classes\");\n\nconst errorHandlerMiddleware = (err, req, res, next) =\u003e {\n  const error = err instanceof HttpError ? err : new InternalServerError();\n  res.status(error.status).send({\n    message: error.message,\n    context: error.context,\n  });\n};\n```\n\n## API\n\n### `HttpError`\n\n```ts\ndeclare class HttpError\u003cContextType\u003e extends Error {\n  readonly status: number;\n  readonly message: string;\n  readonly context?: ContextType | undefined;\n  readonly name: string;\n  readonly statusCode: number;\n\n  constructor(\n    status: number,\n    message: string,\n    context?: ContextType | undefined = undefined\n  );\n}\n```\n\n### Any other error, for example `BadRequestError`\n\n```ts\ndeclare class BadRequestError\u003cContextType\u003e extends HttpError\u003cContextType\u003e {\n  readonly message: string;\n  readonly context?: ContextType | undefined;\n  readonly name = \"BadRequestError\";\n\n  constructor(\n    message?: string = \"Bad Request\",\n    context?: ContextType | undefined = undefined\n  );\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftlaanemaa%2Fhttp-error-classes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftlaanemaa%2Fhttp-error-classes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftlaanemaa%2Fhttp-error-classes/lists"}