The ProblemResponseFormatter class provides a way to customize the format of
problem responses in your API. This allows you to maintain consistent error
response formats across your API while adhering to the
RFC 7807 Problem Details specification.
Overview
The ProblemResponseFormatter class works in conjunction with the
HttpProblems helper to format error responses. It can be used to customize how
problem details are serialized and presented to API consumers.
Static Methods
format
Formats a problem response with the provided details.
import { ProblemResponseFormatter, RuntimeExtensions, ProblemResponseDetails,} from "@zuplo/runtime";export function runtimeInit(runtime: RuntimeExtensions) { runtime.addErrorHandler(async (error, request, context) => { // Map errors to problem details const problemDetails: ProblemResponseDetails = { type: "https://api.example.com/errors/internal", title: "Internal Server Error", status: 500, detail: error.message, instance: request.url, // Add custom extensions extensions: { errorId: crypto.randomUUID(), timestamp: new Date().toISOString(), }, }; return ProblemResponseFormatter.format(problemDetails, request, context); });}
typescript
Problem Response Details Interface
The ProblemResponseDetails interface defines the structure of problem details:
interface ProblemResponseDetails { type?: string; // URI reference for the problem type title: string; // Short, human-readable summary status: number; // HTTP status code detail?: string; // Human-readable explanation instance?: string; // URI reference for the specific occurrence extensions?: Record<string, any>; // Additional problem-specific details}
typescript
Integration with HttpProblems
The ProblemResponseFormatter is used internally by the HttpProblems helper
class. When you use HttpProblems methods, they utilize
ProblemResponseFormatter to ensure consistent formatting: