Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.
JavaScript aianthropiccross-platformgeminigroqlinuxmacosnvidiaopen-sourceopenaiparakeetspeech-to-texttranscribewhisperwindows
fix(api): extract top-level and string detail fields from API error bodies (#1339)
Henry Suf70073611d9d
diff --git a/src/services/ai/apiErrorMessage.ts b/src/services/ai/apiErrorMessage.tsindex b09ae4a06..fa1266cf6 100644--- a/src/services/ai/apiErrorMessage.ts+++ b/src/services/ai/apiErrorMessage.ts@@ -12,12 +12,17 @@ function isRecord(value: unknown): value is Record<string, unknown> {return typeof value === "object" && value !== null && !Array.isArray(value);}-/** FastAPI/Pydantic validation errors: {detail: [{loc, msg}]}. */-function fromDetailList(message: unknown): string | null {- if (!isRecord(message) || !Array.isArray(message.detail)) return null;+/** FastAPI/Pydantic error payloads: {detail: "..."} or {detail: [{loc, msg}]}. */+function fromDetail(container: unknown): string | null {+ if (!isRecord(container)) return null;++ const direct = asNonEmptyString(container.detail);+ if (direct) return direct;++ if (!Array.isArray(container.detail)) return null;const parts: string[] = [];- for (const entry of message.detail) {+ for (const entry of container.detail) {if (!isRecord(entry)) continue;const msg = asNonEmptyString(entry.msg);if (!msg) continue;@@ -51,19 +56,19 @@ export function extractApiErrorMessage(errorData: unknown, fallback: string): stconst flat = asNonEmptyString(errorData.message);if (flat) return flat;- const detail = fromDetailList(errorData.message);- if (detail) return detail;+ // Nested containers first: a top-level detail usually just restates the HTTP status,+ // while the wrapped payload carries the upstream error.+ for (const container of [errorData.message, errorData.error, errorData]) {+ const detail = fromDetail(container);+ if (detail) return detail;+ }const errorString = asNonEmptyString(errorData.error);if (errorString) return errorString;- if (isRecord(errorData.message) || Array.isArray(errorData.message)) {- const stringified = stringify(errorData.message);- if (stringified) return stringified;- }-- if (isRecord(errorData.error) || Array.isArray(errorData.error)) {- const stringified = stringify(errorData.error);+ for (const value of [errorData.message, errorData.detail, errorData.error]) {+ if (!isRecord(value) && !Array.isArray(value)) continue;+ const stringified = stringify(value);if (stringified) return stringified;}diff --git a/test/helpers/apiErrorMessage.test.js b/test/helpers/apiErrorMessage.test.jsindex 23d1213b1..6edc9b196 100644--- a/test/helpers/apiErrorMessage.test.js+++ b/test/helpers/apiErrorMessage.test.js@@ -135,3 +135,58 @@ test("an empty fallback still yields a non-empty message", async () => {assert.equal(extractApiErrorMessage({}, ""), "Unknown API error");});++test("a fastapi detail string is used when there is no message", async () => {+ const { extractApiErrorMessage } = await load();++ assert.equal(+ extractApiErrorMessage({ detail: "Model 'llama3' not found" }, "fallback"),+ "Model 'llama3' not found"+ );+});++test("a top-level detail list names every rejected field", async () => {+ const { extractApiErrorMessage } = await load();++ const body = { detail: [{ loc: ["body", "model"], msg: "field required" }] };++ assert.equal(extractApiErrorMessage(body, "fallback"), "model: field required");+});++test("a detail string nested under message is extracted rather than stringified", async () => {+ const { extractApiErrorMessage } = await load();++ assert.equal(+ extractApiErrorMessage({ message: { detail: "Rate limit exceeded" } }, "fallback"),+ "Rate limit exceeded"+ );+});++test("a detail string nested under error is extracted rather than stringified", async () => {+ const { extractApiErrorMessage } = await load();++ assert.equal(+ extractApiErrorMessage({ error: { detail: "Rate limit exceeded" } }, "fallback"),+ "Rate limit exceeded"+ );+});++test("a nested detail list beats a top-level detail restating the status", async () => {+ const { extractApiErrorMessage } = await load();++ const body = {+ detail: "Unprocessable Entity",+ message: { detail: [{ loc: ["body", "model"], msg: "field required" }] },+ };++ assert.equal(extractApiErrorMessage(body, "fallback"), "model: field required");+});++test("a top-level detail with nothing usable still surfaces the raw body", async () => {+ const { extractApiErrorMessage } = await load();++ assert.equal(+ extractApiErrorMessage({ detail: { reason: "upstream timeout" } }, "fallback"),+ '{"reason":"upstream timeout"}'+ );+});