/** * src/http/siax-error-response.ts * ================================= * Maps a thrown error to a NextResponse for the source-registry routes * (app/api/0pt/sources/*). SourceRegistryService throws real `SiaxError` * instances (src/platform/errors.ts) with a pinned `.httpStatus`/`.code` — * this honors that mapping directly instead of re-deriving status codes from * regex-matching `error.message`, the ad-hoc pattern most other 0pt routes * use (app/api/0pt/documents/[documentId]/extraction/route.ts etc.) because * their services still throw plain `Error`. SourceService/SourceRegistryService's * lower-level plain Errors ("already exists", "not found") still fall back * to that same regex convention here, for consistency with the rest of the * repo's error surface. */ import { NextResponse } from "next/server"; import { SiaxError } from "../platform/errors"; export function siaxErrorResponse(error: unknown): NextResponse { if (error instanceof SiaxError) { return NextResponse.json(error.toJSON(), { status: error.httpStatus }); } const message = error instanceof Error ? error.message : "Unknown error"; const status = /already exists/i.test(message) ? 409 : /not found/i.test(message) ? 404 : 400; return NextResponse.json({ error: message }, { status }); }