Repository Public
1// One error type for every failed API call. `code` follows the contract's open code table; unknown codes are kept.23export class ApiError extends Error {4 readonly status: number;5 readonly code: string;6 readonly correlationId: string | undefined;7 readonly details: Record<string, unknown> | undefined;8 readonly retryAfter: number | undefined;910 constructor(init: { status: number; code: string; message: string; correlationId?: string | undefined; details?: Record<string, unknown> | undefined; retryAfter?: number | undefined }) {11 super(init.message);12 this.name = "ApiError";13 this.status = init.status;14 this.code = init.code;15 this.correlationId = init.correlationId;16 this.details = init.details;17 this.retryAfter = init.retryAfter;18 }1920 /** Endpoint exists in the contract but the server does not implement it yet (501). */21 get notImplemented(): boolean {22 return this.status === 501;23 }24 get unavailable(): boolean {25 return this.status === 503 || this.status === 502 || this.status === 504;26 }27 get network(): boolean {28 return this.status === 0;29 }30 get forbidden(): boolean {31 return this.status === 403 && this.code !== "step_up_required" && this.code !== "csrf_failed";32 }33 get unauthenticated(): boolean {34 return this.status === 401;35 }36 /** Name of the permission the server said is missing, when it said so. */37 get missingPermission(): string | undefined {38 const p = this.details?.permission;39 return typeof p === "string" ? p : undefined;40 }41}4243export function isApiError(e: unknown): e is ApiError {44 return e instanceof ApiError;45}4647/**48 * Builds the ApiError of a non-2xx response. `parsed` is the body when the caller already consumed it (openapi-fetch49 * reads error bodies itself, after which the Response can no longer be read).50 */51export async function errorFromResponse(response: Response, parsed?: unknown): Promise<ApiError> {52 let code = `http_${response.status}`;53 let message = response.statusText || code;54 let correlationId = response.headers.get("X-Correlation-Id") ?? undefined;55 let details: Record<string, unknown> | undefined;56 let body: unknown = parsed;57 if (body === undefined && !response.bodyUsed) {58 try {59 body = await response.clone().json();60 } catch {61 /* non-JSON error body (proxy error page): keep the HTTP status */62 }63 }64 if (typeof body === "string") {65 try {66 body = JSON.parse(body);67 } catch {68 /* plain text */69 }70 }71 if (typeof body === "object" && body !== null && "error" in body) {72 const e = (body as { error: unknown }).error;73 if (typeof e === "object" && e !== null) {74 const err = e as Record<string, unknown>;75 if (typeof err.code === "string") code = err.code;76 if (typeof err.message === "string") message = err.message;77 if (typeof err.correlation_id === "string") correlationId = err.correlation_id;78 if (typeof err.details === "object" && err.details !== null) details = err.details as Record<string, unknown>;79 }80 }81 const ra = Number(response.headers.get("Retry-After"));82 return new ApiError({ status: response.status, code, message, correlationId, details, retryAfter: Number.isFinite(ra) && ra > 0 ? ra : undefined });83}84