Skip to content

Instantly share code, notes, and snippets.

@pemre
Created August 20, 2024 10:51
Show Gist options
  • Save pemre/03d06b27ecbc832c527982a2850d8f49 to your computer and use it in GitHub Desktop.
Save pemre/03d06b27ecbc832c527982a2850d8f49 to your computer and use it in GitHub Desktop.
const safeJsonParse = (str) => {
try {
return [null, JSON.parse(str)];
} catch (err) {
return [err];
}
};
// EXAMPLE USAGE
const checkEditorSession = (url, widget) => {
if (url && typeof url === "string") {
const decodedUrl = decodeURIComponent(url);
// console.log("HELLO! PAR", JSON.parse(decodedUrl));
const [err, json] = safeJsonParse(decodedUrl);
if (!err) {
console.log("Successfully parsed JSON:", json);
const jsonStr = JSON.stringify(json, null, 2);
get().setContents({ contents: jsonStr });
return useJson.setState({ json: jsonStr, loading: false });
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment