Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle json parse errors? #91

Open
mattdesl opened this issue Sep 1, 2015 · 6 comments
Open

Handle json parse errors? #91

mattdesl opened this issue Sep 1, 2015 · 6 comments
Milestone

Comments

@mattdesl
Copy link

mattdesl commented Sep 1, 2015

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?

@TehShrike
Copy link
Collaborator

I can understand why you might want to return the body as-is in that case - how even would you report these kinds of errors? Have an event emitter somewhere that would emit parse errors or something? I'm not sure what I would want (but I can agree I would probably want to see something if this happened).

@mattdesl
Copy link
Author

mattdesl commented Jun 1, 2016

I would assume just return Error in first callback, maybe with a code that can be used to identify the issue?

Or: just leave it as is but document it and encourage users to JSON.parse themselves if they want to be safe.

@TehShrike
Copy link
Collaborator

It would be really weird to me if it returned an error as the first argument to the callback, but then also included the un-parsed body of the response.

Right now it just returns the un-parsed body of the response.

@naugtur
Copy link
Owner

naugtur commented Jun 4, 2016

IT would actually make sense to do that to give the consumer a chance to recover, but if the server returns invalid JSON with JSON content-type, there's no good way to proceed.

Anybody willing to compare with https://github.com/request/request?

@prawnsalad
Copy link

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.

@naugtur
Copy link
Owner

naugtur commented 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.

@naugtur naugtur added this to the v3.0 milestone Feb 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants