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

errors-express

v2.0.1

Published

Central error handling for Express

Readme

Errors for Express

Easy error handling for Express APIs.

NPM NPM NPM

Installation

Install together with express

npm i express errors-express

[!WARNING] From v2, this plugin is adapted to work with Express v5. For Express v4 compatibility, use v1.

Usage

Import the error handler middleware and place it at the end of the middleware stack. Any error (operational or not) thrown by a controller is handled and sent to the client.

Use the handler with a callback to perform extra actions such as logging. Callback receives the error and the request object.

Use guards to return a custom error response after not found or not allowed methods requests.

import express from 'express';
import errorHandler, { Errors, Guards } from 'errors-express';

const app = express();

app.get('/protected', async (req, res) => {
  throw Errors.Unauthorized('You must first sign in');
});

app.all('*splat', Guards.NotFound());

app.use(errorHandler((error, req) => {
  console.log(`[${req.method} ${req.url}] ${error.message}`);
}));

app.listen(process.env.PORT || 3000);

Errors

The base error class used by the package is HttpError. Send optional error details, useful for providing context to error handling in frontend.

HttpError(statusCode, message, details?)

Details can be a string or an object that contains an error code, message and a free context object

Default errors are provided by the package, just include optional message and details.

import { HttpError, Errors } from 'errors-express';

throw new HttpError(400, 'Invalid request', 'MISSING_PSWD');

throw Errors.NotFound();
throw Errors.Forbidden('You cannot do this');
throw Errors.TooManyRequests('You reached the maximum limit or requests', {
  code: 'REQUEST_LIMIT_REACHED';
  message: 'You reached the maximum limit or requests';
  ctx: {
    maxRequests: 5,
    retryIn: '1min',
  };
});

| Error | statusCode | Default message | | --- | --- | --- | | BadRequest | 400 | The request syntax is invalid | | Unauthorized | 401 | The authentication credentials are invalid | | Forbidden | 403 | You are not allowed to use this resource | | NotFound | 404 | This resource does not exist | | MethodNotAllowed | 405 | This method is not allowed for this resource | | Conflict | 409 | There is a conflict with the current state of the resource | | Unprocessable | 422 | The request is unprocessable | | TooManyRequests | 429 | The maximum number of requests has been exceeded | | InternalServer | 500 | An internal server error occurred |

Guards

Guards automatically return an error if none of the previous handlers are called.

import { Guards } from 'errors-express';

app.get('/resource', ResourceController);

app.use(Guards.MethodNotAllowed());
app.use(Guards.NotFound()),

Only MethodNotAllowed and NotFound are available.