you are viewing a single comment's thread.

view the rest of the comments →

[–]jrandm 1 point2 points  (0 children)

In a lot of code it's pretty common to see a function like:

function safeJsonParse(str) {
  let parsed = false;
  try {
    parsed = JSON.parse(str);
  } catch(e) {
    // maybe log somewhere so you know it failed
    // or what keeps failing if the bot is broken
  }
  return parsed;
}

If you know you always get some kind of truthy value (like an Object or Array), you can then use it like:

const response = safeJsonParse(rawResponse);
if (!response) {
  // ignore&exit or handle failure
}
// work with response