duplicated body read fix

This commit is contained in:
2025-12-03 13:04:47 +01:00
parent 841d79f26b
commit a696c914ce
2 changed files with 6 additions and 4 deletions

View File

@@ -40,8 +40,10 @@ async function request<T = any>(method: string, url: string, body?: any, extraIn
})
if (!res.ok) {
// Read the body once to avoid "Body has already been consumed" errors on non-JSON responses
const rawBody = await res.text().catch(() => '')
let details: any = undefined
try { details = await res.json() } catch { details = await res.text() }
try { details = rawBody ? JSON.parse(rawBody) : undefined } catch { details = rawBody }
const status = res.status
if (status === 401) throw makeError('Unauthorized', 'UNAUTHORIZED', status, details)
if (status === 403) throw makeError('Forbidden', 'FORBIDDEN', status, details)
@@ -114,4 +116,3 @@ export const chatbotApi = {
}).then(res => res.json())
}
}