err-http
The `err-http` package provides a collection of error constructors specifically designed for common HTTP status codes, simplifying error management in web applications. It allows developers to easily create custom error types that inherit from the standard `Error` object and automatically carry an associated HTTP status code, such as `BadRequestError` (400) or a custom `TeapotError` (418). Currently stable at version 1.2.2, the library has not seen significant updates or new major versions since 2014, indicating an abandoned state. Its core value lies in its straightforward approach to mapping application errors to HTTP semantics, enabling clearer API responses. However, its CommonJS-only nature and lack of active maintenance pose challenges for modern JavaScript development practices, particularly within ESM-first projects or those relying on TypeScript for type safety. This package serves as a foundational example of HTTP error handling but is largely superseded by more actively maintained alternatives.
Common errors
-
ReferenceError: require is not defined
cause Attempting to use `require()` in an ECMAScript Module (ESM) file, where `require` is not globally available.fixIf within an ESM file, you must use `import` statements or explicitly create a `require` function for CJS interop: `import { createRequire } from 'module'; const require = createRequire(import.meta.url); const MyError = require('err-http/myerror');` -
TypeError: (0, _errHttp.default) is not a function
cause Incorrect default import syntax or incorrect assumption about the module's default export when transpiling or using a non-standard module loader. The main `err-http` module exports a function directly, not an object with a default property.fixIn CommonJS, use `const createError = require('err-http');`. In ESM with CJS interop, you might need `import createError from 'err-http';` or `import * as ErrHttp from 'err-http'; const createError = ErrHttp;` if transpilers wrap it, but it's fundamentally a CommonJS module with a direct function export.
Warnings
- breaking The `err-http` package has not been updated since 2014, making it abandoned. This implies no new features, bug fixes, or security patches will be released. Modern JavaScript projects should consider more actively maintained alternatives for HTTP error handling.
- gotcha This library is exclusively CommonJS (`require`). Direct ESM `import` statements (e.g., `import { BadRequestError } from 'err-http/badrequest';`) will fail in pure ESM environments or result in unexpected behavior when transpiled, as it does not provide explicit ESM exports.
- gotcha The library does not provide TypeScript type definitions. Projects using TypeScript will lack type safety when interacting with `err-http` constructors and custom errors, requiring manual type declarations or `@ts-ignore` directives.
Install
-
npm install err-http -
yarn add err-http -
pnpm add err-http
Imports
- BadRequestError
import { BadRequestError } from 'err-http/badrequest';const BadRequestError = require('err-http/badrequest'); - UnauthorizedError
import UnauthorizedError from 'err-http/unauthorized';
const UnauthorizedError = require('err-http/unauthorized'); - err-http factory
import createError from 'err-http';
const createError = require('err-http');
Quickstart
const express = require('express');
const app = express();
const port = 3000;
// Import specific error constructor for 400 Bad Request
const BadRequestError = require('err-http/badrequest');
// Import the factory to create a custom error (e.g., HTTP 418 I'm a Teapot)
const CustomTeapotError = require('err-http')('TeapotError', 'I am a teapot', 418);
app.get('/api/resource', (req, res, next) => {
// Simulate an invalid request if a required query parameter is missing
if (!req.query.id) {
return next(new BadRequestError('Missing required query parameter: id.'));
}
res.json({ message: `Resource with ID ${req.query.id} found.` });
});
app.get('/api/coffee', (req, res, next) => {
// Simulate a custom teapot error for a coffee request
next(new CustomTeapotError('Sorry, this server is a teapot and cannot brew coffee.'));
});
// Generic error handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);
// `err-http` errors have a `status` property
const status = err.status || 500;
const message = err.message || 'Internal Server Error';
res.status(status).json({ error: message, code: status });
});
app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
console.log('Test with: http://localhost:3000/api/resource'); // Should return 400
console.log('Test with: http://localhost:3000/api/resource?id=123'); // Should return 200
console.log('Test with: http://localhost:3000/api/coffee'); // Should return 418
});