{"id":20935422,"url":"https://github.com/ww-tech/express-rest-error","last_synced_at":"2025-05-13T20:32:44.244Z","repository":{"id":42916303,"uuid":"247588500","full_name":"ww-tech/express-rest-error","owner":"ww-tech","description":"Clean error throwing/handling for Express REST apps","archived":false,"fork":false,"pushed_at":"2023-07-18T21:21:28.000Z","size":1262,"stargazers_count":2,"open_issues_count":16,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-13T16:10:37.657Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/ww-tech.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}},"created_at":"2020-03-16T02:01:35.000Z","updated_at":"2023-09-15T15:14:42.000Z","dependencies_parsed_at":"2023-02-13T09:30:33.441Z","dependency_job_id":null,"html_url":"https://github.com/ww-tech/express-rest-error","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ww-tech%2Fexpress-rest-error","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ww-tech%2Fexpress-rest-error/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ww-tech%2Fexpress-rest-error/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ww-tech%2Fexpress-rest-error/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ww-tech","download_url":"https://codeload.github.com/ww-tech/express-rest-error/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225258918,"owners_count":17445838,"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":[],"created_at":"2024-11-18T22:14:54.379Z","updated_at":"2024-11-18T22:14:54.956Z","avatar_url":"https://github.com/ww-tech.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# express-rest-error\n\nClean error throwing/handling for Express REST apps using the [Service layer pattern](https://en.wikipedia.org/wiki/Service_layer_pattern)\n\n## Install\n\n```\nnpm i -S express-rest-error\n```\n\n## Example\n\n```js\nimport { validationError, errorHandler } from 'express-rest-error'\nimport express from 'express'\n\nconst app = express()\n\nasync function helloService(name) {\n  if (!name) throw validationError('Must specify name.')\n  return `Hello ${name}!`\n}\n\napp.get('/hello', async (req, res, next) =\u003e {\n  try {\n    const result = await helloService(req.query.name)\n    res.json({ result })\n  } catch (err) {\n    next(err)\n  }\n})\n\napp.use(errorHandler({ debug: true }))\n```\n\n## Throwing Errors\n\nYou may throw any of the following types of errors with an optional error `message`.\n\n### `throw validationError( message, details )`\n\n- Appropriate for missing required fields or invalid input.\n- Sets the response status code to `400`.\n\n### `throw authRequired( message, details )`\n\n- Appropriate when token or authentication is missing.\n- Sets the response status code to `401`.\n\n### `throw accessDenied( message, details )`\n\n- Appropriate when the user making the request does not have permission to the resource.\n- Sets the response status code to `403`.\n\n### `throw notFound( message, details )`\n\n- Appropriate when the requested resource does not exist.\n- Sets the response status code to `404`.\n\n\n### `throw customError( message, details, httpStatus )`\n\n- Appropriate for when custom errors are necessary\n\n## Handling Errors\n\nThis error handler will detect which type of error was thrown and set the status code accordingly.\n\nNote: You should define the error handler last, after all other middleware.\n\n```js\napp.use(errorHandler({ debug: true }))\n```\n\n### Error Handler Options\n\n- `debug` {Boolean} - (optional) returns debug output (stack trace, request data)\n- `transform` {Function} - (optional) intercept error before sending to client\n\n\n```js\napp.use(errorHandler({\n  debug: true,\n  transform: ({ err, req, res, responseBody }) =\u003e {\n    res.setHeader('X-Warning', 'Stop sending bad requests.')\n    return responseBody;\n  }\n}))\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fww-tech%2Fexpress-rest-error","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fww-tech%2Fexpress-rest-error","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fww-tech%2Fexpress-rest-error/lists"}