Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(web): show English error fallbacks before translations load

Andrej8aa9bb77ff87
diff --git a/apps/web/src/lib/http-error.test.ts b/apps/web/src/lib/http-error.test.ts
index 9fa402a94..91bac7d7e 100644
--- a/apps/web/src/lib/http-error.test.ts
+++ b/apps/web/src/lib/http-error.test.ts
@@ -1,7 +1,13 @@
+import i18n from "i18next";
import { describe, expect, it, vi } from "vite-plus/test";
import { HttpError, isUnauthorizedError } from "./http-error";
-vi.mock("i18next", () => ({ default: { t: (key: string) => key } }));
+vi.mock("i18next", async (importOriginal) => {
+ const { default: i18next } = await importOriginal<typeof import("i18next")>();
+ const instance = i18next.createInstance();
+ await instance.init({ lng: "en-US", resources: {} });
+ return { default: instance };
+});
describe("isUnauthorizedError", () => {
it("recognizes a 401 HttpError with a different prototype", () => {
@@ -96,7 +102,7 @@ describe("HttpError.fromResponse", () => {
);
expect(error.status).toBe(502);
- expect(error.message).toBe("common:error.messages.server");
+ expect(error.message).toBe("Server error. Please try again later.");
expect(error.code).toBeUndefined();
});
@@ -107,7 +113,7 @@ describe("HttpError.fromResponse", () => {
});
expect(error.status).toBe(500);
- expect(error.message).toBe("common:error.messages.server");
+ expect(error.message).toBe("Server error. Please try again later.");
});
it("falls back to the unknown error message for a blank 4xx body", async () => {
@@ -115,7 +121,7 @@ describe("HttpError.fromResponse", () => {
new Response(" \n", { status: 404 }),
);
- expect(error.message).toBe("common:error.messages.unknown");
+ expect(error.message).toBe("An unexpected error occurred.");
});
it("keeps the code when the JSON message is blank", async () => {
@@ -123,7 +129,36 @@ describe("HttpError.fromResponse", () => {
Response.json({ message: "", code: "CONFLICT" }, { status: 409 }),
);
- expect(error.message).toBe("common:error.messages.unknown");
+ expect(error.message).toBe("An unexpected error occurred.");
expect(error.code).toBe("CONFLICT");
});
+
+ it("uses the English fallback before translations load", async () => {
+ expect(i18n.hasResourceBundle("en-US", "common")).toBe(false);
+
+ const serverError = await HttpError.fromResponse(
+ new Response(null, { status: 500 }),
+ );
+ const unknownError = await HttpError.fromResponse(
+ new Response(null, { status: 400 }),
+ );
+
+ expect(serverError.message).toBe("Server error. Please try again later.");
+ expect(unknownError.message).toBe("An unexpected error occurred.");
+ });
+
+ it("uses the loaded translation once resources are available", async () => {
+ i18n.addResourceBundle("en-US", "common", {
+ error: { messages: { server: "Translated server error" } },
+ });
+ try {
+ const error = await HttpError.fromResponse(
+ new Response(null, { status: 503 }),
+ );
+
+ expect(error.message).toBe("Translated server error");
+ } finally {
+ i18n.removeResourceBundle("en-US", "common");
+ }
+ });
});
diff --git a/apps/web/src/lib/http-error.ts b/apps/web/src/lib/http-error.ts
index 255a0a2d8..2dd82005f 100644
--- a/apps/web/src/lib/http-error.ts
+++ b/apps/web/src/lib/http-error.ts
@@ -6,11 +6,14 @@ import {
} from "./parse-api-error-body";
function fallbackMessage(status: number): string {
- return i18n.t(
- status >= 500
- ? "common:error.messages.server"
- : "common:error.messages.unknown",
- );
+ if (status >= 500) {
+ return i18n.t("common:error.messages.server", {
+ defaultValue: "Server error. Please try again later.",
+ });
+ }
+ return i18n.t("common:error.messages.unknown", {
+ defaultValue: "An unexpected error occurred.",
+ });
}
export class HttpError extends Error {