playcanvas / playcanvas/engine

Invalid json in case of failed load

Open
#1,175 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
16.8k
Forks
2k
Avg merge
4h 32m
Merged PRs (30d)
222

Description

https://github.com/playcanvas/engine/blob/18bdc36d108031beca3fbfab948feb911eea2159/src/net/http.js#L372

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/net/http.js at line 372 and reproduce a JSON load with a CORS failure or the network offline. Trace how an XHR status of 0 and an empty response are handled before JSON.parse. Done means failed loads report an error instead of being treated as successful empty JSON results.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
networking
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.