{"id":16226,"library":"standard-http-error","title":"Standard HTTP Error Class","description":"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.","status":"abandoned","version":"2.0.1","language":"javascript","source_language":"en","source_url":"git://github.com/moll/js-standard-http-error","tags":["javascript","error","errors","http","exception"],"install":[{"cmd":"npm install standard-http-error","lang":"bash","label":"npm"},{"cmd":"yarn add standard-http-error","lang":"bash","label":"yarn"},{"cmd":"pnpm add standard-http-error","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Provides the base `StandardError` class from which `HttpError` extends, ensuring proper `name` and `stack` properties.","package":"standard-error","optional":false}],"imports":[{"note":"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.","wrong":"import { HttpError } from 'standard-http-error';","symbol":"HttpError","correct":"import HttpError from 'standard-http-error';"},{"note":"This is the native CommonJS import pattern as shown in the package's documentation.","symbol":"HttpError","correct":"const HttpError = require('standard-http-error');"},{"note":"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.","symbol":"HttpError.NOT_FOUND","correct":"if (err.code === HttpError.NOT_FOUND) { /* ... */ }"}],"quickstart":{"code":"import HttpError from 'standard-http-error';\n\ntry {\n  // Create an HTTP error with a specific code\n  throw new HttpError(404);\n} catch (err) {\n  if (err instanceof HttpError) {\n    console.log(`Caught HttpError: ${err.name} (${err.code}) - ${err.message}`); // Output: HttpError (404) - Not Found\n  }\n}\n\ntry {\n  // Create an HTTP error with a named code and custom message\n  throw new HttpError(HttpError.FORBIDDEN, 'Access to this resource is denied');\n} catch (err) {\n  if (err instanceof HttpError) {\n    console.log(`Caught HttpError: ${err.name} (${err.code}) - ${err.message}`); // Output: HttpError (403) - Access to this resource is denied\n  }\n}\n\ntry {\n  // Create an HTTP error with custom properties\n  throw new HttpError(412, 'Bad CSRF Token', { url: '/api/resource', userId: 'user123' });\n} catch (err) {\n  if (err instanceof HttpError) {\n    console.log(`Caught HttpError with custom props: ${err.code} - ${err.message}. URL: ${err.url}, User: ${err.userId}`);\n  }\n}","lang":"typescript","description":"Demonstrates creating `HttpError` instances with numeric codes, named codes, custom messages, and custom properties, and catching them with `instanceof`."},"warnings":[{"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.","message":"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.","severity":"breaking","affected_versions":">=2.0.0"},{"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 })`.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"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.","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.","error":"SyntaxError: Cannot use import statement outside a module"},{"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';`.","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.","error":"Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported"},{"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.","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.","error":"Property 'status' does not exist on type 'HttpError' (TypeScript error)"}],"ecosystem":"npm"}