{"id":17703,"library":"httperr","title":"HTTP Status Code Errors","description":"The `httperr` package provides a comprehensive set of JavaScript Error types, each corresponding to a standard HTTP status code. It is designed to address common issues found in similar libraries, such as incorrect stack trace capture and limited support for status codes. `httperr` version 1.0.0 is stable but appears to be in maintenance mode, with infrequent updates. A key differentiator is its ability to associate arbitrary additional data with an error object during instantiation, allowing for context-rich error handling without losing the semantic meaning of HTTP status codes. This enables developers to separate error handling logic from the final error response generation, enhancing flexibility and clarity, especially when dealing with specific HTTP headers like `Allow` or `Retry-After` for certain error types.","status":"maintenance","version":"1.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/pluma/httperr","tags":["javascript","http","errors","error","request","response","errorclass"],"install":[{"cmd":"npm install httperr","lang":"bash","label":"npm"},{"cmd":"yarn add httperr","lang":"bash","label":"yarn"},{"cmd":"pnpm add httperr","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The package is primarily designed for CommonJS. Direct ESM `import` statements may not work without a transpiler or ESM wrapper.","wrong":"import httperr from 'httperr';","symbol":"httperr","correct":"const httperr = require('httperr');"},{"note":"Individual error constructors can be accessed as named properties on the `httperr` object. Direct named ESM imports are generally not supported without transpilation.","wrong":"import { NotFound } from 'httperr';","symbol":"NotFound","correct":"const { NotFound } = require('httperr');\n// or\nconst httperr = require('httperr');\nconst err = new httperr.NotFound('Resource not found');"},{"note":"Error constructors can be accessed via their numeric status code using bracket notation. The `new` keyword is optional when using the function-call syntax for specific errors (e.g., `httperr[404]()` or `httperr.NotFound()`).","wrong":"const err = new httperr['404']();","symbol":"404 Error","correct":"const httperr = require('httperr');\nconst err = httperr[404]('The path could not be resolved');"}],"quickstart":{"code":"const httperr = require('httperr');\n\n// Create a 404 Not Found error with a message\nconst notFoundErr = httperr[404]('The requested resource was not found.');\nconsole.log(notFoundErr); // Displays error object with details\n// { [NotFound: The requested resource was not found.]\n//   title: 'Not Found',\n//   name: 'NotFound',\n//   code: 'NOT_FOUND',\n//   statusCode: 404,\n//   message: 'The requested resource was not found.'\n// }\n\n// Create a 405 Method Not Allowed error with extra information\nconst methodErr = httperr.methodNotAllowed({\n  message: 'HTTP method not supported for this endpoint.',\n  allowed: ['GET', 'POST']\n});\nconsole.log(methodErr);\n// { [MethodNotAllowed: HTTP method not supported for this endpoint.]\n//   title: 'Method Not Allowed',\n//   name: 'MethodNotAllowed',\n//   code: 'METHOD_NOT_ALLOWED',\n//   statusCode: 405,\n//   message: 'HTTP method not supported for this endpoint.',\n//   allowed: [ 'GET', 'POST' ]\n// }\n\n// Check error types\nconst isNotFound = notFoundErr instanceof httperr.NotFound; // true\nconst isHttpError = methodErr instanceof httperr.HttpError; // true\nconst isGenericError = notFoundErr instanceof Error; // true\n\nconsole.log(`Is notFoundErr a NotFound error? ${isNotFound}`);\nconsole.log(`Is methodErr an HttpError? ${isHttpError}`);\nconsole.log(`Is notFoundErr a generic Error? ${isGenericError}`);","lang":"javascript","description":"This quickstart demonstrates creating HTTP errors using both numeric and named constructors, attaching custom messages and additional properties, and verifying error types using `instanceof` checks."},"warnings":[{"fix":"For Node.js projects, use `const httperr = require('httperr');`. In an ESM context, consider using an intermediary CJS-to-ESM wrapper or transpilation if absolutely necessary, but migrating to a more modern error handling library is recommended for new projects.","message":"The `httperr` package, due to its age (last major update around 2017), lacks official support for ES Modules (ESM).","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"While stable for existing applications, consider more actively maintained alternatives for new projects that require ongoing support, modern JavaScript features, or integrations.","message":"The package is in maintenance mode; active development has ceased, and new features or bug fixes are unlikely to be implemented.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"To maintain code clarity and consistency, it is generally recommended to always use the `new` keyword when instantiating error objects (e.g., `new httperr.NotFound('Message')`).","message":"The `new` keyword is optional when instantiating errors (e.g., `httperr.NotFound()` vs. `new httperr.NotFound()`). This can lead to inconsistent code styles.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Prefer using the named error constructors (e.g., `httperr.BadRequest`, `httperr.Unauthorized`) when available, as they improve code readability and maintainability.","message":"Accessing error constructors via numeric indices (e.g., `httperr[404]`) is less readable and discoverable than using named properties (e.g., `httperr.NotFound`).","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Developers should consult the package's GitHub repository for recent commit activity and issue tracker status to gauge its true level of current maintenance and stability.","message":"The 'stability 3 - stable' badge in the README refers to an outdated Node.js API documentation standard (circa Node.js 0.x) and does not reflect current stability or maintenance in the broader JavaScript ecosystem.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-23T00:00:00.000Z","next_check":"2026-07-22T00:00:00.000Z","problems":[{"fix":"The main `httperr` export is an object containing error constructors, not a constructor itself. You must instantiate specific errors like `new httperr.NotFound()` or `httperr.badRequest('Invalid input');`.","cause":"Attempting to instantiate the main `httperr` object directly using `new httperr()`.","error":"TypeError: httperr is not a constructor"},{"fix":"Use the CommonJS `require` syntax instead: `const httperr = require('httperr');`.","cause":"Trying to use `import` syntax (`import httperr from 'httperr';`) in a Node.js project configured for CommonJS (default).","error":"SyntaxError: Cannot use import statement outside a module"},{"fix":"Ensure the module is imported at the top of your file: `const httperr = require('httperr');`.","cause":"Attempting to use `httperr` without first requiring it in a CommonJS environment.","error":"ReferenceError: httperr is not defined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}