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 🙏

© 2026 – Pkg Stats / Ryan Hefner

cube-error

v1.1.0

Published

Custom errors

Readme

cube-error

This library adds some standardization to errors, and provides an easy way of building new error types.

Installation

npm install cube-error --save

Usage

It is simple to require in the library and throw a standardized error:

var CustomErrors = require("cube-error");

throw new CustomErrors.NotFound("Hello");

The real power, however, comes with matching an error-type:

somethingThatFails(function(error) {
  if(error && error.is(CustomErrors.NotFound)) {
    //do something
    return;
  }
  if(error) {
    //do else
    return;
  }
  //success
});

Supported errors

We currently support these errors:

  • NotFound(message, previousError): Used when a resource could not be located.
  • HttpError(statusCode, message, previousError): Maps common status codes to correct errors. 404 maps to NotFound. Otherwise returns a HttpError with a statusCode field.
  • Conflict(message, previousError): Used when a resource modification conflicts with existing internal state.
  • MissingArgument(message, previousError): Can be used when arguments were expected but not passed.
  • InvalidArgument(invalidArgumentName, message, previousError): Can be used as a custom TypeError to indicate unexpected input-type or value The error has a invalidArgument-field which stores the value of invalidArgumentName
  • Unauthorized(message, previousError): Can be used to indicate that authorization has failed. A message can be included to provide a reason as to what will happen as a consequence.
  • Internal(message, previousError): Used when an error happens, which can't be recognized as any other error type.

Custom should never be instantiated, but is intended to be inherited from.

Building custom errors

You can build a custom error by inheriting from Custom:

var CustomError = require("cube-error").Custom;

function MyError(message, previousError) {
  CustomError.call(this, message, previousError);
  //something custom?
}
util.inherits(MyError, CustomError);

This error will come prepackaged with is(MyError), stack, message, and previousError fields.

TODO

  • Map HttpError to more errors in specific cases. Some ideas are:
    • 400 to BadRequestError
    • 403 to ForbiddenError
    • 500 to InternalServerError
  • Make HttpError alternate classes always have a statusCode anyway (to reduce breakingness of changes when introducing new mappings).
  • Test CustomError extension as a thing in unit tests.
  • Handle the case where CustomError is subclasses wrongly (eg. var MyError = function() {}; util.inherits(MyError, CustomError);), and no this.constructor.name exists.