HTTP Error Response Generator
The `error-http-response` package provides a utility to convert JavaScript `Error` objects into standardized HTTP problem detail objects, adhering to RFC 7807. It's currently stable at version 3.0.1, with major releases primarily driven by updates to the minimal supported Node.js version. The library automatically maps standard error properties like `name`, `message`, and `stack` to the generated HTTP response fields, and allows for extensive customization via `error.http` properties or direct options. It supports both Node.js (>=18.18.0) and browser environments, and ships with TypeScript definitions, making it suitable for modern JavaScript and TypeScript projects that need to consistently format API error responses without manual serialization.
Common errors
-
ERR_REQUIRE_ESM: require() of ES Module .../node_modules/error-http-response/build/main.js from ... not supported.
cause Attempted to use `require()` to import `error-http-response`, which is an ES module.fixChange `const errorHttpResponse = require('error-http-response');` to `import errorHttpResponse from 'error-http-response';`. -
Cannot find module 'error-http-response' or its corresponding type declarations.ts(2307)
cause TypeScript is configured to output CommonJS modules (e.g., `"module": "CommonJS"` in `tsconfig.json`), which conflicts with this package's ESM format.fixUpdate your `tsconfig.json` to configure TypeScript to output ES modules, for example, by setting `"module": "NodeNext"` or `"module": "ESNext"`.
Warnings
- breaking The minimal supported Node.js version was upgraded to `18.18.0`.
- breaking The minimal supported Node.js version was upgraded to `16.17.0`.
- gotcha This package is distributed as an ES module (ESM) only. Attempting to `require()` it will result in a runtime error.
- gotcha When using TypeScript, your project must be configured to output ES modules to correctly import and use this package. CommonJS output will lead to module resolution issues.
Install
-
npm install error-http-response -
yarn add error-http-response -
pnpm add error-http-response
Imports
- errorHttpResponse
const errorHttpResponse = require('error-http-response')import errorHttpResponse from 'error-http-response'
- AuthError
import errorHttpResponse from 'error-http-response'; class AuthError extends Error { // ... }
Quickstart
import errorHttpResponse from 'error-http-response';
class AuthError extends Error {
constructor(...args) {
super(...args);
this.http = {
type: 'https://example.com/probs/auth',
status: 401,
};
// Ensure stack trace is captured
Error.captureStackTrace(this, AuthError);
}
}
const error = new AuthError('Could not authenticate.');
error.userId = 62;
Object.assign(error.http, {
instance: '/users/62',
extra: { userId: 62 },
});
const object = errorHttpResponse(error, {
extra: { isHttp: true },
});
console.log(object);
// Expected output:
// {
// type: 'https://example.com/probs/auth',
// status: 401,
// title: 'AuthError',
// detail: 'Could not authenticate.',
// instance: '/users/62',
// stack: 'AuthError: Could not authenticate.\n at ...',
// extra: { isHttp: true, userId: 62 }
// }