import axios, { AxiosError } from "axios"; import { ApiError, ApiErrorPayload, ApiValidationIssue, StructuredApiErrorDetail } from "./types"; function isRecord(value: unknown): value is Record { return typeof value === "object" && value !== null && !Array.isArray(value); } function validationMessage(detail: ApiValidationIssue[]): string | null { const first=detail.find((issue)=>typeof issue?.msg === "string" && issue.msg.trim().length>0); return first?.msg?.trim() ?? null; } function structuredDetail(detail: unknown): StructuredApiErrorDetail | null { if(!isRecord(detail)) return null; return { code: typeof detail.code === "string" ? detail.code : undefined, message: typeof detail.message === "string" ? detail.message : undefined, context: isRecord(detail.context) ? detail.context : null }; } export function normalizeApiError(error: unknown): ApiError { if(error instanceof ApiError) return error; if(!axios.isAxiosError(error)) return new ApiError({message:error instanceof Error && error.message ? error.message : "Unexpected client error.",status:null,code:null,details:null,cause:error}); const axiosError=error as AxiosError; const payload=axiosError.response?.data; const detail=payload?.detail ?? null; const structured=structuredDetail(detail); let message:string|null=null; if(typeof detail === "string" && detail.trim()) message=detail.trim(); else if(Array.isArray(detail)) message=validationMessage(detail as ApiValidationIssue[]); else if(structured?.message?.trim()) message=structured.message.trim(); else if(typeof payload?.message === "string" && payload.message.trim()) message=payload.message.trim(); const code=structured?.code ?? (typeof payload?.code === "string" ? payload.code : null) ?? axiosError.code ?? null; return new ApiError({message:message ?? axiosError.message ?? "Request failed.",status:axiosError.response?.status ?? null,code,details:detail,cause:axiosError,response:axiosError.response ? {status:axiosError.response.status,data:payload}:undefined}); } export function getApiErrorStatus(error: unknown): number | null { if(error instanceof ApiError) return error.status; if(axios.isAxiosError(error)) return error.response?.status ?? null; if(isRecord(error)){ if(typeof error.status === "number") return error.status; const response=error.response; if(isRecord(response)&&typeof response.status === "number") return response.status;} return null; } export function getApiErrorMessage(error: unknown, fallback="Something went wrong. Please try again."): string { if(error instanceof ApiError && error.message.trim()) return error.message; const normalized=normalizeApiError(error); return normalized.message || fallback; } export function isUnauthorizedApiError(error: unknown): boolean { return getApiErrorStatus(error)===401; } export function isForbiddenApiError(error: unknown): boolean { return getApiErrorStatus(error)===403; } export function isTransientApiError(error: unknown): boolean { const status=getApiErrorStatus(error); return status===null || status===408 || status===429 || (status!==null && status>=500); }