mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 15:42:24 +00:00
fix(i18n): say "sign in again" or "too many requests" on a refused save
A save after the session ran out, or into a rate limit, showed the form's generic "could not save". The server already names both refusals in meta.errorCode (auth.missing for an ended web session, the status codes http.rate_limited, http.forbidden and http.invalid otherwise), but the catalogs had no sentence for them, so the lookup fell through to the fallback. They have one now in all ten locales, and a refusal that reaches the client without a code (a reverse proxy's 429) is read by its status the way the server labels it.
This commit is contained in:
1 parent
4721914cac
commit
0639d28f6a
12 files changed
+137
-15
No files matched your search
@@ -113,4 +113,51 @@ describe("localizedApiError", () => {
|
||||
),
|
||||
).toBe(translator(locale as Catalog)("measurements.saveError"));
|
||||
});
|
||||
|
||||
describe("a refusal the client can act on", () => {
|
||||
const enT = translator(en as Catalog);
|
||||
|
||||
it.each([
|
||||
["an ended session", 401, "auth.missing", "Sign in again"],
|
||||
["a rate limit", 429, "http.rate_limited", "too many requests"],
|
||||
["a missing right", 403, "http.forbidden", "permission"],
|
||||
["a rejected entry", 422, "http.invalid", "Check them"],
|
||||
])("names %s instead of the save fallback", (_, status, code, text) => {
|
||||
const error = new ApiError("raw English server prose", status, {
|
||||
errorCode: code,
|
||||
});
|
||||
expect(localizedApiError(error, enT, "measurements.saveError")).toMatch(
|
||||
new RegExp(text, "i"),
|
||||
);
|
||||
});
|
||||
|
||||
it("reads a refusal without a code by its status", () => {
|
||||
// A reverse proxy's 429 carries no envelope at all.
|
||||
const error = new ApiError("Request failed (429)", 429);
|
||||
expect(localizedApiError(error, enT, "measurements.saveError")).toBe(
|
||||
enT("apiErrors.http.rate_limited"),
|
||||
);
|
||||
});
|
||||
|
||||
it.each([
|
||||
["de", de],
|
||||
["es", es],
|
||||
["fr", fr],
|
||||
["it", itCatalog],
|
||||
["pl", pl],
|
||||
])("has its own sentence in %s", (_, locale) => {
|
||||
const localT = translator(locale as Catalog);
|
||||
for (const code of [
|
||||
"auth.missing",
|
||||
"http.unauthenticated",
|
||||
"http.rate_limited",
|
||||
"http.forbidden",
|
||||
"http.invalid",
|
||||
]) {
|
||||
const sentence = localT(`apiErrors.${code}`);
|
||||
expect(sentence).not.toBe(`apiErrors.${code}`);
|
||||
expect(sentence).not.toBe(enT(`apiErrors.${code}`));
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,21 +1,26 @@
|
||||
import { ApiError } from "./api-fetch";
|
||||
import { defaultErrorCodeForStatus } from "./default-error-codes";
|
||||
import { translateCode } from "@/lib/i18n/translate-code";
|
||||
|
||||
type Translate = (key: string) => string;
|
||||
|
||||
/**
|
||||
* Resolve an API envelope's stable error code through the locale catalog.
|
||||
* Missing/unknown codes, network failures, and non-API errors deliberately
|
||||
* collapse to the caller's safe domain fallback; server prose is never shown.
|
||||
* A refusal without a code of its own (a proxy's 429, an answer without the
|
||||
* envelope) is read by its status, the way the server labels one, so an
|
||||
* ended session or a rate limit is named as such. Unknown codes, network
|
||||
* failures, and non-API errors deliberately collapse to the caller's safe
|
||||
* domain fallback; server prose is never shown.
|
||||
*/
|
||||
export function localizedApiError(
|
||||
error: unknown,
|
||||
t: Translate,
|
||||
fallbackKey: string,
|
||||
): string {
|
||||
const errorCode =
|
||||
error instanceof ApiError && typeof error.meta?.errorCode === "string"
|
||||
const errorCode = !(error instanceof ApiError)
|
||||
? null
|
||||
: typeof error.meta?.errorCode === "string"
|
||||
? error.meta.errorCode
|
||||
: null;
|
||||
: defaultErrorCodeForStatus(error.status);
|
||||
return translateCode(t, "apiErrors", errorCode, fallbackKey);
|
||||
}
|
||||
Reference in new issue
Block a user