Registry / http-networking / modern-errors-http

modern-errors-http

JSON →
library5.0.1jsnpmunverified

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-http
INSTALL
IMPORT
SIG · MODERN-ERRORS-HTTP
M
modern-errors-http
http-networkingjavascriptv5.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

modernErrorsHttp
import modernErrorsHttp from 'modern-errors-http'
const modernErrorsHttp = require('modern-errors-http')
This package is ESM-only. CommonJS `require()` is not supported.
ModernError
import ModernError from 'modern-errors'
const ModernError = require('modern-errors')
The peer dependency `modern-errors` is also ESM-only, requiring `import` syntax.
HttpResponse
import type { HttpResponse } from 'modern-errors-http'
import { HttpResponse } from 'modern-errors-http'
The `HttpResponse` type defines the shape of the RFC 7807 problem details object returned by the plugin. Use `import type` to avoid runtime import issues.

Demonstrates how to integrate the modern-errors-http plugin, define custom HTTP errors, and generate RFC 7807 compliant problem detail objects for API responses.

import ModernError from 'modern-errors'; import modernErrorsHttp from 'modern-errors-http'; // 1. Extend ModernError with the HTTP plugin export const BaseError = ModernError.subclass('BaseError', { plugins: [modernErrorsHttp], }); // 2. Define a custom error with HTTP-specific properties export const AuthError = BaseError.subclass('AuthError', { http: { type: 'https://example.com/probs/auth', status: 401, title: 'Authentication Failed' }, }); // 3. Create an instance of the custom error const error = new AuthError('Could not authenticate the provided credentials.', { http: { instance: '/users/62/login', extra: { userId: 62, attemptedUsername: 'testuser' }, }, }); // 4. Convert the error to an RFC 7807 compliant HTTP response object const httpResponseObject = BaseError.httpResponse(error); // In a real application, this 'httpResponseObject' would be sent as a JSON response body. console.log(JSON.stringify(httpResponseObject, null, 2)); /* Expected output (stack trace will vary): { "type": "https://example.com/probs/auth", "status": 401, "title": "Authentication Failed", "detail": "Could not authenticate the provided credentials.", "instance": "/users/62/login", "stack": "AuthError: Could not authenticate the provided credentials.\n at ...", "extra": { "userId": 62, "attemptedUsername": "testuser" } } */
Debug
Known issues
breaking`modern-errors-http` v5.x and later require Node.js version `18.18.0` or higher.
fix
Ensure your Node.js environment is updated to `18.18.0` or newer. Earlier versions will not work.
affects: >=5.0.0
breaking`modern-errors-http` v5.x requires `modern-errors` v7.0.3 or higher as a peer dependency. Installing `modern-errors-http` without the correct `modern-errors` version will lead to runtime errors.
fix
Install the required peer dependency: `npm install modern-errors@^7.0.3` or `yarn add modern-errors@^7.0.3`.
affects: >=5.0.0
deprecatedThe instance method `error.httpResponse()` was deprecated in v2.2.0. While still supported, it's recommended to use the static method.
fix
Instead of `error.httpResponse()`, use the static method `BaseError.httpResponse(error)` where `BaseError` is your plugin-configured error class.
affects: >=2.2.0
gotchaThis package is distributed exclusively as an ES module (ESM). Attempting to import it using CommonJS `require()` will result in an error.
fix
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"`).
affects: >=1.0.0
Errors
Common errors & fixes
ERR_REQUIRE_ESM: require() of ES Module modern-errors-http not supported
You are attempting to use CommonJS `require()` to import `modern-errors-http`, which is an ES module.
fix
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`).
Error: Cannot find module 'modern-errors' or 'modern-errors-http'
`modern-errors` is a peer dependency, and both packages must be installed separately.
fix
Run `npm install modern-errors modern-errors-http` to ensure both the plugin and its core dependency are present.
TypeError: BaseError.httpResponse is not a function
The `modernErrorsHttp` plugin was not correctly added to your `ModernError.subclass` definition, or you are calling `error.httpResponse()` on an instance after v2.2.0 when the static method `BaseError.httpResponse(error)` should be used.
fix
Ensure your `BaseError` subclass correctly includes `plugins: [modernErrorsHttp]` and call `BaseError.httpResponse(error)` passing the error instance as an argument.
Upgrade
Version history
5.0.1latest on npm
Audit
Dependencies
modern-errorsrequiredCore error library that modern-errors-http extends and integrates with.
Agent activity
14 hits · last 30 days
node
10
OpenAI (training)
2
Resources
modern-errors-http — npm install modern-errors-http · libregistry