Registry / http-networking / standard-http-error

standard-http-error

JSON →
library2.0.1jsnpmunverified

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.

npm install standard-http-error
INSTALL
IMPORT
SIG · STANDARD-HTTP-ERRO
S
standard-http-error
http-networkingjavascriptv2.0.1
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 'standard-http-error';
import { HttpError } from 'standard-http-error';
The core library is CommonJS-only (v2.x), exporting `HttpError` as a default. TypeScript/ESM interop typically handles this as a default import. Named import will fail.
HttpError
const HttpError = require('standard-http-error');
This is the native CommonJS import pattern as shown in the package's documentation.
HttpError.NOT_FOUND
if (err.code === HttpError.NOT_FOUND) { /* ... */ }
Static properties representing HTTP status codes (e.g., `UNAUTHORIZED`, `FORBIDDEN`) are exposed directly on the `HttpError` class. These are primarily for comparison, not direct import.

Demonstrates creating `HttpError` instances with numeric codes, named codes, custom messages, and custom properties, and catching them with `instanceof`.

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}`); } }
Debug
Known issues
breakingThe `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.
fix
Use the CommonJS `const HttpError = require('standard-http-error');` or the ESM default import `import HttpError from 'standard-http-error';` which Node.js's CJS interop handles.
affects: >=2.0.0
gotchaFor 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.
fix
To serialize these specific properties, explicitly copy them to a new object or use a custom serializer. For example: `JSON.stringify({ code: err.code, message: err.message, status: err.status })`.
affects: >=1.0.0
gotchaThe 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.
fix
When subclassing with modern `class extends`, ensure `super()` is called correctly and consider explicitly setting `this.name = this.constructor.name;` if needed, although the base `standard-error` library aims to handle this.
affects: >=1.0.0
Errors
Common errors & fixes
SyntaxError: Cannot use import statement outside a module
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.
fix
If 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
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.
fix
Ensure 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)
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.
fix
Access `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.
Upgrade
Version history
2.0.1latest on npm
Audit
Dependencies
standard-errorrequiredProvides the base `StandardError` class from which `HttpError` extends, ensuring proper `name` and `stack` properties.
Agent activity
9 hits · last 30 days
node
6
OpenAI (training)
2
Resources
standard-http-error — npm install standard-http-error · libregistry