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

Better error when trying to .json a Resource that's not JSON. #47

Closed
matthew-andrews opened this issue Dec 24, 2014 · 5 comments
Closed

Comments

@matthew-andrews
Copy link
Contributor

It would be nice if there were a better error when trying to .json a Resource that's not JSON.

Currently in two implementations (this polyfill and Chrome Service Workers) trying to fetch and .json() a non JSON resource throws a not particularly helpful error:-

fetch("//some.thing/not-a-json-resource")
  .then(function(res) {
    return res.json();
  })
  .catch(function(err) {
    console.log(err); // => Unexpected token c
  });

screen shot 2014-12-24 at 12 36 59

It would make for more less unpleasant debugging / error aggregation if it threw an error along the lines of “Failed to parse {{url}} as JSON”… or something like that…

I know errors aren't spec'd as such so it's not something that should/could be fixed in the spec — but I will raise as an issue in Chrome as well so that GitHub & Chrome's fetches can stay aligned but I think it's something that should be improved and I'm sure you have an idea of what it might look like…

@matthew-andrews
Copy link
Contributor Author

@josh
Copy link
Contributor

josh commented Dec 24, 2014

Yeah, that's kinda lame.

Thanks for opening the issue on the Chrome side. I was just recently trying
to align us to any Chrome differences.
On Wed, Dec 24, 2014 at 6:46 AM Matt Andrews notifications@github.com
wrote:

Chrome Bug here:
https://code.google.com/p/chromium/issues/detail?id=445041&thanks=445041&ts=1419425155


Reply to this email directly or view it on GitHub
#47 (comment).

@josh
Copy link
Contributor

josh commented Jan 13, 2015

Have any specific ideas for how we could improve this? Or should we just wait it out until Chrome makes the first move?

@dgraham
Copy link
Contributor

dgraham commented Jan 13, 2015

I think this error is pretty good. "SyntaxError" and "unexpected token" are good clues that's it's invalid JSON. Then you can inspect the response and see it's HTML or other non-JSON data.

@josh josh closed this as completed Jan 13, 2015
@matthew-andrews
Copy link
Contributor Author

for what it's worth i'm playing with ideas here:-
https://github.com/matthew-andrews/fetchres#usage-node

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants