From fa785a726c0888a77bce761f1f37bf648e1cdc6b Mon Sep 17 00:00:00 2001 From: Tom Russell Date: Wed, 2 Oct 2019 22:13:34 +0100 Subject: [PATCH] Add JSON.parse error-handling when parsing from URL --- app/src/frontend/building/multi-edit.tsx | 15 ++++++++++++++- app/src/frontend/map-app.tsx | 12 +++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/app/src/frontend/building/multi-edit.tsx b/app/src/frontend/building/multi-edit.tsx index a408153b..81617bae 100644 --- a/app/src/frontend/building/multi-edit.tsx +++ b/app/src/frontend/building/multi-edit.tsx @@ -34,7 +34,20 @@ const MultiEdit = (props) => { } const q = parse(props.location.search); - const data = JSON.parse(q.data as string) // TODO: verify what happens when data is string[] + + let data: object; + if (cat === 'like'){ + data = { like: true } + } else { + try { + // TODO: verify what happens if data is string[] + data = JSON.parse(q.data as string); + } catch (error) { + console.error(error, q) + data = {} + } + } + const title = sectionTitleFromCat(cat); return ( diff --git a/app/src/frontend/map-app.tsx b/app/src/frontend/map-app.tsx index 9f77ee99..5191f3fe 100644 --- a/app/src/frontend/map-app.tsx +++ b/app/src/frontend/map-app.tsx @@ -142,11 +142,17 @@ class MapApp extends React.Component { colourBuilding(building) { const cat = this.props.match.params.category; const q = parse(window.location.search); - const data = (cat === 'like') ? { like: true } : JSON.parse(q.data as string); // TODO: verify what happens if data is string[] + if (cat === 'like') { this.likeBuilding(building.building_id) } else { - this.updateBuilding(building.building_id, data) + try { + // TODO: verify what happens if data is string[] + const data = JSON.parse(q.data as string); + this.updateBuilding(building.building_id, data) + } catch (error) { + console.error(error, q) + } } } @@ -245,4 +251,4 @@ class MapApp extends React.Component { } } -export default MapApp; \ No newline at end of file +export default MapApp;