HTTP Status Code Errors
raw JSON →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.
Common errors
error TypeError: httperr is not a constructor ↓
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 ↓
require syntax instead: const httperr = require('httperr');. error ReferenceError: httperr is not defined ↓
const httperr = require('httperr');. Warnings
breaking The `httperr` package, due to its age (last major update around 2017), lacks official support for ES Modules (ESM). ↓
gotcha The package is in maintenance mode; active development has ceased, and new features or bug fixes are unlikely to be implemented. ↓
gotcha The `new` keyword is optional when instantiating errors (e.g., `httperr.NotFound()` vs. `new httperr.NotFound()`). This can lead to inconsistent code styles. ↓
gotcha Accessing error constructors via numeric indices (e.g., `httperr[404]`) is less readable and discoverable than using named properties (e.g., `httperr.NotFound`). ↓
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. ↓
Install
npm install httperr yarn add httperr pnpm add httperr Imports
- httperr wrong
import httperr from 'httperr';correctconst httperr = require('httperr'); - NotFound wrong
import { NotFound } from 'httperr';correctconst { NotFound } = require('httperr'); // or const httperr = require('httperr'); const err = new httperr.NotFound('Resource not found'); - 404 Error wrong
const err = new httperr['404']();correctconst httperr = require('httperr'); const err = httperr[404]('The path could not be resolved');
Quickstart
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}`);