Fix: add API request timeout to avoid stuck storage status
This commit is contained in:
25
app.js
25
app.js
@@ -636,9 +636,14 @@ function markLocalDataForImport() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function apiRequest(path, options = {}) {
|
async function apiRequest(path, options = {}) {
|
||||||
|
const controller = new AbortController();
|
||||||
|
const timeoutMs = options.timeoutMs || 6000;
|
||||||
|
const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
|
||||||
|
|
||||||
const requestOptions = {
|
const requestOptions = {
|
||||||
method: options.method || "GET",
|
method: options.method || "GET",
|
||||||
headers: {},
|
headers: {},
|
||||||
|
signal: controller.signal,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (options.body !== undefined) {
|
if (options.body !== undefined) {
|
||||||
@@ -646,16 +651,20 @@ async function apiRequest(path, options = {}) {
|
|||||||
requestOptions.body = JSON.stringify(options.body);
|
requestOptions.body = JSON.stringify(options.body);
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await fetch(path, requestOptions);
|
try {
|
||||||
const rawText = await response.text();
|
const response = await fetch(path, requestOptions);
|
||||||
const payload = rawText ? JSON.parse(rawText) : {};
|
const rawText = await response.text();
|
||||||
|
const payload = rawText ? JSON.parse(rawText) : {};
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const message = payload && payload.message ? payload.message : `HTTP ${response.status}`;
|
const message = payload && payload.message ? payload.message : `HTTP ${response.status}`;
|
||||||
throw new Error(message);
|
throw new Error(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
return payload;
|
||||||
|
} finally {
|
||||||
|
clearTimeout(timeoutId);
|
||||||
}
|
}
|
||||||
|
|
||||||
return payload;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function applyCatalogPayload(payload, preferredBrand, preferredConsole) {
|
function applyCatalogPayload(payload, preferredBrand, preferredConsole) {
|
||||||
|
|||||||
Reference in New Issue
Block a user