Log non JSON API responses

This commit is contained in:
Mikei386
2026-06-23 07:04:43 +02:00
parent d7a62eb15e
commit 7cddfb19a4
7 changed files with 44 additions and 11 deletions
+18 -4
View File
@@ -826,10 +826,17 @@ function startResizeWindow(event, win) {
async function api(url, options) {
const response = await fetchChecked(url, options);
const text = await response.text();
try {
return await response.json();
return JSON.parse(text);
} catch (error) {
recordDebug('api.json.error', { url, error: serializeError(error) });
recordDebug('api.json.error', {
url,
status: response.status,
contentType: response.headers.get('content-type'),
body: text.slice(0, 2000),
error: serializeError(error)
});
throw error;
}
}
@@ -837,12 +844,19 @@ async function api(url, options) {
async function fetchChecked(url, options) {
recordDebug('fetch.request', { url, method: options?.method || 'GET' });
const response = await fetch(url, options);
recordDebug('fetch.response', { url, status: response.status, ok: response.ok });
recordDebug('fetch.response', {
url,
status: response.status,
ok: response.ok,
contentType: response.headers.get('content-type')
});
if (!response.ok) {
let message = `${response.status} ${response.statusText}`;
const text = await response.text();
try {
message = (await response.json()).error || message;
message = JSON.parse(text).error || message;
} catch {}
recordDebug('fetch.error.body', { url, status: response.status, body: text.slice(0, 2000) });
throw new Error(message);
}
return response;