Registry / http-networking / http-custom-errors

http-custom-errors

JSON →
library8.0.0jsnpmunverified

Custom error classes for all HTTP status codes (4xx and 5xx) in Node.js. Current stable version is 8.0.0, released periodically with low churn. Unlike generic error packages, it provides individual constructors for each status code (e.g., NotFoundError, ForbiddenError) and a factory function createHTTPError(code) that returns the correct error class by numeric status. Errors have code and status properties matching the HTTP status. This library is designed for use with Express.js error-handling middleware. It supports CommonJS only and has no runtime dependencies.

npm install http-custom-errors
INSTALL
IMPORT
SIG · HTTP-CUSTOM-ERRORS
H
http-custom-errors
http-networkingjavascriptv8.0.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.

HTTPErrors
const HTTPErrors = require('http-custom-errors')
import HTTPErrors from 'http-custom-errors'
CommonJS only; ESM import will fail.
NotFoundError
const { NotFoundError } = require('http-custom-errors')
const NotFoundError = require('http-custom-errors').NotFoundError
Both destructuring and direct property access work. The example uses the default export.
createHTTPError
const { createHTTPError } = require('http-custom-errors')
const createHTTPError = require('http-custom-errors').createHTTPError
Exported as a method on the default export. Can be destructured or called as HTTPErrors.createHTTPError.

Shows how to throw a 404 error using constructor and how to use createHTTPError factory.

const HTTPErrors = require('http-custom-errors'); try { throw new HTTPErrors.NotFoundError('/missing'); } catch (err) { console.log(err.statusCode); // 404 console.log(err.message); // Not Found console.log(err.code); // 404 } const err = HTTPErrors.createHTTPError(500); console.log(err.name); // InternalServerError console.log(err.statusCode); // 500
Debug
Known issues
gotchaThe library is CommonJS-only; ES module imports will fail at runtime.
fix
Use require() instead of import.
affects: >=0.0.0
gotchaError instances have statusCode and code properties both set to the HTTP status number.
fix
Use err.code or err.statusCode interchangeably.
affects: >=0.0.0
deprecatedcreateHTTPError with invalid/unknown status codes returns a generic Error instead of throwing.
fix
Wrap calls in try/catch or validate the code before calling.
affects: >=5.0.0
Errors
Common errors & fixes
TypeError: HTTPErrors.createHTTPError is not a function
Importing the package with ES module syntax (import) returns an empty object in Node.js CommonJS interop.
fix
Use const HTTPErrors = require('http-custom-errors');
Error: Cannot find module 'http-custom-errors'
Package not installed or located in node_modules.
fix
Run 'npm install http-custom-errors' and ensure the file is in the same project.
HTTPErrors.NotFoundError is not a constructor
Attempting to instantiate an error without the 'new' keyword.
fix
Use new HTTPErrors.NotFoundError(message);
Upgrade
Version history
8.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

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