Skip to content

Commit

Permalink
Load json with try/catch, for malformed json
Browse files Browse the repository at this point in the history
  • Loading branch information
chungmin99 committed May 7, 2024
1 parent 1fe8437 commit 955e616
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/viser/client/src/components/PlotlyComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ const PlotWithAspect = React.memo(function PlotWithAspect({
}) {
// Parse json string, to construct plotly object.
// Note that only the JSON string is kept as state, not the json object.
const plotJson = JSON.parse(jsonStr);
// Also, need to catch if the jsonStr is not valid JSON.
let plotJson;
try {
plotJson = JSON.parse(jsonStr);
} catch (e) {
console.error("Failed to parse JSON string:", e);
return <div></div>;
}

// This keeps the zoom-in state, etc, see https://plotly.com/javascript/uirevision/.
plotJson.layout.uirevision = "true";
Expand Down

0 comments on commit 955e616

Please sign in to comment.