Use typed backend errors for translations

This commit is contained in:
n200534
2026-08-20 01:11:30 +05:30
parent eff8bb16a0
commit 3efec5ce58
38 changed files with 164 additions and 79 deletions

View File

@@ -1,6 +1,7 @@
import { i18n } from "@lingui/core"
import type { AxiosError } from "axios"
import { describe, expect, it } from "vitest"
import { loginErrorToStrings } from "./client"
import { afterEach, describe, expect, it, vi } from "vitest"
import { errorToStrings } from "./client"
const axiosError = (status: number, data: unknown) =>
({
@@ -8,16 +9,28 @@ const axiosError = (status: number, data: unknown) =>
response: { status, data },
}) as AxiosError
describe("loginErrorToStrings", () => {
it("uses the translated message for authentication errors", () => {
const error = axiosError(401, { message: "wrong username or password" })
describe("errorToStrings", () => {
afterEach(() => vi.restoreAllMocks())
expect(loginErrorToStrings(error, "Translated authentication error")).toEqual(["Translated authentication error"])
it("translates known application error types", () => {
vi.spyOn(i18n, "_").mockReturnValue("Translated authentication error")
const error = axiosError(401, {
type: "WRONG_USERNAME_OR_PASSWORD",
message: "wrong username or password",
})
expect(errorToStrings(error)).toEqual(["Translated authentication error"])
})
it("preserves backend messages for unexpected errors", () => {
const error = axiosError(500, { message: "unexpected error" })
expect(loginErrorToStrings(error, "Translated authentication error")).toEqual(["unexpected error"])
expect(errorToStrings(error)).toEqual(["unexpected error"])
})
it("preserves backend messages for unknown application error types", () => {
const error = axiosError(400, { type: "UNKNOWN_ERROR", message: "unknown error" })
expect(errorToStrings(error)).toEqual(["unknown error"])
})
})