// author: simon-pierre boucher import { httpStatusFor, type ErrorCode, type TendrilError } from "@tendril/shared"; export interface ApiErrorBody { readonly success: false; readonly error: { readonly code: ErrorCode; readonly message: string; readonly details?: Readonly>; }; readonly requestId: string; } /** * The §15 mapping applied at the HTTP boundary — the single place that turns a * TendrilError into an HTTP status + response envelope. The status table itself * lives in @tendril/shared ERROR_TAXONOMY; this function is its only consumer at * the API edge. */ export function toApiError(error: TendrilError, requestId: string): { status: number; body: ApiErrorBody } { const status = httpStatusFor(error.code); const body: ApiErrorBody = { success: false, error: { code: error.code, message: error.message, ...(error.details !== undefined ? { details: error.details } : {}), }, requestId, }; return { status, body }; }