Files
descrybe/apps/web/src/lib/api-error.ts
T
greeneclipse 8580c996c3 Initial commit of Descrybe v2 without local scratch artifacts.
Drop one-shot tmp/axe scripts and agent i18n scratch so the Gitea tree is deployable.
2026-08-09 22:47:43 +02:00

44 lines
1.0 KiB
TypeScript

/**
* 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;
}