fix(frontend): apiFetchWithStatus body stream double-read crash

Per user report 2026-05-14: 'Failed to execute text on Response: body stream
already read' при analyze с non-JSON error response.

apiFetchWithStatus делал .json() (consumes stream), потом .text() в catch
(FAILS — stream already consumed). Fetch API: body — ReadableStream,
consumable один раз.

Fix: read text() once → JSON.parse on string. На fail (HTML / non-JSON)
оборачиваем в {detail: rawText}. Никаких double-reads.

Site-finder cad search больше не крашится на error pages.
This commit is contained in:
lekss361 2026-05-15 07:36:36 +03:00
parent 088c1ab2cf
commit 449be211f2

View file

@ -47,13 +47,17 @@ export async function apiFetchWithStatus<T>(
},
});
let body: unknown = null;
// 204 No Content имеет пустое тело; для всех остальных пытаемся JSON
// 204 No Content имеет пустое тело; для всех остальных читаем как text один раз
// (fetch body stream consumable только один раз — нельзя сначала .json() потом .text()).
if (response.status !== 204) {
try {
body = await response.json();
} catch {
// fallback на text() — может прилететь HTML error page от Caddy
body = { detail: await response.text() };
const rawText = await response.text();
if (rawText) {
try {
body = JSON.parse(rawText);
} catch {
// Не JSON (HTML error page от Caddy / 502 / etc) — оборачиваем в detail
body = { detail: rawText };
}
}
}
if (!response.ok) {