{"id":11054,"library":"http-errors-enhanced","title":"HTTP Errors Enhanced","description":"`http-errors-enhanced` is a JavaScript and TypeScript library for creating standardized HTTP error objects with additional properties. It extends the native `Error` class, providing a base `HttpError` class, specific error classes (e.g., `NotFoundError`), and a `createError` factory function. Currently stable at version 4.0.2, the library typically sees patch and minor updates for bug fixes and dependency upgrades, with major versions primarily dropping support for older Node.js runtimes. Key differentiators include its explicit support for attaching arbitrary additional properties to errors, automatic HTTP status code descriptions, `Error.cause` support since v3, and an `expose` property for controlling client visibility. It is designed to be framework-agnostic, allowing for consistent error handling across different environments. The library is ESM-only and ships with TypeScript types.","status":"active","version":"4.0.2","language":"javascript","source_language":"en","source_url":"https://github.com/ShogunPanda/http-errors-enhanced","tags":["javascript","http","errors","enhanced","http-errors","typescript"],"install":[{"cmd":"npm install http-errors-enhanced","lang":"bash","label":"npm"},{"cmd":"yarn add http-errors-enhanced","lang":"bash","label":"yarn"},{"cmd":"pnpm add http-errors-enhanced","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The library is ESM-only since v3.0.0, requiring `import` syntax.","wrong":"const { HttpError } = require('http-errors-enhanced')","symbol":"HttpError","correct":"import { HttpError } from 'http-errors-enhanced'"},{"note":"Specific HTTP error classes like `NotFoundError` are named exports and follow ESM-only rules.","wrong":"const { NotFoundError } = require('http-errors-enhanced')","symbol":"NotFoundError","correct":"import { NotFoundError } from 'http-errors-enhanced'"},{"note":"The `createError` factory function is a named export for convenience, adhering to ESM-only.","wrong":"const { createError } = require('http-errors-enhanced')","symbol":"createError","correct":"import { createError } from 'http-errors-enhanced'"}],"quickstart":{"code":"import { HttpError, NotFoundError, createError } from 'http-errors-enhanced';\n\n// Demonstrate creating a generic HTTP error with a numeric status code and custom properties\nconst genericError = new HttpError(400, 'Invalid request parameters.', {\n  requestId: 'abc-123',\n  details: 'Missing required field: userId'\n});\nconsole.log('Generic Error:', genericError.status, genericError.message, genericError.requestId);\n\n// Demonstrate creating a specific HTTP error by its class name, with custom message and properties\nconst notFoundError = new NotFoundError('Resource /users/123 not found.', {\n  resource: '/users/123',\n  userId: 'non-existent'\n});\nconsole.log('Not Found Error:', notFoundError.status, notFoundError.error, notFoundError.resource);\n\n// Demonstrate using the createError factory function with a string identifier and properties\nconst badGatewayError = createError('BadGateway', {\n  upstreamService: 'payment-gateway',\n  responseCode: 502,\n  message: 'Failed to connect to payment service.'\n});\nconsole.log('Bad Gateway Error:', badGatewayError.status, badGatewayError.errorPhrase, badGatewayError.upstreamService);\n\n// Example of accessing built-in properties like isClientError\nif (genericError.isClientError) {\n  console.log('This is a client error and can potentially be exposed to the client.');\n}\n\n// You can also add a cause to errors (supported since v3.0.0)\nconst originalDbError = new Error('Database connection failed due to network timeout.');\nconst internalServerError = new HttpError(500, 'An unexpected error occurred processing your request.', {\n  transactionId: 'xyz-456'\n}, { cause: originalDbError });\nconsole.log('Internal Server Error with cause:', internalServerError.cause?.message);","lang":"typescript","description":"This quickstart demonstrates how to create various HTTP errors using the `HttpError` class, specific error classes like `NotFoundError`, and the `createError` factory function, showcasing custom properties and error chaining with `Error.cause`."},"warnings":[{"fix":"Upgrade your Node.js runtime to version 22.21.0 or higher. Consider using a version manager like `nvm` (`nvm install 22 && nvm use 22`).","message":"Version 4.0.0 of `http-errors-enhanced` dropped support for Node.js 20. The package now requires Node.js version >= 22.21.0.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Upgrade your Node.js runtime to version 20 or higher. If targeting v4.x, upgrade to Node.js 22.21.0 or higher.","message":"Version 3.0.0 of `http-errors-enhanced` dropped support for Node.js 18. This version requires Node.js 20 or higher.","severity":"breaking","affected_versions":">=3.0.0 <4.0.0"},{"fix":"Always use valid HTTP status codes (400-599) or the predefined string identifiers (e.g., 'NotFound', 'BadRequest') to ensure the correct error status is set.","message":"When constructing an `HttpError` or using `createError`, invalid HTTP status codes (outside 400-599) or unrecognized string identifiers will default the error's status to 500 (Internal Server Error).","severity":"gotcha","affected_versions":"All"},{"fix":"If you need to customize reserved properties, some might be settable directly, but generally, use different property names for custom data to avoid conflicts.","message":"The `properties` object passed to error constructors or `createError` will ignore attempts to overwrite certain reserved error properties like `code`, `message`, `stack`, `expose`, and `headers` if they already exist on the error object. Only non-reserved or new properties will be added.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Convert your module to an ES Module (by setting `\"type\": \"module\"` in `package.json` or using `.mjs` extension) and use `import { ... } from 'http-errors-enhanced';` or use a dynamic import `import('http-errors-enhanced')`.","cause":"Attempting to import `http-errors-enhanced` using CommonJS `require()` syntax in a Node.js environment.","error":"Error [ERR_REQUIRE_ESM]: require() of ES Module .../node_modules/http-errors-enhanced/dist/index.js from ... not supported. Instead change the require of index.js to a dynamic import() which is available in all CommonJS modules."},{"fix":"Upgrade your Node.js runtime to version 22.21.0 or higher. Use `nvm install 22 && nvm use 22` to switch Node.js versions if you have `nvm` installed.","cause":"The installed version of `http-errors-enhanced` (v4.x.x) is not compatible with your current Node.js runtime.","error":"The package 'http-errors-enhanced' requires Node.js version >= 22.21.0. The current Node.js version is v20.x.x."},{"fix":"Ensure you are using the `new` keyword when instantiating `HttpError` (e.g., `new HttpError(...)`) or use the `createError` factory function (e.g., `createError(...)`). Verify your `import` statement is correct.","cause":"This error typically occurs when trying to call `HttpError` as a function or without the `new` keyword, or if the import path is incorrect/module is not properly loaded.","error":"TypeError: HttpError is not a constructor"}],"ecosystem":"npm"}