{"id":17255,"library":"http-error-constructor","title":"HTTP Error Constructor","description":"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.","status":"abandoned","version":"0.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/Jokero/http-error-constructor","tags":["javascript","http","error","constructor"],"install":[{"cmd":"npm install http-error-constructor","lang":"bash","label":"npm"},{"cmd":"yarn add http-error-constructor","lang":"bash","label":"yarn"},{"cmd":"pnpm add http-error-constructor","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package primarily supports CommonJS modules. Attempting to use ESM `import` syntax will result in errors in environments without CJS interoperability.","wrong":"import HttpError from 'http-error-constructor';","symbol":"HttpError","correct":"const HttpError = require('http-error-constructor');"},{"note":"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.","wrong":"import { BadRequest } from 'http-error-constructor';","symbol":"HttpError.BadRequest","correct":"const HttpError = require('http-error-constructor');\nconst err = new HttpError.BadRequest('Validation failed');"},{"note":"Similar to named constructors, numeric status code constructors are accessed as properties on the main `HttpError` object, not as named exports.","wrong":"import { 400 } from 'http-error-constructor';","symbol":"HttpError[statusCode]","correct":"const HttpError = require('http-error-constructor');\nconst err = new HttpError[400]('Bad Request');"}],"quickstart":{"code":"const HttpError = require('http-error-constructor');\n\n// Basic usage with a status code\nconst err1 = new HttpError(404);\nconsole.log(`Error 1: ${err1.name}, Status: ${err1.statusCode}, Message: ${err1.message}`);\n// Expected output: 'Error 1: NotFound, Status: 404, Message: Not Found'\n\n// With a custom message and additional properties\nconst err2 = new HttpError(400, 'Validation Failed', {\n    fields: {\n        email: 'Invalid format'\n    },\n    requestId: 'abc-123'\n});\nconsole.log(`Error 2: ${err2.name}, Status: ${err2.statusCode}, Message: ${err2.message}`);\nconsole.log('JSON Stringify err2:', JSON.stringify(err2));\n// Expected JSON: '{\"message\":\"Validation Failed\",\"fields\":{\"email\":\"Invalid format\"},\"requestId\":\"abc-123\"}'\n\n// Using a specific named constructor\nconst err3 = new HttpError.Unauthorized({ message: 'Authentication required', retryable: true });\nconsole.log(`Error 3: ${err3.name}, Status: ${err3.statusCode}, Message: ${err3.message}`);\nconsole.log('Is err3 an HttpError instance?', err3 instanceof HttpError);","lang":"javascript","description":"Demonstrates instantiating HTTP errors with status codes, custom messages, additional properties, and using specific named constructors, highlighting how properties are handled during serialization."},"warnings":[{"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 };`","message":"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.","severity":"gotcha","affected_versions":">=0.1.0"},{"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.","message":"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.","severity":"gotcha","affected_versions":">=0.1.0"},{"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).","message":"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).","severity":"breaking","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"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.","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`).","error":"ReferenceError: require is not defined"},{"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();`","cause":"Trying to destructure `BadRequest` as a named export (e.g., `import { BadRequest } from 'http-error-constructor';`) or misusing it after a wrong import.","error":"TypeError: HttpError.BadRequest is not a constructor"},{"fix":"Ensure `HttpError` is imported correctly as a CommonJS module: `const HttpError = require('http-error-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.","error":"TypeError: HttpError is not a constructor"}],"ecosystem":"npm","meta_description":null}