From 781bdf8c749e2cde62a1b436c5745af61a4a7dad Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Mon, 17 May 2021 14:43:57 +0200 Subject: [PATCH] Fix error handling in mainpage and stats. --- static/mainpage.js | 11 ++++++++--- static/stats.js | 8 ++++++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/static/mainpage.js b/static/mainpage.js index 1f80c40..42f75dc 100644 --- a/static/mainpage.js +++ b/static/mainpage.js @@ -33,10 +33,15 @@ async function listPublicGroups() { let l; try { - l = await (await fetch('/public-groups.json')).json(); + let r = await fetch('/public-groups.json'); + if(!r.ok) + throw new Error(`${r.status} ${r.statusText}`); + l = await r.json(); } catch(e) { - console.error(e); - l = []; + table.textContent = `Couldn't fetch groups: ${e}`; + div.classList.remove('nogroups'); + div.classList.add('groups'); + return; } if (l.length === 0) { diff --git a/static/stats.js b/static/stats.js index bb67547..3746d96 100644 --- a/static/stats.js +++ b/static/stats.js @@ -25,10 +25,14 @@ async function listStats() { let l; try { - l = await (await fetch('/stats.json')).json(); + let r = await fetch('/stats.json'); + if(!r.ok) + throw new Error(`${r.status} ${r.statusText}`); + l = await r.json(); } catch(e) { console.error(e); - l = []; + table.textContent = `Couldn't fetch stats: ${e}`; + return; } if(l.length === 0) {