fix(frontend): don't treat empty 2xx bodies as request failures
The API client only skipped JSON parsing for 204, so a success with an empty body (e.g. 201 from POST /pools/:id/files) hit res.json() on an empty stream, threw, and surfaced as "Failed to add to pool" even though the add had committed. Read the body as text and parse only when present. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -128,8 +128,13 @@ async function request<T>(path: string, init?: RequestInit): Promise<T> {
|
|||||||
|
|
||||||
invalidateSectionCache(path, (init?.method ?? 'GET').toUpperCase());
|
invalidateSectionCache(path, (init?.method ?? 'GET').toUpperCase());
|
||||||
|
|
||||||
|
// A success doesn't guarantee a JSON body: 204 never has one, and some 200/201
|
||||||
|
// responses (e.g. POST /pools/:id/files) complete with an empty body. Parsing
|
||||||
|
// those as JSON throws, so read the text first and only parse when present —
|
||||||
|
// otherwise an empty 201 would surface as a spurious "failed" error.
|
||||||
if (res.status === 204) return undefined as T;
|
if (res.status === 204) return undefined as T;
|
||||||
return res.json();
|
const text = await res.text();
|
||||||
|
return (text ? JSON.parse(text) : undefined) as T;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Upload with XHR so we can track progress via onProgress(0–100). */
|
/** Upload with XHR so we can track progress via onProgress(0–100). */
|
||||||
|
|||||||
Reference in New Issue
Block a user