From 64afa0426d9aa4a8b9a896b467bf7bdf91839c91 Mon Sep 17 00:00:00 2001 From: Rostyslav Zatserkovnyi Date: Thu, 21 Nov 2024 20:47:25 +0200 Subject: [PATCH] Review fixes --- src/api/EpiData.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/api/EpiData.ts b/src/api/EpiData.ts index 64fa04d..e447acd 100644 --- a/src/api/EpiData.ts +++ b/src/api/EpiData.ts @@ -48,7 +48,13 @@ export function fetchImpl(url: URL): Promise { const urlGetS = url.toString(); if (urlGetS.length < 4096) { // eslint-disable-next-line @typescript-eslint/no-unsafe-return - return fetch(url.toString(), fetchOptions).then((d) => d.json()); + return fetch(url.toString(), fetchOptions).then((d) => { + try { + return d.json(); + } catch (error) { + throw new Error(`[${d.status}] ${d.text()}`); + } + }); } const params = new URLSearchParams(url.searchParams); url.searchParams.forEach((d) => url.searchParams.delete(d)); @@ -57,7 +63,13 @@ export function fetchImpl(url: URL): Promise { ...fetchOptions, method: 'POST', body: params, - }).then((d) => d.json()); + }).then((d) => { + try { + return d.json(); + } catch (error) { + throw new Error(`[${d.status}] ${d.text()}`); + } + }); } // generic epidata loader @@ -155,7 +167,7 @@ export function loadDataSet( .alert( `
- API Link returned no data. + API Link returned no data, which suggests that the API has no available information for the selected location.
`, ) .then(() => null);