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

express-exceptions-handler

v1.0.3

Published

Express exceptions handler is a lightweight, highly customizable, and developer-friendly Node.js package designed to simplify and standardize exception handling in your express applications.

Readme

Express Exceptions Handler Middleware

Overview

The Express Exceptions Handler Middleware package simplifies error management in Express.js applications. It provides a library of predefined HTTP exceptions and a centralized error-handling middleware for structured and consistent error responses.

Why Use Express Exceptions Handler Middleware?

  • Predefined Exceptions: Reduce repetitive code by leveraging ready-to-use exception classes.

  • Centralized Error Handling: A single middleware to manage all application errors.

  • Customizable: Extend predefined exceptions or create your own.

  • Standardized Responses: Ensures consistency across your APIs.

Features

  • Comprehensive HTTP Exceptions: Built-in support for common HTTP status codes like 401 Unauthorized, 404 Not Found, etc.

  • Middleware Integration: Plug-and-play middleware for Express.js.

  • Custom Exception Support: Extend functionality with your own exception classes.

  • Structured Error Responses: Returns detailed error objects for better debugging and client interaction.

Installation

Install the package via npm or yarn:

npm install express-exceptions-handler

Getting Started

Example Usage

Here is a basic example of how to integrate the Express Exceptions Handler Middleware into your Express.js application:

Code Example:

const express = require('express');
const { UnauthorizedException, HttpException, errorHandlerMiddleware } = require('express-exceptions-handler');

const app = express();

// Example route
app.get('/', (req, res, next) => {
  try {
    throw new UnauthorizedException('Invalid credentials');
  } catch (error) {
    return next(new HttpException(error?.message, error?.status));
  }
});

// Add error-handling middleware
app.use(errorHandlerMiddleware);

app.listen(3000, () => {
  console.log('Server running on http://localhost:3000');
});

Predefined Exceptions

The package includes various built-in exceptions corresponding to HTTP status codes. Each exception extends the HttpException base class and includes a default status code and message.

Example:

const { NotFoundException, BadRequestException } = require('express-exceptions-handler');

app.get('/example', (req, res, next) => {
  try {
    throw new NotFoundException('Resource not found');
  } catch (error) {
    return next(error);
  }
});

Error-Handling Middleware

The errorHandlerMiddleware is a robust Express.js middleware that catches and processes all thrown exceptions in your application.

Default JSON Response Format:

{
  "data": null,
  "message": "Error message",
  "statusCode": 500,
  "isSuccess": false
}

Explanation of Response Fields:

  • data: Always null for errors.

  • message: The specific error message.

  • statusCode: The HTTP status code of the error.

  • isSuccess: Always false for errors.

Adding Custom Exceptions

To define custom exceptions, extend the HttpException class as follows:

const { HttpException } = require('express-exceptions-handler');

class CustomException extends HttpException {
  constructor(message = 'Custom error occurred', status = 400) {
    super(message, status);
  }
}

app.get('/custom', (req, res, next) => {
  try {
    throw new CustomException('This is a custom error', 422);
  } catch (error) {
    return next(error);
  }
});

Predefined Exceptions List

Here is the list of available predefined exceptions with their corresponding HTTP status codes:

    Exception                              Status Code

BadRequestException                                                      400

UnauthorizedException                                                  401

ForbiddenException                                                        403

NotFoundException                                                          404

MethodNotAllowedException                                          405

NotAcceptableException                                                406

RequestTimeoutException                                              408

ConflictException                                                          409

GoneException                                                                  410

PreconditionFailedException                                      412

PayloadTooLargeException                                            413

UnsupportedMediaTypeException                                  415

ImATeapotException                                                        418

MisdirectedException                                                    421

UnprocessableEntityException                                     422

TooManyRequestsException                                             429

InternalServerErrorException                                     500

NotImplementedException                                               501

BadGatewayException                                                       502

ServiceUnavailableException                                       503

GatewayTimeoutException                                               504

HttpVersionNotSupportedException                             505

Contributing

We welcome contributions! If you find bugs or have feature requests, please open an issue or submit a pull request on the GitHub repository.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Feedback and Support

If you encounter any issues or have suggestions for improvement, feel free to open an issue on GitHub. Thank you for using Express Exceptions Handler Middleware! 🚀# Express Exceptions Handler Middleware