HTTP Error Constructor

0.1.0 · abandoned · verified Wed Apr 22

This `http-error-constructor` package provides a straightforward way to create HTTP-specific error objects in Node.js applications. Currently at version 0.1.0, it appears to be an early-stage or unmaintained library given its low version number and the explicit requirement for Node.js >= 4.0.0, which was released in 2015. The library offers a main `HttpError` constructor that can accept a status code, a message, and additional properties. A key feature is the dynamic creation of specific error constructors, such as `HttpError.BadRequest` or `HttpError[400]`, making error instantiation semantically clear and readable. It automatically assigns the correct HTTP status message based on the provided code and ensures core properties like `name`, `statusCode`, and `status` are non-enumerable, leading to their exclusion from default `JSON.stringify` output, while custom properties are included. The library's release cadence is unknown, but its age indicates it predates widespread ECMAScript Modules (ESM) adoption.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates instantiating HTTP errors with status codes, custom messages, additional properties, and using specific named constructors, highlighting how properties are handled during serialization.

const HttpError = require('http-error-constructor');

// Basic usage with a status code
const err1 = new HttpError(404);
console.log(`Error 1: ${err1.name}, Status: ${err1.statusCode}, Message: ${err1.message}`);
// Expected output: 'Error 1: NotFound, Status: 404, Message: Not Found'

// With a custom message and additional properties
const err2 = new HttpError(400, 'Validation Failed', {
    fields: {
        email: 'Invalid format'
    },
    requestId: 'abc-123'
});
console.log(`Error 2: ${err2.name}, Status: ${err2.statusCode}, Message: ${err2.message}`);
console.log('JSON Stringify err2:', JSON.stringify(err2));
// Expected JSON: '{"message":"Validation Failed","fields":{"email":"Invalid format"},"requestId":"abc-123"}'

// Using a specific named constructor
const err3 = new HttpError.Unauthorized({ message: 'Authentication required', retryable: true });
console.log(`Error 3: ${err3.name}, Status: ${err3.statusCode}, Message: ${err3.message}`);
console.log('Is err3 an HttpError instance?', err3 instanceof HttpError);

view raw JSON →