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 🙏

© 2025 – Pkg Stats / Ryan Hefner

http-exceptions-library

v1.0.1

Published

A set of exceptions classes.

Readme

http exception library

Features

  • Handle common HTTP error scenarios with dedicated exception classes.
  • Generate error responses with customizable error messages and status codes.
  • Improve code readability and maintainability by encapsulating error logic in reusable classes.

Install

npm i http-exception-library

Usage

You can use it along with Express defining a custom error handler to replace the Express' default error handler like that:

import { STATUS_CODES } from 'http';
import { HttpException, HttpStatus } from 'http-exception-library';

export function errorHandler(error, req, res, next) {
  if (error instanceof HttpException) {
    return res.status(error.statusCode).json({
      message: error.message,
      statusCode: error.statusCode,
      error: error.error,
    });
  }

  return res
    .status(HttpStatus.INTERNAL_SERVER_ERROR)
    .send(STATUS_CODES[HttpStatus.INTERNAL_SERVER_ERROR]);
}

Now into your request handlers, you can use the exception classes without a try/catch block.

import { Router } from 'express';
import { BadRequestException } from 'http-exception-library';

import { User } from '../models/user';

const routes = Router();

routes.get('/users/:id', async (req, res) => {
  const user = await User.FindOne({ id: req.params.id });

  if (!user) throw new BadRequestException(undefined, 'USER_NOT_FOUND');

  //...
});

export { routes };

The HttpStatus object

Note that the http-exception-library also imports an object HttpStatus:

import { HttpException, HttpStatus } from 'http-exception-library';

console.log(HttpStatus.OK); // Output: 200
console.log(HttpStatus.NOT_FOUND); // Output: 404

Which exception classes this package provides?

http-exception-library provides a set of exception classes that inherit from the base HttpException. These classes represent many of the most common HTTP exceptions:

  • BadRequestException
  • UnauthorizedException
  • NotFoundException
  • ForbiddenException
  • NotAcceptableException
  • RequestTimeoutException
  • ConflictException
  • GoneException
  • HttpVersionNotSupportedException
  • PayloadTooLargeException
  • UnsupportedMediaTypeException
  • UnprocessableEntityException
  • InternalServerErrorException
  • NotImplementedException
  • ImATeapotException
  • MethodNotAllowedException
  • BadGatewayException
  • ServiceUnavailableException
  • GatewayTimeoutException
  • PreconditionFailedException

HttpStatus Object

The HttpStatus object maps the status names to their corresponding status codes.

  • HttpStatus.CONTINUE: 100
  • HttpStatus.SWITCHING_PROTOCOLS: 101
  • HttpStatus.PROCESSING: 102
  • HttpStatus.OK: 200
  • HttpStatus.CREATED: 201
  • HttpStatus.ACCEPTED: 202
  • HttpStatus.NON_AUTHORITATIVE_INFORMATION: 203
  • HttpStatus.NO_CONTENT: 204
  • HttpStatus.RESET_CONTENT: 205
  • HttpStatus.PARTIAL_CONTENT: 206
  • HttpStatus.MULTI_STATUS: 207
  • HttpStatus.ALREADY_REPORTED: 208
  • HttpStatus.IM_USED: 226
  • HttpStatus.MULTIPLE_CHOICES: 300
  • HttpStatus.MOVED_PERMANENTLY: 301
  • HttpStatus.FOUND: 302
  • HttpStatus.SEE_OTHER: 303
  • HttpStatus.NOT_MODIFIED: 304
  • HttpStatus.USE_PROXY: 305
  • HttpStatus.TEMPORARY_REDIRECT: 307
  • HttpStatus.PERMANENT_REDIRECT: 308
  • HttpStatus.BAD_REQUEST: 400
  • HttpStatus.UNAUTHORIZED: 401
  • HttpStatus.PAYMENT_REQUIRED: 402
  • HttpStatus.FORBIDDEN: 403
  • HttpStatus.NOT_FOUND: 404
  • HttpStatus.METHOD_NOT_ALLOWED: 405
  • HttpStatus.NOT_ACCEPTABLE: 406
  • HttpStatus.PROXY_AUTHENTICATION_REQUIRED: 407
  • HttpStatus.REQUEST_TIMEOUT: 408
  • HttpStatus.CONFLICT: 409
  • HttpStatus.GONE: 410
  • HttpStatus.LENGTH_REQUIRED: 411
  • HttpStatus.PRECONDITION_FAILED: 412
  • HttpStatus.PAYLOAD_TOO_LARGE: 413
  • HttpStatus.URI_TOO_LONG: 414
  • HttpStatus.UNSUPPORTED_MEDIA_TYPE: 415
  • HttpStatus.RANGE_NOT_SATISFIABLE: 416
  • HttpStatus.EXPECTATION_FAILED: 417
  • HttpStatus.MISDIRECTED_REQUEST: 421
  • HttpStatus.UNPROCESSABLE_ENTITY: 422
  • HttpStatus.LOCKED: 423
  • HttpStatus.FAILED_DEPENDENCY: 424
  • HttpStatus.UPGRADE_REQUIRED: 426
  • HttpStatus.PRECONDITION_REQUIRED: 428
  • HttpStatus.TOO_MANY_REQUESTS: 429
  • HttpStatus.REQUEST_HEADER_FIELDS_TOO_LARGE: 431
  • HttpStatus.UNAVAILABLE_FOR_LEGAL_REASONS: 451
  • HttpStatus.INTERNAL_SERVER_ERROR: 500
  • HttpStatus.NOT_IMPLEMENTED: 501
  • HttpStatus.BAD_GATEWAY: 502
  • HttpStatus.SERVICE_UNAVAILABLE: 503
  • HttpStatus.GATEWAY_TIMEOUT: 504
  • HttpStatus.HTTP_VERSION_NOT_SUPPORTED: 505
  • HttpStatus.VARIANT_ALSO_NEGOTIATES: 506
  • HttpStatus.INSUFFICIENT_STORAGE: 507
  • HttpStatus.LOOP_DETECTED: 508
  • HttpStatus.NOT_EXTENDED: 510
  • HttpStatus.NETWORK_AUTHENTICATION_REQUIRED: 511

License

This project is licensed under the MIT MIT License.