Skip to content

Commit c616d15

Browse files
committed
Improve error handling
1 parent 2bd6f52 commit c616d15

File tree

3 files changed

+45
-18
lines changed

3 files changed

+45
-18
lines changed

app.js

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ let locale = url.searchParams.get("lang") || "de";
4343

4444

4545
const calendarContainer = document.getElementById("calendar");
46+
const errorBar = document.getElementById("errorbar");
4647
const sourceInfo = document.getElementById("sourceInfo");
4748
const yearSelect = document.getElementById("yearSelect");
4849
const countryList = document.getElementById("countryList");
@@ -74,9 +75,11 @@ document.addEventListener("DOMContentLoaded", async () => {
7475
renderCountrySelection();
7576
await updateCalendar();
7677
} catch (e) {
77-
calendarContainer.innerHTML = e.message + `<br/><a href=".">Reload page</a>`;
78+
errorBar.innerHTML = "Error: " + e.message + `<br/><a href=".">Reload page</a>`;
79+
errorBar.style.display = "block";
7880
throw e
7981
}
82+
errorBar.style.display = "none";
8083

8184
shareLinkButton.text = i18n.share
8285

@@ -238,26 +241,35 @@ async function fetchRegionData(countryCode) {
238241
// ------------------------------------------------------------
239242
// Aktualisiere Kalender
240243
async function updateCalendar() {
241-
const [fromStr, toStr] = selectedMonthRange.split("~");
242-
const fromDate = new Date(fromStr);
243-
let toDate = new Date(toStr);
244-
if (!fromDate || isNaN(fromDate) || !toDate || isNaN(toDate) || toDate < fromDate || toDate - fromDate > 2 * 365 * 24 * 60 * 60 * 1000)
245-
throw Error("Invalid date range " + selectedMonthRange);
246-
247-
// Lade Daten, falls noch nicht vorhanden
248-
const fetch = [];
249-
if (!populationData) fetch.push(fetchPopulationData());
250-
for (let country of selectedCountries) {
251-
if (!cachedData.Regions[country]) fetch.push(fetchRegionData(country));
252-
for (let year of [...new Set([fromDate.getFullYear(), toDate.getFullYear()])]) {
253-
if (!cachedData[year] || !cachedData[year][country]) fetch.push(fetchCountryData(year, country));
244+
try {
245+
const [fromStr, toStr] = selectedMonthRange.split("~");
246+
const fromDate = new Date(fromStr);
247+
let toDate = new Date(toStr);
248+
if (!fromDate || isNaN(fromDate) || !toDate || isNaN(toDate) || toDate < fromDate || toDate - fromDate > 2 * 365 * 24 * 60 * 60 * 1000)
249+
throw Error("Invalid date range " + selectedMonthRange);
250+
251+
// Lade Daten, falls noch nicht vorhanden
252+
const fetch = [];
253+
if (!populationData) fetch.push(fetchPopulationData());
254+
for (let country of selectedCountries) {
255+
if (!cachedData.Regions[country]) fetch.push(fetchRegionData(country));
256+
for (let year of [...new Set([fromDate.getFullYear(), toDate.getFullYear()])]) {
257+
if (!cachedData[year] || !cachedData[year][country]) fetch.push(fetchCountryData(year, country));
258+
}
254259
}
260+
await Promise.all(fetch);
261+
262+
// Daten aggregieren
263+
const stats = calculateDayStatistics(fromDate, toDate);
264+
renderCalendar(stats);
265+
266+
} catch (e) {
267+
errorBar.innerHTML = "Error: " + e.message + `<br/><a href=".">Reload page</a>`;
268+
errorBar.style.display = "block";
269+
throw e
255270
}
256-
await Promise.all(fetch);
271+
errorBar.style.display = "none";
257272

258-
// Daten aggregieren
259-
const stats = calculateDayStatistics(fromDate, toDate);
260-
renderCalendar(stats);
261273
}
262274

263275
// ------------------------------------------------------------

index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ <h1 data-i18n="title" onclick="document.location='.'">Holidense: Feriendichte Ka
2727
<div id="countryList"></div>
2828
</section>
2929

30+
<section id="errorbar">
31+
</section>
3032
<main id="calendar">
3133
Lade Daten...
3234
</main>

styles.css

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,15 @@ a:hover {
124124
border-color: #2196f3;
125125
}
126126

127+
#errorbar {
128+
margin: 1rem;
129+
padding: 1rem;
130+
display: none;
131+
font-family: monospace;
132+
border-radius: 1rem;
133+
background-color: LightCoral;
134+
}
135+
127136
#calendar {
128137
display: grid;
129138
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
@@ -232,6 +241,10 @@ footer {
232241
color: #7fb6ff;
233242
}
234243

244+
#errorbar {
245+
background: DarkRed;
246+
}
247+
235248
#controls, #countrySelect {
236249
background: #272b2e;
237250
border-bottom: 1px solid #444;

0 commit comments

Comments
 (0)