/** * Pure API error types/helpers (no $env / $app) — safe for node:test. */ export class ApiError extends Error { status: number; body: unknown; constructor(message: string, status: number, body: unknown) { super(message); this.name = "ApiError"; this.status = status; this.body = body; } } /** HTTP status phrases that should not be shown as form copy. */ const OPAQUE_HTTP_STATUS = new Set([ "", "bad request", "unauthorized", "forbidden", "not found", "conflict", "too many requests", "internal server error", "service unavailable", "method not allowed", "request failed" ]); function isOpaqueHttpStatus(msg: string): boolean { return OPAQUE_HTTP_STATUS.has(msg.trim().toLowerCase()); } /** User-visible message from an `api()` / `apiDownload()` catch value (or any thrown Error). */ export function failureMessage(err: unknown, fallback: string): string { if (err instanceof Error) { const msg = err.message.trim(); if (msg && !isOpaqueHttpStatus(msg)) return msg; } return fallback; }