HTTP Error Constructor
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
-
ReferenceError: require is not defined
cause Attempting to use `require()` in an ES Module context without proper setup, or when the file is treated as an ES Module (e.g., `"type": "module"` in `package.json`).fixIf your project is ESM, ensure CJS interoperability is configured, or refactor to use dynamic `import()` for this package. If your file is treated as ESM, rename it to `.cjs` or ensure the containing `package.json` does not have `"type": "module"` if you intend to use CJS. -
TypeError: HttpError.BadRequest is not a constructor
cause Trying to destructure `BadRequest` as a named export (e.g., `import { BadRequest } from 'http-error-constructor';`) or misusing it after a wrong import.fixAlways access specific error constructors as static properties of the main `HttpError` object: `const HttpError = require('http-error-constructor'); const err = new HttpError.BadRequest();` -
TypeError: HttpError is not a constructor
cause This usually happens if `HttpError` was imported incorrectly, e.g., using `import { HttpError } from 'http-error-constructor'` when the package provides a default export or is CommonJS-only.fixEnsure `HttpError` is imported correctly as a CommonJS module: `const HttpError = require('http-error-constructor');`
Warnings
- gotcha The `name`, `statusCode`, and `status` properties of `HttpError` instances are non-enumerable. This means they will not be included in the output when calling `JSON.stringify()` on an error instance.
- gotcha This package is a very old project (v0.1.0, requiring Node.js >= 4.0.0 from 2015). It is likely unmaintained, may lack modern features, and could have unpatched security vulnerabilities or incompatibilities with very recent Node.js versions.
- breaking This package is written in CommonJS (CJS) module format. Direct `import` statements (ESM) will not work without proper configuration (e.g., `"type": "module"` in `package.json` with `require` interoperability, or transpilation).
Install
-
npm install http-error-constructor -
yarn add http-error-constructor -
pnpm add http-error-constructor
Imports
- HttpError
import HttpError from 'http-error-constructor';
const HttpError = require('http-error-constructor'); - HttpError.BadRequest
import { BadRequest } from 'http-error-constructor';const HttpError = require('http-error-constructor'); const err = new HttpError.BadRequest('Validation failed'); - HttpError[statusCode]
import { 400 } from 'http-error-constructor';const HttpError = require('http-error-constructor'); const err = new HttpError[400]('Bad Request');
Quickstart
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);