Registry / http-networking / http-error-constructor

http-error-constructor

JSON →
library0.1.0jsnpmunverified

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.

npm install http-error-constructor
INSTALL
IMPORT
SIG · HTTP-ERROR-CONSTRU
H
http-error-constructor
http-networkingjavascriptv0.1.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

HttpError
const HttpError = require('http-error-constructor');
import HttpError from 'http-error-constructor';
This package primarily supports CommonJS modules. Attempting to use ESM `import` syntax will result in errors in environments without CJS interoperability.
HttpError.BadRequest
const HttpError = require('http-error-constructor'); const err = new HttpError.BadRequest('Validation failed');
import { BadRequest } from 'http-error-constructor';
Specific HTTP error constructors like `BadRequest` are static properties of the main `HttpError` object and cannot be destructured directly from the package's top-level export.
HttpError[statusCode]
const HttpError = require('http-error-constructor'); const err = new HttpError[400]('Bad Request');
import { 400 } from 'http-error-constructor';
Similar to named constructors, numeric status code constructors are accessed as properties on the main `HttpError` object, not as named exports.

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);
Debug
Known issues
gotchaThe `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.
fix
If these properties are required in the JSON output, manually create a serializable object: `const serializableError = { name: err.name, statusCode: err.statusCode, message: err.message, ...err };`
affects: >=0.1.0
gotchaThis 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.
fix
Consider using a more actively maintained and modern HTTP error library for new projects, or thoroughly audit this package for security and compatibility if integrating into a current application.
affects: >=0.1.0
breakingThis 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).
fix
Use `const HttpError = require('http-error-constructor');` for importing in CommonJS environments. For ESM, you might need a wrapper or dynamic import if direct usage fails: `const HttpError = await import('http-error-constructor');` (though this might still have CJS compatibility issues for named exports).
affects: >=0.1.0
Errors
Common errors & fixes
ReferenceError: require is not defined
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`).
fix
If 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
Trying to destructure `BadRequest` as a named export (e.g., `import { BadRequest } from 'http-error-constructor';`) or misusing it after a wrong import.
fix
Always 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
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.
fix
Ensure `HttpError` is imported correctly as a CommonJS module: `const HttpError = require('http-error-constructor');`
Upgrade
Version history
0.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
13 hits · last 30 days
node
12
Bingbot
1
Resources
http-error-constructor — npm install http-error-constructor · libregistry