diff --git a/frontend/src/shared/api/http.ts b/frontend/src/shared/api/http.ts index 5d2bf02..806a394 100644 --- a/frontend/src/shared/api/http.ts +++ b/frontend/src/shared/api/http.ts @@ -13,7 +13,31 @@ export async function apiFetch(url: string, options: FetchOptions = ...options, headers, }) - if (!response.ok) throw new Error(`API error: ${response.status}`) + if (!response.ok) { + const detail = await readErrorDetail(response) + throw new Error(detail ? `${response.status}: ${detail}` : `API error: ${response.status}`) + } if (response.status === 204) return null as T return response.json() as Promise } + +async function readErrorDetail(response: Response): Promise { + try { + const contentType = response.headers.get('content-type') ?? '' + if (!contentType.includes('application/json')) return null + const body = await response.json() + if (typeof body?.detail === 'string') return body.detail + if (Array.isArray(body?.detail)) { + return body.detail + .map((e: { loc?: unknown[]; msg?: string }) => { + const loc = Array.isArray(e.loc) ? e.loc.join('.') : '' + return loc ? `${loc}: ${e.msg ?? ''}` : (e.msg ?? '') + }) + .filter(Boolean) + .join('; ') + } + return null + } catch { + return null + } +}