diff --git a/src/api/EpiData.ts b/src/api/EpiData.ts index 1b3dfc5..aae1ac5 100644 --- a/src/api/EpiData.ts +++ b/src/api/EpiData.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/restrict-template-expressions */ import UIkit from 'uikit'; import { appendIssueToTitle } from '../components/dialogs/utils'; import { @@ -142,18 +143,42 @@ export function loadDataSet( url.searchParams.set('format', 'json'); return fetchImpl[]>(url) .then((res) => { - const data = loadEpidata(title, res, columns, columnRenamings, { _endpoint: endpoint, ...params }); - if (data.datasets.length == 0) { + try { + const data = loadEpidata(title, res, columns, columnRenamings, { _endpoint: endpoint, ...params }); + if (data.datasets.length == 0) { + return UIkit.modal + .alert( + ` +
+ API Link returned no data. +
`, + ) + .then(() => null); + } + return data; + } catch (error) { + console.warn('failed loading data', error); + // EpiData API error - JSON with "message" property + if ('message' in res) { + return UIkit.modal + .alert( + ` +
+ Failed to fetch API data from API Link:
${res['message']} +
`, + ) + .then(() => null); + } + // Fallback for generic error return UIkit.modal .alert( `
- API Link returned no data. + Failed to fetch API data from API Link.
`, ) .then(() => null); } - return data; }) .catch((error) => { console.warn('failed fetching data', error);