modern-errors-http is a plugin for the modern-errors library, designed to convert custom error instances into RFC 7807 compliant "problem details" objects suitable for HTTP responses. The current stable version is 5.0.1. Releases typically follow updates to its peer dependency, modern-errors, and align with Node.js LTS versions, indicating a deliberate and stable release cadence. Its key differentiators include seamless integration with the modern-errors ecosystem, adherence to the standardized RFC 7807 format for error responses, and broad compatibility across both Node.js (>=18.18.0) and browser environments. The package also ships with full TypeScript type definitions, enhancing developer experience and compile-time safety for TypeScript users. It simplifies the creation of consistent, machine-readable HTTP error payloads for APIs.
npm install modern-errors-httpVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to integrate the modern-errors-http plugin, define custom HTTP errors, and generate RFC 7807 compliant problem detail objects for API responses.
Ensure your Node.js environment is updated to `18.18.0` or newer. Earlier versions will not work.
Install the required peer dependency: `npm install modern-errors@^7.0.3` or `yarn add modern-errors@^7.0.3`.
Instead of `error.httpResponse()`, use the static method `BaseError.httpResponse(error)` where `BaseError` is your plugin-configured error class.
Configure your project to use ES modules. This typically involves setting `"type": "module"` in your `package.json` and using `import` statements. For TypeScript, ensure your `tsconfig.json` outputs ES modules (e.g., `"module": "Node16"` or `"ESNext"`).
Change your import statement to `import modernErrorsHttp from 'modern-errors-http'` and ensure your project is configured for ES modules (e.g., `"type": "module"` in `package.json`).
Run `npm install modern-errors modern-errors-http` to ensure both the plugin and its core dependency are present.
Ensure your `BaseError` subclass correctly includes `plugins: [modernErrorsHttp]` and call `BaseError.httpResponse(error)` passing the error instance as an argument.