HTTP Error Classes
raw JSON →The `httperrors` package provides a collection of JavaScript Error classes corresponding to standard HTTP 4xx and 5xx status codes. It allows developers to create instances of these errors either by their numeric status code (e.g., `httpErrors(404)`) or by their UpperCamelCase name (e.g., `new httpErrors.NotFound('Resource not found')`). Each error instance includes the HTTP status code and exposes its CamelCased name as a boolean property, which simplifies error type checking in conditional statements without relying on `instanceof`. Originally designed for use with web frameworks like Express, it facilitates consistent error handling by associating a `statusCode` property with error objects, enabling middleware to set appropriate HTTP response codes. The package is currently at version 2.3.0, with its last update over seven years ago, indicating that it is no longer actively maintained.
Common errors
error TypeError: httpErrors is not a function ↓
require syntax: const httpErrors = require('httperrors');. This package is not designed for ESM import statements. error Error [ERR_REQUIRE_ESM]: require() of ES Module C:\path\to\node_modules\httperrors\index.js not supported ↓
require() CommonJS modules. Consider using dynamic import('httperrors') or, preferably, switch to a modern HTTP error library that provides native ESM support. Alternatively, if your project can be CommonJS, remove "type": "module" from your package.json. Warnings
breaking This package is abandoned, with its last release over 7 years ago. It is unlikely to receive updates for critical bug fixes, security vulnerabilities, or compatibility with newer Node.js versions or JavaScript features (like ESM). ↓
gotcha The package is CommonJS-only and does not natively support ES module `import` syntax. Attempting to use `import httpErrors from 'httperrors';` will lead to runtime errors in a pure ESM environment. ↓
gotcha There are no official TypeScript type definitions shipped with this package, nor is there an `@types/httperrors` package. ↓
Install
npm install httperrors yarn add httperrors pnpm add httperrors Imports
- httpErrors wrong
import httpErrors from 'httperrors';correctconst httpErrors = require('httperrors'); - httpErrors.NotFound wrong
import { NotFound } from 'httperrors';correctconst { NotFound } = require('httperrors'); const notFoundError = new NotFound('Item not found');
Quickstart
const httpErrors = require('httperrors');
function handleRequest(resourceExists) {
if (!resourceExists) {
// Instantiate by status code
const notFoundByCode = httpErrors(404, 'The requested resource was not found.');
console.log(`Error (by code): ${notFoundByCode.statusCode} - ${notFoundByCode.message} (Is 404? ${notFoundByCode[404]})`);
// Or by name
const notFoundByName = new httpErrors.NotFound('The item you are looking for does not exist.');
console.log(`Error (by name): ${notFoundByName.statusCode} - ${notFoundByName.message} (Is NotFound? ${notFoundByName.NotFound})`);
throw notFoundByName;
}
console.log('Resource found and processed.');
}
try {
handleRequest(false);
} catch (err) {
if (err.NotFound) {
console.error(`Caught NotFound error: ${err.message}. Status: ${err.statusCode}`);
} else if (err.BadGateway) {
console.error(`Caught BadGateway error: ${err.message}`);
} else {
console.error(`Caught unexpected error: ${err.name} - ${err.message}`);
}
}
// Example of creating other errors
const teapotError = new httpErrors.ImATeapot('I am a teapot, indeed.');
console.log(`Custom error: ${teapotError.message} (Status: ${teapotError.statusCode})`);