From 7a1b27ecc9d0df378045ef743a4e73c51564870b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20Boduro=C4=9Flu?= <80620802+burakboduroglu@users.noreply.github.com> Date: Tue, 28 Jul 2026 18:49:13 +0300 Subject: [PATCH] =?UTF-8?q?fix(api):=20=F0=9F=9A=AB=20sanitize=20unmapped?= =?UTF-8?q?=20error=20messages=20=E2=80=94=20no=20raw=20backend=20text=20l?= =?UTF-8?q?eak?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - replace sentenceCase fallback with safe localized generic - avoids exposing raw English/technical backend errors to users - update tests to match new messages --- src/api/__tests__/client/client.test.ts | 14 +++++++++----- src/api/client/client.ts | 8 ++++++-- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/api/__tests__/client/client.test.ts b/src/api/__tests__/client/client.test.ts index a0b1435..a2ac1d0 100644 --- a/src/api/__tests__/client/client.test.ts +++ b/src/api/__tests__/client/client.test.ts @@ -242,9 +242,13 @@ describe("client", () => { ).toBe("Bu sınava yalnızca dersin öğretmenleri veya bir müdür soru ekleyebilir."); }); - it("handles unknown messages with sentence case", () => { - expect(formatApiErrorMessage("custom error occurred", "en")).toBe("Custom error occurred"); - expect(formatApiErrorMessage("custom error occurred", "tr")).toBe("İşlem tamamlanamadı: Custom error occurred"); + it("falls back to a clean localized message for unmapped errors (no raw leak)", () => { + expect(formatApiErrorMessage("custom error occurred", "en")).toBe( + "Something went wrong. Please check your input and try again.", + ); + expect(formatApiErrorMessage("custom error occurred", "tr")).toBe( + "İşlem tamamlanamadı. Lütfen bilgileri kontrol edip tekrar dene.", + ); }); it("localizes backend max-length validation messages", () => { @@ -330,8 +334,8 @@ describe("client", () => { }); it("formats generic Error", () => { - expect(formatApiError(new Error("generic"), "en")).toBe("Generic"); - expect(formatApiError(new Error("generic"), "tr")).toBe("İşlem tamamlanamadı: Generic"); + expect(formatApiError(new Error("generic"), "en")).toBe("Something went wrong. Please check your input and try again."); + expect(formatApiError(new Error("generic"), "tr")).toBe("İşlem tamamlanamadı. Lütfen bilgileri kontrol edip tekrar dene."); }); it("does not expose network, runtime, or provider errors to users", () => { diff --git a/src/api/client/client.ts b/src/api/client/client.ts index e0e7363..35a31ae 100644 --- a/src/api/client/client.ts +++ b/src/api/client/client.ts @@ -477,8 +477,12 @@ export function formatApiErrorMessage(message: string, locale: Locale = currentL if (runtime) return runtime; const validation = formatValidationMessage(message, locale); if (validation) return validation; - if (locale === "tr") return `İşlem tamamlanamadı: ${sentenceCase(message)}`; - return sentenceCase(message); + // Unmapped backend text is raw English/technical — never surface it to the + // user. Fall back to a clean localized line; add a mapping in + // API_ERROR_MESSAGES when a specific message deserves its own wording. + return locale === "tr" + ? "İşlem tamamlanamadı. Lütfen bilgileri kontrol edip tekrar dene." + : "Something went wrong. Please check your input and try again."; } export function formatApiError(err: unknown, locale: Locale = currentLocale()): string {