fix(web): body stream already read — Text zuerst lesen, dann JSON parsen
Some checks failed
CI / validate (push) Has been cancelled

res.json() konsumiert den Body-Stream auch bei SyntaxError, danach
schlägt res.text() mit 'body stream already read' fehl. Fix: text()
einmalig lesen, dann JSON.parse() versuchen.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Till JS 2026-05-10 17:07:24 +02:00
parent 26b136a42c
commit 333581c5ef

View file

@ -71,12 +71,9 @@ export async function apiForm<T>(path: string, form: FormData): Promise<T> {
const res = await fetch(`${API_BASE}${path}`, { method: 'POST', headers, body: form }); const res = await fetch(`${API_BASE}${path}`, { method: 'POST', headers, body: form });
if (!res.ok) { if (!res.ok) {
let body: unknown = null; const text = await res.text();
try { let body: unknown = text;
body = await res.json(); try { body = JSON.parse(text); } catch { /* keep text */ }
} catch {
body = await res.text();
}
throw new ApiError(res.status, body); throw new ApiError(res.status, body);
} }
return (await res.json()) as T; return (await res.json()) as T;
@ -105,12 +102,9 @@ export async function api<T>(path: string, opts: RequestOptions = {}): Promise<T
} }
if (!res.ok) { if (!res.ok) {
let body: unknown = null; const text = await res.text();
try { let body: unknown = text;
body = await res.json(); try { body = JSON.parse(text); } catch { /* keep text */ }
} catch {
body = await res.text();
}
throw new ApiError(res.status, body); throw new ApiError(res.status, body);
} }
return (await res.json()) as T; return (await res.json()) as T;