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

http-custom-errors

v8.0.0

Published

Custom error classes based on HTTP statuses

Downloads

1,726

Readme

http-custom-errors

Build Status Code Climate Coverage Status Dependency Status

NPM

Custom errors based on HTTP status codes -- http://jproulx.github.io/node-http-error

Install

npm install http-custom-errors

Usage

var HTTPErrors = require('http-custom-errors');
var error = HTTPErrors.createHTTPError(500); // Create a "500 Internal Server Error" exception
throw new HTTPErrors.NotFoundError('/missing'); // Throw a "404 Not Found Error" exception

The createHTTPError(code); function export will inherit and return a new error exception, with the additional code and status fields that correspond to the HTTP Server Statuses advertised by node's internal HTTP module.

One practical use would allow you to use a polymorphic error handler with express.js:

app.use(function (err, res, req, next) {
  res.status(err.code || 500);
  // respond with html page
  if (req.accepts('html')) {
    return res.render(err.code || 500, { url: req.url });
  }
  // respond with json
  if (req.accepts('json')) {
    res.send({ 'message' : err.message || 'Server Error' });
    return;
  }
  // default to plain-text. send()
  res.type('txt').send(err.message || 'Server Error');
});

With this middleware you would be able to simply throw the appropriate HTTP status error in the right situation:

function authorize (req, res, next) {
    if (// not authorized logic) {
        return next(new HTTPErrors.ForbiddenError('Please log in'));
    }
    return next();
});
app.get('/sensitive', authorize, function (req, res, next) {
    return res.render('logged_in');
});
app.use(function (req, res, next) {
    throw new HTTPErrors.NotFoundError(req.url);
});

Additionally, each error type is exposed as its own Error constructor:

  • HTTPErrors.BadRequestError(message);
  • HTTPErrors.UnauthorizedError(message);
  • HTTPErrors.PaymentRequiredError(message);
  • HTTPErrors.ForbiddenError(message);
  • HTTPErrors.NotFoundError(message);
  • HTTPErrors.MethodNotAllowedError(message);
  • HTTPErrors.NotAcceptableError(message);
  • HTTPErrors.ProxyAuthenticationRequiredError(message);
  • HTTPErrors.RequestTimeoutError(message);
  • HTTPErrors.ConflictError(message);
  • HTTPErrors.GoneError(message);
  • HTTPErrors.LengthRequiredError(message);
  • HTTPErrors.PreconditionFailedError(message);
  • HTTPErrors.RequestEntityTooLargeError(message);
  • HTTPErrors.RequestURITooLargeError(message);
  • HTTPErrors.UnsupportedMediaTypeError(message);
  • HTTPErrors.RequestedRangeNotSatisfiableError(message);
  • HTTPErrors.ExpectationFailedError(message);
  • HTTPErrors.ImATeapotError(message);
  • HTTPErrors.UnprocessableEntityError(message);
  • HTTPErrors.LockedError(message);
  • HTTPErrors.FailedDependencyError(message);
  • HTTPErrors.UnorderedCollectionError(message);
  • HTTPErrors.UpgradeRequiredError(message);
  • HTTPErrors.PreconditionRequiredError(message);
  • HTTPErrors.TooManyRequestsError(message);
  • HTTPErrors.RequestHeaderFieldsTooLargeError(message);
  • HTTPErrors.InternalServerError(message);
  • HTTPErrors.NotImplementedError(message);
  • HTTPErrors.BadGatewayError(message);
  • HTTPErrors.ServiceUnavailableError(message);
  • HTTPErrors.GatewayTimeoutError(message);
  • HTTPErrors.HTTPVersionNotSupportedError(message);
  • HTTPErrors.VariantAlsoNegotiatesError(message);
  • HTTPErrors.InsufficientStorageError(message);
  • HTTPErrors.BandwidthLimitExceededError(message);
  • HTTPErrors.NotExtendedError(message);
  • HTTPErrors.NetworkAuthenticationRequiredError(message);

Notes

Care is taken to preserve the built-in error handling behavior as much as possible, with support for checking instanceof and typeof, as well as making sure the error constructor behaves the same whether it is called with the new operator or not.

In other words, you shouldn't have to worry about these errors affecting your syntax or existing code. Simply drop in place for any existing errors you're throwing and it should work just the same.