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.

Handle json parse errors?

See original GitHub issue

When opt.json is truthy, errors on JSON.parse() are swallowed. https://github.com/Raynos/xhr/blob/bc9674d7d5f5c8870ab23fb736fbacc2b3f73518/index.js#L38-L40

Is there a recommended way of handling these errors while still supporting json: String option?

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
prawnsaladcommented, Feb 3, 2017

I’ve just been tripped up by this for a while. If I set json:true then I expect JSON to be returned, not a string. But due to a JSON parsing issue I ended up trying to iterate over the invalid JSON string causing headaches instead.

We should get an error stating that parsing the JSON failed. As it currently stands, we have to manually parse the JSON ourselves or check the type of response.body just to safeguard against it.

0reactions
naugturcommented, Feb 3, 2017

I know. That’s a behavior that was there long time ago and it’s a total breaking change, so requires a major release.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Proper way to catch exception from JSON.parse - Stack Overflow
i post something into an iframe then read back the contents of the iframe with json parse...so sometimes it's not a json string....
Read more >
How to handle invalid JSON parse error properly.
The most common way to handle JSON parse error is using try-catch block. If the JSON string is valid, it will return a...
Read more >
SyntaxError: JSON.parse: bad parsing - JavaScript | MDN
JSON.parse() parses a string as JSON. This string has to be valid JSON and will throw this error if incorrect syntax was encountered....
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 >
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 Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found