Modern Errors HTTP Response Plugin
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.
Common errors
-
ERR_REQUIRE_ESM: require() of ES Module modern-errors-http not supported
cause You are attempting to use CommonJS `require()` to import `modern-errors-http`, which is an ES module.fixChange 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'
cause `modern-errors` is a peer dependency, and both packages must be installed separately.fixRun `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
cause 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.fixEnsure your `BaseError` subclass correctly includes `plugins: [modernErrorsHttp]` and call `BaseError.httpResponse(error)` passing the error instance as an argument.
Warnings
- breaking `modern-errors-http` v5.x and later require Node.js version `18.18.0` or higher.
- 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.
- deprecated The instance method `error.httpResponse()` was deprecated in v2.2.0. While still supported, it's recommended to use the static method.
- gotcha This package is distributed exclusively as an ES module (ESM). Attempting to import it using CommonJS `require()` will result in an error.
Install
-
npm install modern-errors-http -
yarn add modern-errors-http -
pnpm add modern-errors-http
Imports
- modernErrorsHttp
const modernErrorsHttp = require('modern-errors-http')import modernErrorsHttp from 'modern-errors-http'
- ModernError
const ModernError = require('modern-errors')import ModernError from 'modern-errors'
- HttpResponse
import { HttpResponse } from 'modern-errors-http'import type { HttpResponse } from 'modern-errors-http'
Quickstart
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"
}
}
*/