Skip to content

Instantly share code, notes, and snippets.

@AndreiCalazans
Created April 3, 2022 13:49
Show Gist options
  • Save AndreiCalazans/b074b1e80411a00bae18d63680dd22d3 to your computer and use it in GitHub Desktop.
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.
// 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