Standard HTTP Error Class
The `standard-http-error` package provides a minimalist, extensible JavaScript error class (`HttpError`) specifically designed for representing HTTP status codes. It allows for easy detection of HTTP-related errors using `instanceof` checks within error handling middleware. Currently at version 2.0.1, the core library has not seen updates since 2017, suggesting it is effectively abandoned by its original author, though type definitions are community-maintained. It follows semantic versioning for its major versions. Its key differentiators include its small footprint, direct alignment with standard HTTP status codes (supporting both numeric codes and descriptive names like "NOT_FOUND"), and features for proper error serialization. It offers compatibility with frameworks like Express and Koa by providing non-enumerable aliases for `code` and `message` as `status`, `statusCode`, and `statusMessage` for consistent error object structures.
Common errors
-
SyntaxError: Cannot use import statement outside a module
cause Attempting to use `import` syntax for `standard-http-error` in a Node.js environment configured as CommonJS, or using an incorrect named import in an ESM context.fixIf in a CommonJS file, use `const HttpError = require('standard-http-error');`. If in an ES module, use `import HttpError from 'standard-http-error';` to leverage Node.js's CJS-ESM interop. -
Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported
cause Trying to `require()` a package that is ESM-only from a CommonJS context. While `standard-http-error` is CJS, this error can appear if a build system incorrectly tries to apply ESM logic to it.fixEnsure your module resolution and bundling configuration correctly identifies `standard-http-error` as a CommonJS module. If you are consuming it in an ESM project, use `import HttpError from 'standard-http-error';`. -
Property 'status' does not exist on type 'HttpError' (TypeScript error)
cause While `HttpError` instances have `status`, `statusCode`, and `statusMessage` aliases, the TypeScript definitions might not expose these directly as enumerable properties, or the context implies a plain `Error` type.fixAccess `err.code` and `err.message` directly as they are the primary enumerable properties. If you specifically need the aliases for downstream compatibility, use `(err as any).status` or ensure your type definition explicitly includes these non-enumerable properties.
Warnings
- breaking The `standard-http-error` package (v2.x) is distributed as CommonJS. Directly using ESM `import { HttpError } from 'standard-http-error'` in a pure ES module environment without proper Node.js CJS interop or a bundler might lead to `SyntaxError` or `ERR_REQUIRE_ESM` errors.
- gotcha For compatibility with some frameworks, `HttpError` sets `status`, `statusCode`, and `statusMessage` as non-enumerable aliases of `code` and `message`. These properties will not appear when the error object is stringified (e.g., via `JSON.stringify`), which can be unexpected for serialization.
- gotcha The official documentation demonstrates subclassing `HttpError` using pre-ES6 `Object.create(HttpError.prototype, { ... })`. While still functional, developers using modern TypeScript or ES6+ `class extends` syntax might encounter subtle differences in `name` and `stack` behavior if not careful, compared to the library's internal handling.
Install
-
npm install standard-http-error -
yarn add standard-http-error -
pnpm add standard-http-error
Imports
- HttpError
import { HttpError } from 'standard-http-error';import HttpError from 'standard-http-error';
- HttpError
const HttpError = require('standard-http-error'); - HttpError.NOT_FOUND
if (err.code === HttpError.NOT_FOUND) { /* ... */ }
Quickstart
import HttpError from 'standard-http-error';
try {
// Create an HTTP error with a specific code
throw new HttpError(404);
} catch (err) {
if (err instanceof HttpError) {
console.log(`Caught HttpError: ${err.name} (${err.code}) - ${err.message}`); // Output: HttpError (404) - Not Found
}
}
try {
// Create an HTTP error with a named code and custom message
throw new HttpError(HttpError.FORBIDDEN, 'Access to this resource is denied');
} catch (err) {
if (err instanceof HttpError) {
console.log(`Caught HttpError: ${err.name} (${err.code}) - ${err.message}`); // Output: HttpError (403) - Access to this resource is denied
}
}
try {
// Create an HTTP error with custom properties
throw new HttpError(412, 'Bad CSRF Token', { url: '/api/resource', userId: 'user123' });
} catch (err) {
if (err instanceof HttpError) {
console.log(`Caught HttpError with custom props: ${err.code} - ${err.message}. URL: ${err.url}, User: ${err.userId}`);
}
}