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-constructorVerified import paths — ran on the pinned version, not inferred.
Demonstrates instantiating HTTP errors with status codes, custom messages, additional properties, and using specific named constructors, highlighting how properties are handled during serialization.
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 };`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.
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).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.
Always access specific error constructors as static properties of the main `HttpError` object: `const HttpError = require('http-error-constructor'); const err = new HttpError.BadRequest();`Ensure `HttpError` is imported correctly as a CommonJS module: `const HttpError = require('http-error-constructor');`No dependency data recorded yet.