From 664178f0cfd50990844bf6eddd6af6a96d1337d5 Mon Sep 17 00:00:00 2001 From: Jamie Pine Date: Mon, 16 Mar 2026 22:47:01 -0700 Subject: [PATCH] fix error detail serialization producing [object Object] in error messages Closes #290 --- app/src/lib/api/client.ts | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/app/src/lib/api/client.ts b/app/src/lib/api/client.ts index 036af3df..98f98182 100644 --- a/app/src/lib/api/client.ts +++ b/app/src/lib/api/client.ts @@ -35,6 +35,21 @@ import type { WhisperModelSize, } from './types'; +function formatErrorDetail(detail: unknown, fallback: string): string { + if (typeof detail === 'string') return detail; + if (Array.isArray(detail)) { + return detail + .map((e: Record) => e.msg || e.message || JSON.stringify(e)) + .join('; '); + } + if (detail && typeof detail === 'object') { + const obj = detail as Record; + if (typeof obj.message === 'string') return obj.message; + return JSON.stringify(detail); + } + return fallback; +} + class ApiClient { private getBaseUrl(): string { const serverUrl = useServerStore.getState().serverUrl; @@ -55,7 +70,7 @@ class ApiClient { const error = await response.json().catch(() => ({ detail: response.statusText, })); - throw new Error(error.detail || `HTTP error! status: ${response.status}`); + throw new Error(formatErrorDetail(error.detail, `HTTP error! status: ${response.status}`)); } return response.json(); @@ -114,7 +129,7 @@ class ApiClient { const error = await response.json().catch(() => ({ detail: response.statusText, })); - throw new Error(error.detail || `HTTP error! status: ${response.status}`); + throw new Error(formatErrorDetail(error.detail, `HTTP error! status: ${response.status}`)); } return response.json(); @@ -148,7 +163,7 @@ class ApiClient { const error = await response.json().catch(() => ({ detail: response.statusText, })); - throw new Error(error.detail || `HTTP error! status: ${response.status}`); + throw new Error(formatErrorDetail(error.detail, `HTTP error! status: ${response.status}`)); } return response.blob(); @@ -168,7 +183,7 @@ class ApiClient { const error = await response.json().catch(() => ({ detail: response.statusText, })); - throw new Error(error.detail || `HTTP error! status: ${response.status}`); + throw new Error(formatErrorDetail(error.detail, `HTTP error! status: ${response.status}`)); } return response.json(); @@ -188,7 +203,7 @@ class ApiClient { const error = await response.json().catch(() => ({ detail: response.statusText, })); - throw new Error(error.detail || `HTTP error! status: ${response.status}`); + throw new Error(formatErrorDetail(error.detail, `HTTP error! status: ${response.status}`)); } return response.json(); @@ -258,7 +273,7 @@ class ApiClient { const error = await response.json().catch(() => ({ detail: response.statusText, })); - throw new Error(error.detail || `HTTP error! status: ${response.status}`); + throw new Error(formatErrorDetail(error.detail, `HTTP error! status: ${response.status}`)); } return response.blob(); @@ -272,7 +287,7 @@ class ApiClient { const error = await response.json().catch(() => ({ detail: response.statusText, })); - throw new Error(error.detail || `HTTP error! status: ${response.status}`); + throw new Error(formatErrorDetail(error.detail, `HTTP error! status: ${response.status}`)); } return response.blob(); @@ -298,7 +313,7 @@ class ApiClient { const error = await response.json().catch(() => ({ detail: response.statusText, })); - throw new Error(error.detail || `HTTP error! status: ${response.status}`); + throw new Error(formatErrorDetail(error.detail, `HTTP error! status: ${response.status}`)); } return response.json(); @@ -343,7 +358,7 @@ class ApiClient { const error = await response.json().catch(() => ({ detail: response.statusText, })); - throw new Error(error.detail || `HTTP error! status: ${response.status}`); + throw new Error(formatErrorDetail(error.detail, `HTTP error! status: ${response.status}`)); } return response.json(); @@ -616,7 +631,7 @@ class ApiClient { const error = await response.json().catch(() => ({ detail: response.statusText, })); - throw new Error(error.detail || `HTTP error! status: ${response.status}`); + throw new Error(formatErrorDetail(error.detail, `HTTP error! status: ${response.status}`)); } return response.blob(); @@ -713,7 +728,7 @@ class ApiClient { const error = await response.json().catch(() => ({ detail: response.statusText, })); - throw new Error(error.detail || `HTTP error! status: ${response.status}`); + throw new Error(formatErrorDetail(error.detail, `HTTP error! status: ${response.status}`)); } return response.blob();