HTTP Status Code Errors

raw JSON →
1.0.0 verified Thu Apr 23 auth: no javascript maintenance

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.

error TypeError: httperr is not a constructor
cause Attempting to instantiate the main `httperr` object directly using `new httperr()`.
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');.
error SyntaxError: Cannot use import statement outside a module
cause Trying to use `import` syntax (`import httperr from 'httperr';`) in a Node.js project configured for CommonJS (default).
fix
Use the CommonJS require syntax instead: const httperr = require('httperr');.
error ReferenceError: httperr is not defined
cause Attempting to use `httperr` without first requiring it in a CommonJS environment.
fix
Ensure the module is imported at the top of your file: const httperr = require('httperr');.
breaking The `httperr` package, due to its age (last major update around 2017), lacks official support for ES Modules (ESM).
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.
gotcha The package is in maintenance mode; active development has ceased, and new features or bug fixes are unlikely to be implemented.
fix While stable for existing applications, consider more actively maintained alternatives for new projects that require ongoing support, modern JavaScript features, or integrations.
gotcha The `new` keyword is optional when instantiating errors (e.g., `httperr.NotFound()` vs. `new httperr.NotFound()`). This can lead to inconsistent code styles.
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')`).
gotcha Accessing error constructors via numeric indices (e.g., `httperr[404]`) is less readable and discoverable than using named properties (e.g., `httperr.NotFound`).
fix Prefer using the named error constructors (e.g., `httperr.BadRequest`, `httperr.Unauthorized`) when available, as they improve code readability and maintainability.
gotcha 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.
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.
npm install httperr
yarn add httperr
pnpm add httperr

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.

const httperr = require('httperr');

// Create a 404 Not Found error with a message
const notFoundErr = httperr[404]('The requested resource was not found.');
console.log(notFoundErr); // Displays error object with details
// { [NotFound: The requested resource was not found.]
//   title: 'Not Found',
//   name: 'NotFound',
//   code: 'NOT_FOUND',
//   statusCode: 404,
//   message: 'The requested resource was not found.'
// }

// Create a 405 Method Not Allowed error with extra information
const methodErr = httperr.methodNotAllowed({
  message: 'HTTP method not supported for this endpoint.',
  allowed: ['GET', 'POST']
});
console.log(methodErr);
// { [MethodNotAllowed: HTTP method not supported for this endpoint.]
//   title: 'Method Not Allowed',
//   name: 'MethodNotAllowed',
//   code: 'METHOD_NOT_ALLOWED',
//   statusCode: 405,
//   message: 'HTTP method not supported for this endpoint.',
//   allowed: [ 'GET', 'POST' ]
// }

// Check error types
const isNotFound = notFoundErr instanceof httperr.NotFound; // true
const isHttpError = methodErr instanceof httperr.HttpError; // true
const isGenericError = notFoundErr instanceof Error; // true

console.log(`Is notFoundErr a NotFound error? ${isNotFound}`);
console.log(`Is methodErr an HttpError? ${isHttpError}`);
console.log(`Is notFoundErr a generic Error? ${isGenericError}`);