diff --git a/client/src/admin/Admin.tsx b/client/src/admin/Admin.tsx index 7a7a38b..81884d3 100644 --- a/client/src/admin/Admin.tsx +++ b/client/src/admin/Admin.tsx @@ -6,6 +6,15 @@ async function j(res: Response): Promise { return res.json(); } +// Best-effort error message from a failed response ({ error } JSON or status text). +async function errText(res: Response): Promise { + try { + return (await res.json()).error ?? res.statusText; + } catch { + return res.statusText; + } +} + interface Drink { id: number; name: string; price_cents: number; archived: number } interface BarRow { id: number; name: string; pfand_cents: number; drink_ids: number[] } interface Totals { bar_id: number; bar_name: string; tx_count: number; paid_cents: number; crew_count: number; pfand_returns: number } @@ -189,7 +198,7 @@ function Drinks() { async function del(d: Drink) { if (!confirm(`Getränk „${d.name}“ wirklich löschen?`)) return; const res = await fetch(`/admin/api/drinks/${d.id}`, { method: 'DELETE' }); - if (!res.ok) { alert(`Fehler: ${(await res.json().catch(() => null))?.error ?? await res.text()}`); return; } + if (!res.ok) { alert(`Fehler: ${await errText(res)}`); return; } reload(); } @@ -337,14 +346,14 @@ function Bars() { headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), }); - if (!res.ok) alert(`Fehler: ${await res.text()}`); + if (!res.ok) alert(`Fehler: ${await errText(res)}`); reload(); } async function delBar(b: BarRow) { if (!confirm(`Tresen „${b.name}“ wirklich löschen?`)) return; const res = await fetch(`/admin/api/bars/${b.id}`, { method: 'DELETE' }); - if (!res.ok) { alert(`Fehler: ${(await res.json().catch(() => null))?.error ?? await res.text()}`); return; } + if (!res.ok) { alert(`Fehler: ${await errText(res)}`); return; } reload(); } @@ -357,7 +366,7 @@ function Bars() { body: JSON.stringify({ name, pfand_cents: 200 }), }); if (!res.ok) { - alert(`Fehler: ${await res.text()}`); + alert(`Fehler: ${await errText(res)}`); return; } setNewName('');