HTTP Errors Enhanced

4.0.2 · active · verified Sun Apr 19

`http-errors-enhanced` is a JavaScript and TypeScript library for creating standardized HTTP error objects with additional properties. It extends the native `Error` class, providing a base `HttpError` class, specific error classes (e.g., `NotFoundError`), and a `createError` factory function. Currently stable at version 4.0.2, the library typically sees patch and minor updates for bug fixes and dependency upgrades, with major versions primarily dropping support for older Node.js runtimes. Key differentiators include its explicit support for attaching arbitrary additional properties to errors, automatic HTTP status code descriptions, `Error.cause` support since v3, and an `expose` property for controlling client visibility. It is designed to be framework-agnostic, allowing for consistent error handling across different environments. The library is ESM-only and ships with TypeScript types.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create various HTTP errors using the `HttpError` class, specific error classes like `NotFoundError`, and the `createError` factory function, showcasing custom properties and error chaining with `Error.cause`.

import { HttpError, NotFoundError, createError } from 'http-errors-enhanced';

// Demonstrate creating a generic HTTP error with a numeric status code and custom properties
const genericError = new HttpError(400, 'Invalid request parameters.', {
  requestId: 'abc-123',
  details: 'Missing required field: userId'
});
console.log('Generic Error:', genericError.status, genericError.message, genericError.requestId);

// Demonstrate creating a specific HTTP error by its class name, with custom message and properties
const notFoundError = new NotFoundError('Resource /users/123 not found.', {
  resource: '/users/123',
  userId: 'non-existent'
});
console.log('Not Found Error:', notFoundError.status, notFoundError.error, notFoundError.resource);

// Demonstrate using the createError factory function with a string identifier and properties
const badGatewayError = createError('BadGateway', {
  upstreamService: 'payment-gateway',
  responseCode: 502,
  message: 'Failed to connect to payment service.'
});
console.log('Bad Gateway Error:', badGatewayError.status, badGatewayError.errorPhrase, badGatewayError.upstreamService);

// Example of accessing built-in properties like isClientError
if (genericError.isClientError) {
  console.log('This is a client error and can potentially be exposed to the client.');
}

// You can also add a cause to errors (supported since v3.0.0)
const originalDbError = new Error('Database connection failed due to network timeout.');
const internalServerError = new HttpError(500, 'An unexpected error occurred processing your request.', {
  transactionId: 'xyz-456'
}, { cause: originalDbError });
console.log('Internal Server Error with cause:', internalServerError.cause?.message);

view raw JSON →