Files
HealthLog/src/lib/api/read-error.ts
T
Marc-André Bombeck fea1d1e0b5 chore: strip planning tags from comments and enable the rule
Output of `node scripts/strip-planning-tags.mjs --write`: 3,157 comment
lines in 1,308 files lose the work-item identifier at their head. Release
versions and issue references stay; nothing outside comment text
changes, which the script checks per file against a comment-free
printout. healthlog/no-planning-tags is enabled at error level with this
commit, so the tree it lands on is clean.
2026-10-20 09:08:40 +02:00

28 lines
1.0 KiB
TypeScript

/**
* v1.4.25 — shared client-side error reader.
*
* The four onboarding components (WelcomeCarousel, GoalsChipPicker,
* SourceCardGrid, BaselineForm) all hand-rolled the same helper to
* extract the `error` string from a non-OK `apiError` envelope, with an
* identical fall-through to `Request failed (<status>)`. Hoisting the
* helper keeps the four call sites honest — the envelope shape only
* needs to evolve once.
*
* Contract:
* - The server-side `apiError` helper writes `{ data: null, error: "<msg>" }`.
* - On parse failure (network error, non-JSON body, malformed shape)
* fall back to a stable string the toast can surface.
* - Never throw — the caller is already in a catch / setError path.
*/
export async function readError(res: Response): Promise<string> {
try {
const json = (await res.json()) as { error?: string };
if (typeof json.error === "string" && json.error.length > 0) {
return json.error;
}
} catch {
/* fall through */
}
return `Request failed (${res.status})`;
}