running-dashboard / src /utils /storage.js
lewtun's picture
lewtun HF Staff
Improve running dashboard error handling
ed11f02
Raw History Blame Contribute Delete
1.45 kB
async function request(path, options = {}) {
const response = await fetch(path, {
headers: {
'Content-Type': 'application/json',
...(options.headers || {}),
},
...options,
});
if (!response.ok) {
let message = `API ${response.status}: ${response.statusText}`;
const contentType = response.headers.get('content-type') || '';
try {
if (contentType.includes('application/json')) {
const payload = await response.json();
if (payload && typeof payload.error === 'string' && payload.error.trim()) {
message = payload.error.trim();
}
} else {
const text = await response.text();
if (text.trim()) {
message = text.trim();
}
}
} catch {
// Fall back to the HTTP status message when the error body is unreadable.
}
throw new Error(message);
}
if (response.status === 204) return null;
return response.json();
}
export async function loadRuns() {
return request('/api/runs');
}
export async function createRun(run) {
return request('/api/runs', {
method: 'POST',
body: JSON.stringify(run),
});
}
export async function updateRun(id, changes) {
return request(`/api/runs/${encodeURIComponent(id)}`, {
method: 'PUT',
body: JSON.stringify(changes),
});
}
export async function deleteRun(id) {
return request(`/api/runs/${encodeURIComponent(id)}`, {
method: 'DELETE',
});
}