fix(forms): show the form's own sentence when a save is refused

The custom metric, allergy, vaccination, booster, visit, address-book,
workout and quick-add dose forms printed the server's error string as
it came: English in every locale, and for a network failure the
browser's own TypeError text. They now resolve the error code through
localizedApiError like the other entry forms, so a known refusal reads
in the person's language and anything else falls back to the form's
"could not save" sentence. A custom metric whose name is taken keeps
its specific message, now translated.

The workout form's browser test expected the server prose and now
expects the form's sentence.
This commit is contained in:
Marc-André Bombeck committed 2026-11-11 19:57:06 +01:00
1 parent 7001308636
commit 121b23f4a2
21 files changed
+57 -52

No files matched your search

@@ -128,9 +128,10 @@ describe("<ManualWorkoutForm>", () => {
await pickSport("Running");
await screen.getByLabelText("Minutes").fill("30");
await save.click();
// The form's own sentence, never the server's English prose.
await expect
.element(screen.getByRole("alert"))
.toHaveTextContent("Service unavailable");
.toHaveTextContent("The workout could not be saved");
expect(onSuccess).not.toHaveBeenCalled();
api.on("POST", BATCH, {
@@ -21,7 +21,8 @@ import {
} from "@/components/ui/select";
import { useAuth } from "@/hooks/use-auth";
import { useUnitDisplay } from "@/hooks/use-unit-display";
import { ApiError, apiPost } from "@/lib/api/api-fetch";
import { apiPost } from "@/lib/api/api-fetch";
import { localizedApiError } from "@/lib/api/localized-error";
import { useTranslations } from "@/lib/i18n/context";
import { getQuantityTransform } from "@/lib/measurements/display-transform";
import {
@@ -232,9 +233,7 @@ export function ManualWorkoutForm({
onSuccess?.();
} catch (err) {
setSaveError(
err instanceof ApiError && err.message
? err.message
: t("insights.workouts.manual.saveError"),
localizedApiError(err, t, "insights.workouts.manual.saveError"),
);
} finally {
setSaving(false);