question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to handle error from JSON.parse

See original GitHub issue

The transformRequest method attempts to parse JSON, but swallows any error in an empty catch statement. This results in the stringified form of the response body to be returned should the JSON be malformed, and supplied to the response object that is used to resolve the Promise.

Without the try/catch a global error would occur. With the try/catch the code handling the response will error, but in an unpredictable way. Consider:

// Assume a malformed response from the server for the request.
// Missing quotes around property names:
//   {firstName: "Jimmy", lastName: "Page", isBlocked: true}
axios.get('/user/12345').then((res) => {
  var user = res.data;
  if (!user.isBlocked) {
    // Do something potentially dangerous that only an unblocked user should be allowed to do
  }
});

In this case user is a String not an Object as expected. Calling 'some string'.isBlocked will return undefined, which evaluates as falsey. This will cause the if condition to erroneously be entered.

A potential solution would be to reject the Promise if JSON.parse throws an Error. This isn’t a straight forward fix though as it would break #55 again.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:24
  • Comments:21 (2 by maintainers)

github_iconTop GitHub Comments

20reactions
youurayycommented, Dec 24, 2016

Just got burned on this. Instead of correctly rejecting the promise on invalid (truncated) JSON, it simply returns the orignal string object. Bad bad bad. At least put a warning in the docs about this.

14reactions
youurayycommented, Aug 9, 2018

@emilyemorehouse is this something you could fix, for the sake of all humanity?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Proper way to catch exception from JSON.parse - Stack Overflow
data = JSON.parse(response, function (key, value) { var type; if (value && typeof ...
Read more >
Proper way to catch exception from JSON.parse - Tutorialspoint
The best way to catch invalid JSON parsing errors is to put the calls to JSON.parse() to a try/catch block.
Read more >
SyntaxError: JSON.parse: bad parsing - JavaScript | MDN
The JavaScript exceptions thrown by JSON.parse() occur when string failed to be parsed as JSON.
Read more >
SyntaxError: JSON.parse: bad parsing Breaking Your Code ...
Traveling deftly through to the next item in our JavaScript Error Handling series, today we're taking a hard look at the JSON Parse...
Read more >
How to handle errors during Json parse? - Microsoft Q&A
How to handle errors during Json parse? · Dim jsonResponse As JsonNode · Using http As HttpClient = New HttpClient · Dim url...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found