It collects some common errors in a lib, to enable throw and catch those easily.
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, but as good practices says, we should focus in our business logic instead of our architecture.
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..).
List of errors to throw:
- BadRequestError
- ConflictError
- ForbiddenError
- InternalServerError
- MethodNotAllowedError
- NotFoundError
- UnauthorizationError
- TimeoutError
- MeaningError
To use when data is not valid;
message
: Error messageerror
: Errors that you to forward for who going to consume BadRequestError
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
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;
message
: Error message
throw new ConflictError('User is already taken');
To use when requester does not have rights to access resource;
message
: Error message
throw new ForbiddenError('You can not access this news');
To use when some internal error happens;
message
: Error message
throw new InternalServerError('Something weird happens');
To use when operation does not support the requested action;
message
: Error message
throw new MethodNotAllowedError('We only support PUT');
To use when resource requested does not exists;
message
: Error message
throw new NotFoundError('We could not found the article by this given id');
To use when requester is not authenticated;
message
: Error message
throw new UnauthorizationError('You must login to access');
To use when request reaches a timeout;
message
: Timeout message
throw new TimeoutError('Timeout');
To use by inheritance for custom errors;
message
: Error message
throw new MeaningError('New custom error');
Licensed under The MIT License Redistributions of files must retain the above copyright notice.
Vinícius Krolow - krolow[at]gmail.com