https://github.com/krolow/meaning-error
Give some meaning for the errors that you throw
https://github.com/krolow/meaning-error
http http-status-code nodejs
Last synced: 7 months ago
JSON representation
Give some meaning for the errors that you throw
- Host: GitHub
- URL: https://github.com/krolow/meaning-error
- Owner: krolow
- License: mit
- Created: 2015-09-30T13:19:47.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2018-03-02T21:14:51.000Z (almost 8 years ago)
- Last Synced: 2025-04-15T20:13:32.182Z (10 months ago)
- Topics: http, http-status-code, nodejs
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/meaning-error
- Size: 37.1 KB
- Stars: 5
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# meaning-error
[](https://travis-ci.org/krolow/meaning-error)
[](http://badge.fury.io/js/meaning-error)
It collects some common errors in a lib, to enable throw and catch those easily.
## Why?
While working with web services, we often need to make some checks, for example in one crud of posts:
- Check ```if``` post exists, ```else``` HTTP 404
- Check ```if``` post data is valid, ```else``` HTTP 400
- Check ```if``` user has authorization to access post, ```else``` HTTP 403
- Check ```if``` user is logged, ```else``` HTTP 401
- etc...
We 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).
Having 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.
That'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..)*.
## Usage
**List of errors to throw:**
- [BadRequestError](#BadRequestError)
- [ConflictError](#ConflictError)
- [ForbiddenError](#ForbiddenError)
- [InternalServerError](#InternalServerError)
- [MethodNotAllowedError](#MethodNotAllowedError)
- [NotFoundError](#NotFoundError)
- [UnauthorizationError](#UnauthorizationError)
- [TimeoutError](#TimeoutError)
- [MeaningError](#MeaningError)
To use when data is not valid;
##### Options
- ```message```: Error message
- ```error```: Errors that you to forward for who going to consume BadRequestError
```js
throw new BadRequestError(
'Data not Valid',
[
{
field: 'name',
message: 'Name can not be empty',
},
{
field: 'date',
message: 'Date field should be a valid date'
}
]
);
```
**Accessing Error**
```js
try {
throw new BadRequestError('Something weird', {fieldName: 'something', 'message': 'Bad Format'});
} catch (e) {
e.getError(); //access the error object, the second argument with given errors...
}
```
To use when the request could not be completed due to a conflict with the current state of the target resource;
##### Options
- ```message```: Error message
```js
throw new ConflictError('User is already taken');
```
To use when *requester* does not have *rights* to access *resource*;
##### Options
- ```message```: Error message
```js
throw new ForbiddenError('You can not access this news');
```
To use when some internal error happens;
##### Options
- ```message```: Error message
```js
throw new InternalServerError('Something weird happens');
```
To use when *operation* does not support the *requested* action;
##### Options
- ```message```: Error message
```js
throw new MethodNotAllowedError('We only support PUT');
```
To use when *resource* *requested* does not exists;
##### Options
- ```message```: Error message
```js
throw new NotFoundError('We could not found the article by this given id');
```
To use when *requester* is not authenticated;
##### Options
- ```message```: Error message
```js
throw new UnauthorizationError('You must login to access');
```
To use when *request* reaches a timeout;
##### Options
- ```message```: Timeout message
```js
throw new TimeoutError('Timeout');
```
To use by inheritance for custom errors;
##### Options
- ```message```: Error message
```js
throw new MeaningError('New custom error');
```
## License
Licensed under The MIT License
Redistributions of files must retain the above copyright notice.
## Author
Vinícius Krolow - krolow[at]gmail.com