{"id":17294,"library":"modern-errors-http","title":"Modern Errors HTTP Response Plugin","description":"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.","status":"active","version":"5.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/ehmicky/modern-errors-http","tags":["javascript","browser","error","error-handler","error-handling","error-monitoring","http","library","typescript"],"install":[{"cmd":"npm install modern-errors-http","lang":"bash","label":"npm"},{"cmd":"yarn add modern-errors-http","lang":"bash","label":"yarn"},{"cmd":"pnpm add modern-errors-http","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core error library that modern-errors-http extends and integrates with.","package":"modern-errors","optional":false}],"imports":[{"note":"This package is ESM-only. CommonJS `require()` is not supported.","wrong":"const modernErrorsHttp = require('modern-errors-http')","symbol":"modernErrorsHttp","correct":"import modernErrorsHttp from 'modern-errors-http'"},{"note":"The peer dependency `modern-errors` is also ESM-only, requiring `import` syntax.","wrong":"const ModernError = require('modern-errors')","symbol":"ModernError","correct":"import ModernError from 'modern-errors'"},{"note":"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.","wrong":"import { HttpResponse } from 'modern-errors-http'","symbol":"HttpResponse","correct":"import type { HttpResponse } from 'modern-errors-http'"}],"quickstart":{"code":"import ModernError from 'modern-errors';\nimport modernErrorsHttp from 'modern-errors-http';\n\n// 1. Extend ModernError with the HTTP plugin\nexport const BaseError = ModernError.subclass('BaseError', {\n  plugins: [modernErrorsHttp],\n});\n\n// 2. Define a custom error with HTTP-specific properties\nexport const AuthError = BaseError.subclass('AuthError', {\n  http: {\n    type: 'https://example.com/probs/auth',\n    status: 401,\n    title: 'Authentication Failed'\n  },\n});\n\n// 3. Create an instance of the custom error\nconst error = new AuthError('Could not authenticate the provided credentials.', {\n  http: {\n    instance: '/users/62/login',\n    extra: { userId: 62, attemptedUsername: 'testuser' },\n  },\n});\n\n// 4. Convert the error to an RFC 7807 compliant HTTP response object\nconst httpResponseObject = BaseError.httpResponse(error);\n\n// In a real application, this 'httpResponseObject' would be sent as a JSON response body.\nconsole.log(JSON.stringify(httpResponseObject, null, 2));\n/*\nExpected output (stack trace will vary):\n{\n  \"type\": \"https://example.com/probs/auth\",\n  \"status\": 401,\n  \"title\": \"Authentication Failed\",\n  \"detail\": \"Could not authenticate the provided credentials.\",\n  \"instance\": \"/users/62/login\",\n  \"stack\": \"AuthError: Could not authenticate the provided credentials.\\n    at ...\",\n  \"extra\": {\n    \"userId\": 62,\n    \"attemptedUsername\": \"testuser\"\n  }\n}\n*/","lang":"typescript","description":"Demonstrates how to integrate the modern-errors-http plugin, define custom HTTP errors, and generate RFC 7807 compliant problem detail objects for API responses."},"warnings":[{"fix":"Ensure your Node.js environment is updated to `18.18.0` or newer. Earlier versions will not work.","message":"`modern-errors-http` v5.x and later require Node.js version `18.18.0` or higher.","severity":"breaking","affected_versions":">=5.0.0"},{"fix":"Install the required peer dependency: `npm install modern-errors@^7.0.3` or `yarn add modern-errors@^7.0.3`.","message":"`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.","severity":"breaking","affected_versions":">=5.0.0"},{"fix":"Instead of `error.httpResponse()`, use the static method `BaseError.httpResponse(error)` where `BaseError` is your plugin-configured error class.","message":"The instance method `error.httpResponse()` was deprecated in v2.2.0. While still supported, it's recommended to use the static method.","severity":"deprecated","affected_versions":">=2.2.0"},{"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\"`).","message":"This package is distributed exclusively as an ES module (ESM). Attempting to import it using CommonJS `require()` will result in an error.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"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`).","cause":"You are attempting to use CommonJS `require()` to import `modern-errors-http`, which is an ES module.","error":"ERR_REQUIRE_ESM: require() of ES Module modern-errors-http not supported"},{"fix":"Run `npm install modern-errors modern-errors-http` to ensure both the plugin and its core dependency are present.","cause":"`modern-errors` is a peer dependency, and both packages must be installed separately.","error":"Error: Cannot find module 'modern-errors' or 'modern-errors-http'"},{"fix":"Ensure your `BaseError` subclass correctly includes `plugins: [modernErrorsHttp]` and call `BaseError.httpResponse(error)` passing the error instance as an argument.","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.","error":"TypeError: BaseError.httpResponse is not a function"}],"ecosystem":"npm","meta_description":null}