{"id":21902441,"url":"https://github.com/krolow/meaning-error","last_synced_at":"2025-07-06T19:38:56.219Z","repository":{"id":137549971,"uuid":"43434011","full_name":"krolow/meaning-error","owner":"krolow","description":"Give some meaning for the errors that you throw","archived":false,"fork":false,"pushed_at":"2018-03-02T21:14:51.000Z","size":38,"stargazers_count":5,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-15T20:13:32.182Z","etag":null,"topics":["http","http-status-code","nodejs"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/meaning-error","language":"JavaScript","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/krolow.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,"zenodo":null}},"created_at":"2015-09-30T13:19:47.000Z","updated_at":"2023-03-10T10:15:30.000Z","dependencies_parsed_at":null,"dependency_job_id":"151d51f9-e127-4190-8d19-71df486d3bdb","html_url":"https://github.com/krolow/meaning-error","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/krolow/meaning-error","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krolow%2Fmeaning-error","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krolow%2Fmeaning-error/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krolow%2Fmeaning-error/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krolow%2Fmeaning-error/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/krolow","download_url":"https://codeload.github.com/krolow/meaning-error/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krolow%2Fmeaning-error/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263961715,"owners_count":23536275,"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":["http","http-status-code","nodejs"],"created_at":"2024-11-28T15:18:43.650Z","updated_at":"2025-07-06T19:38:56.212Z","avatar_url":"https://github.com/krolow.png","language":"JavaScript","readme":"# meaning-error\n\n[![Build Status](https://travis-ci.org/krolow/meaning-error.svg?branch=master)](https://travis-ci.org/krolow/meaning-error)\n[![npm version](https://badge.fury.io/js/meaning-error.svg)](http://badge.fury.io/js/meaning-error)\n\nIt collects some common errors in a lib, to enable throw and catch those easily.\n\n## Why?\n\nWhile working with web services, we often need to make some checks, for example in one crud of posts:\n\n- Check ```if``` post exists, ```else``` HTTP 404\n- Check ```if``` post data is valid, ```else``` HTTP 400\n- Check ```if``` user has authorization to access post, ```else``` HTTP 403\n- Check ```if``` user is logged, ```else``` HTTP 401\n- etc...\n\nWe are able to handle those errors directly by HTTP framework in use, for example [express](https://github.com/strongloop/express), but as good practices says, we should focus in our **business logic** instead of our [architecture](http://www.coderspeech.com/videos/talks/architecture-the-lost-years).\n\nHaving this as a **fact**, we should give some **meaning** for our errors, in our **business logic** layer, to enable our **HTTP Frameworks/TCP frameworks/etc..**, translate these errors for the propel protocol in use.\n\nThat's what **meaning-error** aim for. It creates a conjunct of standard errors to enable developers to ```throw``` those errors in their **business logic** given meaning for the operations, instead of, return *inconsistent objects* or *boolean values* to indicate the success or failure of operations, in the other hand it keep the ability to easily handle those errors, by pluging middlewares that *handle/catch* those errors and translate it for the protocol in use *(tcp/http/web socket/etc..)*.\n\n## Usage\n\n**List of errors to throw:**\n\n- [BadRequestError](#BadRequestError)\n- [ConflictError](#ConflictError)\n- [ForbiddenError](#ForbiddenError)\n- [InternalServerError](#InternalServerError)\n- [MethodNotAllowedError](#MethodNotAllowedError)\n- [NotFoundError](#NotFoundError)\n- [UnauthorizationError](#UnauthorizationError)\n- [TimeoutError](#TimeoutError)\n- [MeaningError](#MeaningError)\n\n#### \u003ca name=\"BadRequestError\"\u003e\u003c/a\u003eBadRequestError\n\nTo use when data is not valid;\n\n##### Options\n- ```message```: Error message\n- ```error```: Errors that you to forward for who going to consume BadRequestError\n\n```js\nthrow new BadRequestError(\n  'Data not Valid',\n  [\n    {\n      field: 'name',\n      message: 'Name can not be empty',\n    },\n    {\n      field: 'date',\n      message: 'Date field should be a valid date'\n    }\n  ]\n);\n```\n\n**Accessing Error**\n```js\ntry {\n  throw new BadRequestError('Something weird', {fieldName: 'something', 'message': 'Bad Format'});\n} catch (e) {\n  e.getError(); //access the error object, the second argument with given errors...\n}\n```\n\n#### \u003ca name=\"ConflictError\"\u003e\u003c/a\u003eConflictError\n\nTo use when the request could not be completed due to a conflict with the current state of the target resource;\n\n##### Options\n- ```message```: Error message\n\n```js\nthrow new ConflictError('User is already taken');\n```\n\n#### \u003ca name=\"ForbiddenError\"\u003e\u003c/a\u003eForbiddenError\n\nTo use when *requester* does not have *rights* to access *resource*;\n\n##### Options\n- ```message```: Error message\n\n```js\nthrow new ForbiddenError('You can not access this news');\n```\n\n#### \u003ca name=\"InternalServerError\"\u003e\u003c/a\u003eInternalServerError\n\nTo use when some internal error happens;\n\n##### Options\n- ```message```: Error message\n\n```js\nthrow new InternalServerError('Something weird happens');\n```\n\n#### \u003ca name=\"MethodNotAllowedError\"\u003e\u003c/a\u003eMethodNotAllowedError\n\nTo use when *operation* does not support the *requested* action;\n\n##### Options\n- ```message```: Error message\n\n```js\nthrow new MethodNotAllowedError('We only support PUT');\n```\n\n#### \u003ca name=\"NotFoundError\"\u003e\u003c/a\u003eNotFoundError\n\nTo use when *resource* *requested* does not exists;\n\n##### Options\n- ```message```: Error message\n\n```js\nthrow new NotFoundError('We could not found the article by this given id');\n```\n\n#### \u003ca name=\"UnauthorizationError\"\u003e\u003c/a\u003eUnauthorizationError\n\nTo use when *requester* is not authenticated;\n\n##### Options\n- ```message```: Error message\n\n```js\nthrow new UnauthorizationError('You must login to access');\n```\n\n#### \u003ca name=\"TimeoutError\"\u003e\u003c/a\u003eTimeoutError\n\nTo use when *request* reaches a timeout;\n\n##### Options\n- ```message```: Timeout message\n\n```js\nthrow new TimeoutError('Timeout');\n```\n\n#### \u003ca name=\"MeaningError\"\u003e\u003c/a\u003eMeaningError\n\nTo use by inheritance for custom errors;\n\n##### Options\n- ```message```: Error message\n\n```js\nthrow new MeaningError('New custom error');\n```\n## License\n\nLicensed under \u003ca href=\"http://krolow.mit-license.org/\"\u003eThe MIT License\u003c/a\u003e\nRedistributions of files must retain the above copyright notice.\n\n## Author\n\nVinícius Krolow - krolow[at]gmail.com\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkrolow%2Fmeaning-error","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkrolow%2Fmeaning-error","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkrolow%2Fmeaning-error/lists"}