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-error-middleware

v0.0.1-1.3

Published

A simple Express middleware to handle and format error messages, improving error management in your applications.

Readme

http-error-middleware

http-error-middleware is a package that simplifies error handling in Express applications. It provides an easy way to generate and manage specific HTTP errors using middleware, making your code clearer and ensuring a consistent structure for error responses.

This package allows you to throw errors with specific HTTP status codes and custom messages, then handle them centrally in your application using a single middleware.

Installation

You can install the package from npm using the following command:

npm install http-error-middleware

Basic Usage

Set Up Middleware in Your Express Application

First, you need to import and use the middleware provided by http-error-middleware in your Express app.

import express from 'express'
// Import the package
import { httpErrorMiddleware } from 'http-error-middleware'

const app = express()

// Define your app routes.
app.get('/', (req, res) => {
  res.status(200).json({ message: 'Hello world' })
})

// Use the middleware to handle errors.
app.use(httpErrorMiddleware())
// Other middleware for handling errors can go here.

// Start the server.
app.listen(3000, () => {
  console.log('Server running')
})

httpErrorMiddleware settings

The middleware offers some configurations to customize the error response, which are optional.

import express from 'express'

import { httpErrorMiddleware } from 'http-error-middleware'

const app = express()
//Other Express app config

app.use(httpErrorMiddleware({
  destructure: false,
  statusCodeOnResponse: false
}))
  • If you want the error details to be placed at the root of the response body, set the "destructure" flag to true.

  • If you want the status code sent to be in the response body, set the "statusCodeOnResponse" flag to true.

The default settings is as follows:

{
  "destructure": false,
  "statusCodeOnResponse": true
}

Throw Errors Where You Need Them

You can throw HTTP errors anywhere in your application using the HttpError class provided by the package. Here's a simple example to throw a 400 Bad Request error.

import { HttpError } from 'http-error-middleware'

if (condition) throw HttpError.badRequest('Email and/or password are wrong')

This code will throw an error that gets handled by the middleware, and the response will look like this:

{
  "message": "Email and/or password are wrong",
  "statusCode": 400 // This property will be removed this if you set the "statusCodeOnResponse" flag to false.
}

Errors with Additional Details

If you need to send more details with the error (e.g., information about which form field is incorrect), you can do it like this:

import { HttpError } from 'http-error-middleware'

if (condition) HttpError.badRequest('Email and/or password are wrong', { fieldName: "Error message", ...moreErrors })

This will generate a response with additional error details:

{
  "message": "Email and/or password are wrong",
  "details": {
    "fieldName": "Error message",
    "fieldName2": "Error message"
  },
  "statusCode": 400 // This property will be removed this if you set the "statusCodeOnResponse" flag to false.
}

If you have the "destructure" flag set, the message will be displayed like this:

{
  "message": "Email and/or password are wrong",
  "fieldName": "Error message",
  "fieldName2": "Error message",
  "statusCode": 400 // This property will be removed this if you set the "statusCodeOnResponse" flag to false.
}

Available HTTP Error Methods

The package provides methods to generate common HTTP errors with specific status codes. Here are the available methods:

Client Errors (4xx)

  • HttpError.badRequest(message: string, details?: object)
    • Throws a 400 Bad Request error with the provided message and optional details.
  • HttpError.unauthorized(message: string, details?: object)
    • Throws a 401 Unauthorized error with the provided message and optional details.
  • HttpError.paymentRequired(message: string, details?: object)
    • Throws a 402 Payment Required error with the provided message and optional details.
  • HttpError.forbidden(message: string, details?: object)
    • Throws a 403 Forbidden error with the provided message and optional details.
  • HttpError.notFound(message: string, details?: object)
    • Throws a 404 Not Found error with the provided message and optional details.
  • HttpError.methodNotAllowed(message: string, details?: object)
    • Throws a 405 Method Not Allowed error with the provided message and optional details.
  • HttpError.notAcceptable(message: string, details?: object)
    • Throws a 406 Not Acceptable error with the provided message and optional details.
  • HttpError.proxyAuthenticationRequired(message: string, details?: object)
    • Throws a 407 Proxy Authentication Required error with the provided message and optional details.
  • HttpError.requestTimeOut(message: string, details?: object)
    • Throws a 408 Request Timeout error with the provided message and optional details.
  • HttpError.conflict(message: string, details?: object)
    • Throws a 409 Conflict error with the provided message and optional details.

Server Errors (5xx)

  • HttpError.internalServerError(message: string, details?: object)
    • Throws a 500 Internal Server Error with the provided message and optional details.
  • HttpError.notImplemented(message: string, details?: object)
    • Throws a 501 Not Implemented error with the provided message and optional details.
  • HttpError.badGateway(message: string, details?: object)
    • Throws a 502 Bad Gateway error with the provided message and optional details.
  • HttpError.serviceUnavailable(message: string, details?: object)
    • Throws a 503 Service Unavailable error with the provided message and optional details.
  • HttpError.gatewayTimeOut(message: string, details?: object)
    • Throws a 504 Gateway Timeout error with the provided message and optional details.

Custom Error

  • HttpError.custom(message: string, statusCode: number, details?: object)
    • Throws a custom error with a specified statusCode (for errors not covered by the predefined methods) and optional details.

Each of these methods generates a response with an appropriate HTTP status code, a message, and optionally, additional details.

Benefits

Consistency: Centralize error handling in a single middleware. Clarity: Easily create and throw HTTP errors with clear and specific messages. Extensibility: Add additional details to errors to provide more context, such as information about specific fields.