throw-http-errors is a JavaScript/TypeScript library designed to streamline the creation and handling of HTTP status code-specific errors. Currently at version 4.0.1, the library provides a comprehensive set of custom error classes corresponding to most standard HTTP status codes, from client errors like `BadRequest` (400) and `Unauthorized` (401) to server errors such as `InternalServerError` (500) and `ServiceUnavailable` (503). It allows developers to instantiate these errors either by their descriptive named class (e.g., `new errors.NotFound('User not found')`) or by their numeric status code (e.g., `new errors[404]('Resource missing')`), along with optional custom messages and internal error codes, providing flexibility for different error reporting needs. The library also includes a `CreateCustomError` utility for defining unique application-specific HTTP errors. This package differentiates itself by offering a structured, type-safe approach to HTTP error propagation in modern Node.js and browser environments, shipping with full TypeScript definitions to enhance developer experience and reduce runtime errors. While a previous JavaScript-only version exists on a separate branch (pre-v4), the current major version prioritizes TypeScript and ESM, ensuring a modern and robust error handling mechanism for web applications and APIs. The project maintains a steady release cadence, with major versions typically indicating significant architectural or language-level shifts.
npm install throw-http-errorsVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to create and catch various HTTP-specific errors, including custom ones, within a simulated API request function. It shows instantiation by named classes and how to handle them polymorphically using `instanceof HttpError`.
For ESM projects, use `import { ErrorName } from 'throw-http-errors';` or `import * as errors from 'throw-http-errors';`. For CommonJS projects, ensure your build setup correctly handles ESM modules, or consider sticking to a pre-v4 version if a full migration to ESM imports is not feasible.Always instantiate specific error classes, e.g., `new errors.NotFound()` or `new errors[404]()`, or import them individually as named exports like `new NotFound()`.
Use `new errors.BadRequest()` or `new errors[400]()` to create specific HTTP error instances, or import and instantiate individual error classes like `new BadRequest()`.
Migrate your imports to ESM syntax: `import { BadRequest } from 'throw-http-errors';` or `import * as errors from 'throw-http-errors';`.Ensure your bundler (e.g., Webpack, Rollup, Parcel) or TypeScript configuration (`esModuleInterop`, `allowSyntheticDefaultImports`) is correctly set up to handle module interop. If using TypeScript, set `"esModuleInterop": true` in your `tsconfig.json`. Always explicitly import named exports: `import { BadRequest } from 'throw-http-errors';`.No dependency data recorded yet.