Skip to content

Commit

Permalink
fix: catch geoJson fetch errors
Browse files Browse the repository at this point in the history
  • Loading branch information
vesameskanen committed Dec 5, 2024
1 parent 7d0a8b8 commit bc37c5d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
4 changes: 3 additions & 1 deletion app/component/map/withGeojsonObjects.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
46 changes: 27 additions & 19 deletions app/store/GeoJsonStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
}
}

Expand Down Expand Up @@ -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;
};
}

Expand Down

0 comments on commit bc37c5d

Please sign in to comment.