mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-26 09:56:17 -05:00
Displaying plug icon when API fails to respond. Using window.apiDown to communicate to other hooks to stop fetching, the status interval will keep checking if the API is back up.
This commit is contained in:
parent
e7415873a5
commit
01338e4d89
7 changed files with 76 additions and 21 deletions
|
|
@ -20,22 +20,32 @@ function timeoutPromise(ms, promise) {
|
|||
})
|
||||
}
|
||||
|
||||
export class ApiDownError extends Error {
|
||||
}
|
||||
|
||||
const apiDownError = new ApiDownError('API is down');
|
||||
|
||||
async function apiCall(url, method, body, ms = 60_000) {
|
||||
let init = {method};
|
||||
if (body !== undefined) {
|
||||
init.body = JSON.stringify(body);
|
||||
}
|
||||
|
||||
// Create a fetch promise, this will be awaited by the timeout.
|
||||
let promise = fetch(url, init);
|
||||
|
||||
let promise;
|
||||
try {
|
||||
// Create a fetch promise, this will be awaited by the timeout.
|
||||
promise = fetch(url, init);
|
||||
|
||||
// await the response or error.
|
||||
const response = await timeoutPromise(ms, promise);
|
||||
if (200 <= response.status && response.status < 300) {
|
||||
// Request was successful.
|
||||
return response;
|
||||
}
|
||||
if (response.status === 502) {
|
||||
// API is down.
|
||||
throw apiDownError;
|
||||
}
|
||||
// Request encountered an error.
|
||||
let copy = response.clone();
|
||||
const content = await copy.json();
|
||||
|
|
@ -51,6 +61,10 @@ async function apiCall(url, method, body, ms = 60_000) {
|
|||
}
|
||||
return response;
|
||||
} catch (e) {
|
||||
if (e instanceof ApiDownError) {
|
||||
// Throw ApiDownError immediately without logging.
|
||||
throw e;
|
||||
}
|
||||
// Ignore SyntaxError because they happen when the API is down.
|
||||
if (!(e instanceof SyntaxError)) {
|
||||
console.error(e);
|
||||
|
|
@ -236,15 +250,6 @@ export async function downloadVideoMetadata(videoUrl, destination) {
|
|||
return await postDownload(downloadData);
|
||||
}
|
||||
|
||||
export async function getDirectories(search_str) {
|
||||
let form_data = {'search_str': search_str || null};
|
||||
const response = await apiPost(`${API_URI}/files/directories`, form_data);
|
||||
if (response.ok) {
|
||||
return await response.json();
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
export async function getStatus() {
|
||||
const response = await apiGet(`${API_URI}/status`);
|
||||
if (response.status === 200) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue