From bc37c5dbffbf09bda2a4e202b82e1ebd7ce3cab8 Mon Sep 17 00:00:00 2001 From: Vesa Meskanen Date: Thu, 5 Dec 2024 08:51:28 +0200 Subject: [PATCH] fix: catch geoJson fetch errors --- app/component/map/withGeojsonObjects.js | 4 ++- app/store/GeoJsonStore.js | 46 +++++++++++++++---------- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/app/component/map/withGeojsonObjects.js b/app/component/map/withGeojsonObjects.js index eb90772401..8c661336a1 100644 --- a/app/component/map/withGeojsonObjects.js +++ b/app/component/map/withGeojsonObjects.js @@ -45,7 +45,9 @@ function withGeojsonObjects(Component) { ); const newGeoJson = {}; json.forEach(({ url, data, isOffByDefault }) => { - newGeoJson[url] = { ...data, isOffByDefault }; + if (data) { + newGeoJson[url] = { ...data, isOffByDefault }; + } }); updateGeoJson(newGeoJson); } diff --git a/app/store/GeoJsonStore.js b/app/store/GeoJsonStore.js index c805e27497..b4a553db2b 100644 --- a/app/store/GeoJsonStore.js +++ b/app/store/GeoJsonStore.js @@ -68,12 +68,15 @@ class GeoJsonStore extends Store { if (!url) { return undefined; } - if (!this.layers) { - const response = await getJson(url); - const root = response.geoJson || response.geojson; - if (root && Array.isArray(root.layers)) { - this.layers = root.layers; + try { + const response = await getJson(url); + const root = response.geoJson || response.geojson; + if (root && Array.isArray(root.layers)) { + this.layers = root.layers; + } + } catch (error) { + this.layers = []; } } @@ -104,23 +107,28 @@ class GeoJsonStore extends Store { } if (!this.geoJsonData[id]) { this.geoJsonData[id] = 'pending'; - const responses = await Promise.all(urlArr.map(u => getJson(u))); - const mapped = responses.map(r => { - if (metadata) { - MapJSON(r, metadata); + try { + const responses = await Promise.all(urlArr.map(u => getJson(u))); + const mapped = responses.map(r => { + if (metadata) { + MapJSON(r, metadata); + } + return styleFeatures(r); + }); + for (let i = 1; i < mapped.length; i++) { + mapped[0].features = mapped[0].features.concat(mapped[i].features); } - return styleFeatures(r); - }); - for (let i = 1; i < mapped.length; i++) { - mapped[0].features = mapped[0].features.concat(mapped[i].features); + const data = { + name: name || id, + data: mapped[0], + }; + this.geoJsonData[id] = data; + } catch (error) { + // store non falsy value to avoid new fetch + this.geoJsonData[id] = {}; } - const data = { - name: name || id, - data: mapped[0], - }; - this.geoJsonData[id] = data; } - return { ...this.geoJsonData[id] }; + return this.geoJsonData[id].name ? { ...this.geoJsonData[id] } : null; }; }