fix(web): body stream already read — Text zuerst lesen, dann JSON parsen
Some checks failed
CI / validate (push) Has been cancelled
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:
parent
26b136a42c
commit
333581c5ef
1 changed files with 6 additions and 12 deletions
|
|
@ -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 });
|
||||
|
||||
if (!res.ok) {
|
||||
let body: unknown = null;
|
||||
try {
|
||||
body = await res.json();
|
||||
} catch {
|
||||
body = await res.text();
|
||||
}
|
||||
const text = await res.text();
|
||||
let body: unknown = text;
|
||||
try { body = JSON.parse(text); } catch { /* keep text */ }
|
||||
throw new ApiError(res.status, body);
|
||||
}
|
||||
return (await res.json()) as T;
|
||||
|
|
@ -105,12 +102,9 @@ export async function api<T>(path: string, opts: RequestOptions = {}): Promise<T
|
|||
}
|
||||
|
||||
if (!res.ok) {
|
||||
let body: unknown = null;
|
||||
try {
|
||||
body = await res.json();
|
||||
} catch {
|
||||
body = await res.text();
|
||||
}
|
||||
const text = await res.text();
|
||||
let body: unknown = text;
|
||||
try { body = JSON.parse(text); } catch { /* keep text */ }
|
||||
throw new ApiError(res.status, body);
|
||||
}
|
||||
return (await res.json()) as T;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue