{"id":25774443,"url":"https://github.com/markusand/errors-express","last_synced_at":"2025-07-25T05:33:07.304Z","repository":{"id":167415342,"uuid":"447771789","full_name":"markusand/errors-express","owner":"markusand","description":"Central error handling for Express","archived":false,"fork":false,"pushed_at":"2024-05-31T18:57:47.000Z","size":389,"stargazers_count":1,"open_issues_count":1,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-27T05:47:46.562Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/markusand.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2022-01-13T22:31:10.000Z","updated_at":"2024-05-31T18:57:50.000Z","dependencies_parsed_at":null,"dependency_job_id":"3c7cd4a0-4bb8-4f36-b16f-7a538b714c88","html_url":"https://github.com/markusand/errors-express","commit_stats":null,"previous_names":["markusand/errors-express"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/markusand/errors-express","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/markusand%2Ferrors-express","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/markusand%2Ferrors-express/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/markusand%2Ferrors-express/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/markusand%2Ferrors-express/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/markusand","download_url":"https://codeload.github.com/markusand/errors-express/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/markusand%2Ferrors-express/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266961929,"owners_count":24012988,"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","status":"online","status_checked_at":"2025-07-25T02:00:09.625Z","response_time":70,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2025-02-27T05:29:59.182Z","updated_at":"2025-07-25T05:33:07.253Z","avatar_url":"https://github.com/markusand.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Errors for Express\n\nEasy error handling for Express APIs.\n\n[![NPM](https://img.shields.io/npm/v/errors-express)](https://npmjs.org/package/errors-express)\n[![NPM](https://img.shields.io/bundlephobia/minzip/errors-express)](https://npmjs.org/package/errors-express)\n[![NPM](https://img.shields.io/npm/l/errors-express)](https://npmjs.org/package/errors-express)\n\n## Installation\n\nInstall together with express\n\n```bash\nnpm i express errors-express\n```\n\n## Usage\n\nImport the error handler middleware and place it at the end of the middleware stack. Any error (operational or not) thrown by a controller is handled and sent to the client.\n\nUse the handler with a callback to perform extra actions such as logging. Callback receives the error and the request object.\n\nUse guards to return a custom error response after not found or not allowed methods requests.\n\n```js\nimport express from 'express';\nimport errorHandler, { Errors, Guards } from 'errors-express';\n\nconst app = express();\n\napp.get('/protected', async (req, res) =\u003e {\n  throw Errors.Unauthorized('You must first sign in');\n});\n\napp.all('*', Guards.NotFound());\n\napp.use(errorHandler((error, req) =\u003e {\n  console.log(`[${req.method} ${req.url}] ${error.message}`);\n}));\n\napp.listen(process.env.PORT || 3000);\n```\n\n## Errors\n\nThe base error class used by the package is HttpError. Send optional error details, useful for providing context to error handling in frontend.\n\n`HttpError(statusCode, message, details?)`\n\nDetails can be a string or an object that contains an error code, message and a free context object\n\nDefault errors are provided by the package, just include optional message and details.\n\n```javascript\nimport { HttpError, Errors } from 'errors-express';\n\nthrow new HttpError(400, 'Invalid request', 'MISSING_PSWD');\n\nthrow Errors.NotFound();\nthrow Errors.Forbidden('You cannot do this');\nthrow Errors.TooManyRequests('You reached the maximum limit or requests', {\n  code: 'REQUEST_LIMIT_REACHED';\n  message: 'You reached the maximum limit or requests';\n  ctx: {\n    maxRequests: 5,\n    retryIn: '1min',\n  };\n});\n```\n\n| Error | statusCode | Default message |\n| --- | --- | --- |\n| **BadRequest** | 400 | The request syntax is invalid |\n| **Unauthorized** | 401 | The authentication credentials are invalid |\n| **Forbidden** | 403 | You are not allowed to use this resource |\n| **NotFound** | 404 | This resource does not exist |\n| **MethodNotAllowed** | 405 | This method is not allowed for this resource |\n| **Conflict** | 409 | There is a conflict with the current state of the resource |\n| **Unprocessable** | 422 | The request is unprocessable |\n| **TooManyRequests** | 429 | The maximum number of requests has been exceeded |\n| **InternalServer** | 500 | An internal server error occurred |\n\n## Guards\n\nGuards automatically return an error if none of the previous handlers are called.\n\n```javascript\nimport { Guards } from 'errors-express';\n\napp.get('/resource', ResourceController);\n\napp.use(Guards.MethodNotAllowed());\napp.use(Guards.NotFound()),\n```\n\nOnly **MethodNotAllowed** and **NotFound** are available.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarkusand%2Ferrors-express","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarkusand%2Ferrors-express","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarkusand%2Ferrors-express/lists"}