Attempted to add some more error handling, there is now a manual timeout for if the API server is not sending any response, but is still up.

This commit is contained in:
Ada Werefox 2024-04-16 18:01:42 -05:00
parent 09cdc465c4
commit 5e3115fb23
2 changed files with 96 additions and 38 deletions

View File

@ -1,42 +1,73 @@
async function refresh_overview(server_address) {
try {
const response = await fetch(`${server_address}overview`);
if (response.ok) {
const data = await response.json();
if (!("error" in data)) {
return { overview: data, error: null };
const response = await fetch_api(server_address, "overview");
if (response.data) {
return response.data;
} else {
return { overview: null, error: data };
}
if (response.error) {
return response.error;
} else {
return { overview: null, error: null };
return null;
}
} catch (e) {
return { overview: null, error: e };
}
}
async function refresh_hints(server_address) {
const response = await fetch(`${server_address}hints`);
const data = await response.json();
return data;
const response = await fetch_api(server_address, "hints");
if (response.data) {
return response.data;
} else {
return null;
}
}
async function refresh_checks(server_address) {
const response = await fetch(`${server_address}items`);
const data = await response.json();
return data;
const response = await fetch_api(server_address, "items");
if (response.data) {
return response.data;
} else {
return null;
}
}
async function refresh_entrances(server_address) {
const response = await fetch(`${server_address}doors`);
const data = await response.json();
return data;
const response = await fetch_api(server_address, "doors");
if (response.data) {
return response.data;
} else {
return null;
}
}
async function fetch_api(server_address, uri) {
try {
const response = await fetch(`${server_address}${uri}`, {
signal: AbortSignal.timeout(1100),
});
if (response.ok) {
const data = await response.json();
if (!("error" in data)) {
return { data: data, error: null };
} else {
return { data: null, error: data };
}
} else {
return { data: null, error: null };
}
} catch (e) {
return { data: null, error: e };
}
}
AbortSignal.timeout ??= function timeout(ms) {
const ctrl = new AbortController();
setTimeout(() => ctrl.abort(), ms);
return ctrl.signal;
};
export default {
refresh_overview,
refresh_checks,
refresh_entrances,
refresh_hints,
fetch_api,
};

View File

@ -11,7 +11,7 @@ var current_hints = 0;
// Global state internal
var server_address = "localhost:51111/";
var can_access_api_server = true;
var can_access_api_server = false;
var is_timeout = false;
var hide_completed_areas = false;
var cross_codes = {};
@ -22,7 +22,7 @@ var all_scenes = [];
window.onload = async () => {
await get_updated_server_address();
await parse_cross_codes();
await initialize_elements();
// await initialize_elements();
await refresh_elements();
};
@ -62,13 +62,23 @@ async function get_updated_server_address() {
async function initialize_elements() {
// Grab all updates from the backend.
let overview = await update.refresh_overview(server_address);
if (!overview.overview) {
if (!overview) {
return;
}
// console.log(overview)
const checks = await update.refresh_checks(server_address);
if (!checks) {
return;
}
const entrances = await update.refresh_entrances(server_address);
if (!entrances) {
return;
}
const hints = await update.refresh_hints(server_address);
overview = overview.overview;
if (!hints) {
return;
}
// Initialize global state
current_checks = checks.collected;
@ -111,12 +121,13 @@ async function initialize_elements() {
}
async function refresh_elements() {
const response = await update.refresh_overview(server_address);
try {
const response = await update.fetch_api(server_address, "overview");
// Check if the "Hide completed areas" option is checked
hide_completed_areas = document.getElementById("hideDone").checked;
if (response.overview) {
if (response.data) {
if (!can_access_api_server) {
console.info("I found the server!");
document.getElementById("status-block").classList.add("hidden");
@ -124,8 +135,11 @@ async function refresh_elements() {
await initialize_elements();
}
is_timeout = false;
await update_if_changes(response.overview);
} else if (response.error.error) {
await update_if_changes(response.data);
setTimeout(refresh_elements, 500)
} else if (response.error) {
setTimeout(refresh_elements, 1100)
if (response.error.error) {
if (!is_timeout) {
is_timeout = true;
console.debug("Received timeout from API server.");
@ -142,7 +156,20 @@ async function refresh_elements() {
}
can_access_api_server = false;
}
setTimeout(refresh_elements, 500);
} else {
if (can_access_api_server) {
console.debug("Could not access the API server.");
document.getElementById("status-block").classList.remove("hidden");
}
can_access_api_server = false;
setTimeout(refresh_elements, 1100)
}
} catch (error) {
setTimeout(refresh_elements, 1100)
}
}
async function update_if_changes(overview) {