admin: unify response error extraction into errText helper

removes a double-body-read in the delete handlers and makes the four
alert sites consistent.
This commit is contained in:
müde 2026-06-14 22:30:49 +02:00
commit df513fda68

View file

@ -6,6 +6,15 @@ async function j<T = any>(res: Response): Promise<T> {
return res.json();
}
// Best-effort error message from a failed response ({ error } JSON or status text).
async function errText(res: Response): Promise<string> {
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('');