Better error handling for when the server is inaccessible/down.

This commit is contained in:
Ada Werefox 2024-04-09 03:41:51 -05:00
parent c44506dbec
commit f024e1a1cf
2 changed files with 32 additions and 15 deletions

View File

@ -5,9 +5,12 @@ async function refresh_overview(server_address) {
const data = await response.json(); const data = await response.json();
if (!("error" in data)) { if (!("error" in data)) {
return { overview: data, error: null }; return { overview: data, error: null };
} else {
return { overview: null, error: data };
} }
} } else {
return { overview: null, error: null }; return { overview: null, error: null };
}
} catch (e) { } catch (e) {
return { overview: null, error: e }; return { overview: null, error: e };
} }

View File

@ -8,14 +8,16 @@ var current_checks = 0;
var current_entrances = 0; var current_entrances = 0;
var current_codes = Number.MAX_VALUE; var current_codes = Number.MAX_VALUE;
var current_hints = 0; var current_hints = 0;
var total_checks = 0;
var total_entrances = 0;
// Global state internal // Global state internal
var server_address = "localhost:51111/"; var server_address = "localhost:51111/";
var cross_codes = {};
var can_access_api_server = true; var can_access_api_server = true;
var is_timeout = false;
var hide_completed_areas = false; var hide_completed_areas = false;
var cross_codes = {};
var total_checks = 0;
var total_entrances = 0;
var all_scenes = [];
window.onload = async () => { window.onload = async () => {
await get_updated_server_address(); await get_updated_server_address();
@ -28,7 +30,7 @@ window.onload = async () => {
refresh_elements(); refresh_elements();
}, },
(error) => { (error) => {
console.log(error); console.error(error);
} }
); );
}; };
@ -38,7 +40,7 @@ async function get_updated_server_address() {
.then( .then(
(response) => response.json(), (response) => response.json(),
(error) => { (error) => {
console.log("Are you sure the front end is up?"); console.error(error);
} }
) )
.then( .then(
@ -47,7 +49,7 @@ async function get_updated_server_address() {
server_address = parsed_data["listen_address"]; server_address = parsed_data["listen_address"];
}, },
(error) => { (error) => {
console.log(error); console.error(error);
} }
); );
} }
@ -69,6 +71,7 @@ async function initialize_elements() {
current_entrances = entrances.found; current_entrances = entrances.found;
total_checks = checks.total; total_checks = checks.total;
total_entrances = entrances.total; total_entrances = entrances.total;
all_scenes = Array.from(Object.keys(checks.scenes));
await update_overview(overview, false); await update_overview(overview, false);
// Initialize breakdown list // Initialize breakdown list
@ -111,23 +114,34 @@ async function refresh_elements() {
if (response.overview) { if (response.overview) {
if (!can_access_api_server) { if (!can_access_api_server) {
console.log("I found the server!"); console.info("I found the server!");
document.getElementById("status-block").classList.add("hidden");
can_access_api_server = true; can_access_api_server = true;
await initialize_elements(); await initialize_elements();
} }
is_timeout = false;
await update_if_changes(response.overview); await update_if_changes(response.overview);
} else if (response.error.error) {
if (!is_timeout) {
is_timeout = true;
console.debug("Received timeout from API server.");
}
if (!can_access_api_server) {
console.info("I found the server!");
document.getElementById("status-block").classList.add("hidden");
can_access_api_server = true;
}
} else { } else {
if (can_access_api_server) { if (can_access_api_server) {
console.log("Could not access the API server."); console.debug("Could not access the API server.");
document.getElementById("status-block").classList.remove("hidden");
} }
can_access_api_server = false; can_access_api_server = false;
} }
setTimeout(refresh_elements, 2000); setTimeout(refresh_elements, 500);
} }
async function update_if_changes(overview) { async function update_if_changes(overview) {
document.getElementById("status-block").classList.add("hidden");
const changed_seed = overview.seed != current_seed; const changed_seed = overview.seed != current_seed;
const changed_scene = overview.scene != current_scene; const changed_scene = overview.scene != current_scene;
const changed_checks = overview.items != current_checks; const changed_checks = overview.items != current_checks;
@ -160,7 +174,7 @@ async function perform_updates(
) { ) {
if (changed_seed) { if (changed_seed) {
initialize_elements(); initialize_elements();
console.log(`Seed changed to: ${current_seed}`); console.info(`Seed changed to: ${current_seed}`);
} else { } else {
if (changed_checks) { if (changed_checks) {
update.refresh_checks(server_address).then((data) => { update.refresh_checks(server_address).then((data) => {
@ -203,7 +217,7 @@ async function update_overview(overview, changed_scene) {
current_entrances = overview.entrances; current_entrances = overview.entrances;
current_hints = overview.hints; current_hints = overview.hints;
if (changed_scene) { if (changed_scene && all_scenes.some((scene) => scene == overview.scene)) {
await update_scene(overview.scene); await update_scene(overview.scene);
} }
} }
@ -343,7 +357,7 @@ async function apply_summary_colors(summary) {
"text-highlight-empty-text", "text-highlight-empty-text",
"text-highlight-undiscovered-text" "text-highlight-undiscovered-text"
); );
if (summary.checks_total > 0 && summary.entrances_total > 0) { if (summary.checks_total > 0 || summary.entrances_total > 0) {
if ( if (
summary.checks_collected == summary.checks_total && summary.checks_collected == summary.checks_total &&
summary.entrances_found == summary.entrances_total summary.entrances_found == summary.entrances_total