{"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.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install modern-errors-http"],"cli":null},"imports":["import modernErrorsHttp from 'modern-errors-http'","import ModernError from 'modern-errors'","import type { HttpResponse } from 'modern-errors-http'"],"auth":{"required":false,"env_vars":[]},"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}