Registry / http-networking / http-errors-enhanced

http-errors-enhanced

JSON →
library4.0.2jsnpmunverified

`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.

npm install http-errors-enhanced
INSTALL
IMPORT
SIG · HTTP-ERRORS-ENHANC
H
http-errors-enhanced
http-networkingjavascriptv4.0.2
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
import { HttpError } from 'http-errors-enhanced'
const { HttpError } = require('http-errors-enhanced')
The library is ESM-only since v3.0.0, requiring `import` syntax.
NotFoundError
import { NotFoundError } from 'http-errors-enhanced'
const { NotFoundError } = require('http-errors-enhanced')
Specific HTTP error classes like `NotFoundError` are named exports and follow ESM-only rules.
createError
import { createError } from 'http-errors-enhanced'
const { createError } = require('http-errors-enhanced')
The `createError` factory function is a named export for convenience, adhering to ESM-only.

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`.

import { HttpError, NotFoundError, createError } from 'http-errors-enhanced'; // Demonstrate creating a generic HTTP error with a numeric status code and custom properties const genericError = new HttpError(400, 'Invalid request parameters.', { requestId: 'abc-123', details: 'Missing required field: userId' }); console.log('Generic Error:', genericError.status, genericError.message, genericError.requestId); // Demonstrate creating a specific HTTP error by its class name, with custom message and properties const notFoundError = new NotFoundError('Resource /users/123 not found.', { resource: '/users/123', userId: 'non-existent' }); console.log('Not Found Error:', notFoundError.status, notFoundError.error, notFoundError.resource); // Demonstrate using the createError factory function with a string identifier and properties const badGatewayError = createError('BadGateway', { upstreamService: 'payment-gateway', responseCode: 502, message: 'Failed to connect to payment service.' }); console.log('Bad Gateway Error:', badGatewayError.status, badGatewayError.errorPhrase, badGatewayError.upstreamService); // Example of accessing built-in properties like isClientError if (genericError.isClientError) { console.log('This is a client error and can potentially be exposed to the client.'); } // You can also add a cause to errors (supported since v3.0.0) const originalDbError = new Error('Database connection failed due to network timeout.'); const internalServerError = new HttpError(500, 'An unexpected error occurred processing your request.', { transactionId: 'xyz-456' }, { cause: originalDbError }); console.log('Internal Server Error with cause:', internalServerError.cause?.message);
Debug
Known issues
breakingVersion 4.0.0 of `http-errors-enhanced` dropped support for Node.js 20. The package now requires Node.js version >= 22.21.0.
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`).
affects: >=4.0.0
breakingVersion 3.0.0 of `http-errors-enhanced` dropped support for Node.js 18. This version requires Node.js 20 or higher.
fix
Upgrade your Node.js runtime to version 20 or higher. If targeting v4.x, upgrade to Node.js 22.21.0 or higher.
affects: >=3.0.0 <4.0.0
gotchaWhen 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).
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.
affects: All
gotchaThe `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.
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.
affects: All
Errors
Common errors & fixes
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.
Attempting to import `http-errors-enhanced` using CommonJS `require()` syntax in a Node.js environment.
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')`.
The package 'http-errors-enhanced' requires Node.js version >= 22.21.0. The current Node.js version is v20.x.x.
The installed version of `http-errors-enhanced` (v4.x.x) is not compatible with your current Node.js runtime.
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.
TypeError: HttpError is not a constructor
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.
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.
Upgrade
Version history
4.0.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
http-errors-enhanced — npm install http-errors-enhanced · libregistry