Coder Social home page Coder Social logo

meaning-error's Introduction

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

meaning-error's People

Contributors

krolow avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.