Created
April 3, 2022 13:49
-
-
Save AndreiCalazans/b074b1e80411a00bae18d63680dd22d3 to your computer and use it in GitHub Desktop.
Unexpected token U in JSON at position 0 is a very common error that happens in JavaScript due to JSON.parse trying to parse an undefined value, always try-catch JSON.parse.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Unexpected token U in JSON at position 0 is a very common error that happens in JavaScript due to JSON.parse trying to parse an undefined value, always try-catch JSON.parse. | |
export function parseJsonAndLogIfError( | |
data: string, | |
context: string, | |
): unknown { | |
try { | |
return JSON.parse(data); | |
} catch (e) { | |
Bugsnag.notify(`Tried to JSON parse invalid value: ${data} in ${context}`); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment