http-problem-details-mapper

raw JSON →
0.1.7 verified Sat Apr 25 auth: no javascript

A library for mapping errors to RFC 7807 HTTP Problem Details (ProblemDocument) objects. Version 0.1.7 ships TypeScript types and follows Conventional Commits. It provides an extensible mapper pattern (ErrorMapper, MapperRegistry, MappingStrategy) to convert domain errors into structured HTTP error responses. Key differentiators: integrates with http-problem-details and express-http-problem-details for a complete REST error handling stack. Release cadence is low; version 0.1.x is stable and maintained. Peer dependency on http-problem-details (^0.1.0) required.

error Error: Could not map error
cause No mapper registered for the error type and useDefaultErrorMapper set to false.
fix
Register a mapper for the error or set useDefaultErrorMapper to true in MapperRegistry constructor.
error TypeError: errorMapper is not a constructor
cause Passed an instance instead of a class to ErrorMapper constructor.
fix
Use super(NotFoundError) not super(new NotFoundError()).
error Cannot find module 'http-problem-details'
cause Missing peer dependency.
fix
npm install http-problem-details.
gotcha ErrorMapper constructor takes an Error class (constructor function), not an instance. Passing an instance will break type checks and runtime mapping.
fix Ensure the argument to super is the Error class itself, e.g., super(NotFoundError).
deprecated IErrorMapper interface is deprecated. Use ErrorMapper base class instead.
fix Extend ErrorMapper class rather than implementing IErrorMapper.
gotcha MapperRegistry.getMapper throws if no mapper found when useDefaultErrorMapper is false. Default is true and returns DefaultErrorMapper.
fix Either enable default mapper or always check mapper existence before calling mapError.
npm install http-problem-details-mapper
yarn add http-problem-details-mapper
pnpm add http-problem-details-mapper

Shows full setup: custom error, mapper, registration, and mapping with ProblemDocument.

import { ErrorMapper, MapperRegistry } from 'http-problem-details-mapper';
import { ProblemDocument } from 'http-problem-details';

class NotFoundError extends Error {
  constructor(options) {
    super();
    this.message = `${options.type} with id ${options.id} not found`;
  }
}

class NotFoundErrorMapper extends ErrorMapper {
  constructor() {
    super(NotFoundError);
  }
  mapError(error) {
    return new ProblemDocument({ status: 404, title: error.message, type: 'http://tempuri.org/NotFoundError' });
  }
}

class MyMappingStrategy {
  constructor(registry) { this.registry = registry; }
  map(error) {
    const mapper = this.registry.getMapper(error);
    if (mapper) return mapper.mapError(error);
    throw new Error('Could not map error');
  }
}

const registry = new MapperRegistry().registerMapper(new NotFoundErrorMapper());
const strategy = new MyMappingStrategy(registry);
const error = new NotFoundError({ type: 'customer', id: '123' });
const problem = strategy.map(error);
console.log(problem);