Throwable HTTP Errors is a Node.js library designed to provide a comprehensive set of strongly-typed HTTP exception classes (e.g., BadRequest, NotFound, InternalServerError) for use in API development. The library, currently at stable version 2.0.3, aims to streamline error handling by integrating naturally with modern JavaScript's `async/await` syntax and `try/catch` blocks. It differentiates itself from older patterns that relied solely on `next(error)` callbacks by promoting the throwing of specific HTTP-related errors, which can then be caught and handled with standard error flow control. This approach enhances code readability and consistency in handling HTTP-specific error conditions across an application. While a specific release cadence isn't published, the current version was released over a year ago, indicating a mature and relatively stable project. The package also ships with TypeScript type definitions, making it suitable for modern TypeScript projects.
npm install throwable-http-errorsVerified import paths — ran on the pinned version, not inferred.
Demonstrates creating and handling throwable HTTP errors within an Express.js API using `async/await` and a global error middleware, showcasing `BadRequest` and `NotFound` errors.
Adopt `try/catch` blocks around asynchronous operations and `throw new Errors.SomeError()` instead of `return next(new Errors.SomeError())`.
Review the library's official changelog (if available) for `throwable-http-errors` v1 to v2. Ensure your error handling middleware correctly identifies and processes `HttpError` instances, potentially using `err instanceof HttpError`.
Always use the specific named error classes (e.g., `new BadRequest('Invalid input')`) for clarity and consistency, rather than attempting to instantiate a generic error with a status code unless explicitly supported by the `HttpError` base class.Always use the `new` keyword when creating an instance of an error class, e.g., `throw new Errors.BadRequest('Invalid data')`.Ensure all `async` functions that might throw an error are called with `await` within a `try/catch` block, or that the returned promise has a `.catch()` handler.
For CommonJS, use `const Errors = require('throwable-http-errors');`. For ESM/TypeScript, use named imports: `import { BadRequest } from 'throwable-http-errors;`.In TypeScript, use a type guard to check if the error is an instance of `HttpError` before accessing `status`: `if (err instanceof HttpError) { console.log(err.status); }`.No dependency data recorded yet.