Skip to content

Commit

Permalink
Improve error display
Browse files Browse the repository at this point in the history
  • Loading branch information
rzats committed Oct 11, 2024
1 parent d944baf commit 2453814
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions src/api/EpiData.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/restrict-template-expressions */
import UIkit from 'uikit';
import { appendIssueToTitle } from '../components/dialogs/utils';
import {
Expand Down Expand Up @@ -142,18 +143,42 @@ export function loadDataSet(
url.searchParams.set('format', 'json');
return fetchImpl<Record<string, unknown>[]>(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(
`
<div class="uk-alert uk-alert-error">
<a href="${url.href}">API Link</a> returned no data.
</div>`,
)
.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(
`
<div class="uk-alert uk-alert-error">
Failed to fetch API data from <a href="${url.href}">API Link</a>:<br/>${res['message']}
</div>`,
)
.then(() => null);
}
// Fallback for generic error
return UIkit.modal
.alert(
`
<div class="uk-alert uk-alert-error">
<a href="${url.href}">API Link</a> returned no data.
Failed to fetch API data from <a href="${url.href}">API Link</a>.
</div>`,
)
.then(() => null);
}
return data;
})
.catch((error) => {
console.warn('failed fetching data', error);
Expand Down

0 comments on commit 2453814

Please sign in to comment.