npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

meaning-error

v2.3.0

Published

Give some meaning to throw errors

Downloads

27

Readme

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