meaning-error

2.3.0 • Public • Published

meaning-error

Build Status npm version

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, 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..).

Usage

List of errors to throw:

BadRequestError

To use when data is not valid;

Options
  • message: Error message
  • error: 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...
}

ConflictError

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
throw new ConflictError('User is already taken');

ForbiddenError

To use when requester does not have rights to access resource;

Options
  • message: Error message
throw new ForbiddenError('You can not access this news');

InternalServerError

To use when some internal error happens;

Options
  • message: Error message
throw new InternalServerError('Something weird happens');

MethodNotAllowedError

To use when operation does not support the requested action;

Options
  • message: Error message
throw new MethodNotAllowedError('We only support PUT');

NotFoundError

To use when resource requested does not exists;

Options
  • message: Error message
throw new NotFoundError('We could not found the article by this given id');

UnauthorizationError

To use when requester is not authenticated;

Options
  • message: Error message
throw new UnauthorizationError('You must login to access');

TimeoutError

To use when request reaches a timeout;

Options
  • message: Timeout message
throw new TimeoutError('Timeout');

MeaningError

To use by inheritance for custom errors;

Options
  • message: Error message
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

Package Sidebar

Install

npm i meaning-error

Weekly Downloads

5

Version

2.3.0

License

MIT

Unpacked Size

147 kB

Total Files

19

Last publish

Collaborators

  • krolow