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

Invalid json in case of failed load #1175

Open
mcmorry opened this issue Apr 17, 2018 · 1 comment
Open

Invalid json in case of failed load #1175

mcmorry opened this issue Apr 17, 2018 · 1 comment

Comments

@mcmorry
Copy link
Contributor

mcmorry commented Apr 17, 2018

response = JSON.parse(xhr.responseText);

If I try to load a JSON file and I have a loading error (for example "No Access-Control-Allow-Origin header is present") the xhr status is 0 and is considered as a success. I don't know if this is correct, but supposing it is, the result of the get request is an empty string that cannot be parsed:

Uncaught SyntaxError: Unexpected end of JSON input
at JSON.parse ()
at Object._onSuccess (playcanvas-stable.dbg.js:20204)
at Object._onReadyStateChange (playcanvas-stable.dbg.js:20174)
at Object. (playcanvas-stable.dbg.js:20138)

So a simple patch would be to check for a not empty string before parsing, but I'm not sure if it's not better to understand why the xhr status zero is considered a successful request.
I had also an issue when the network was off. The get method doesn't return an error, but a successful empty result.

@mcmorry
Copy link
Contributor Author

mcmorry commented Apr 17, 2018

Also, I would probably add a try catch around the JSON.parse and notify the exception as an error, so that it can be better handled by the application.

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

1 participant