viewer: say the world file could not be read when the body read fails, not that the JSON is invalid (WORLD-JSON-READ-MISREPORTED) #918

Open
github-bot wants to merge 1 commit from github-bot/gh-918 into main AGit
Owner

@⁠TuroYT on GitHub · 2026-11-24 15:11 UTC · original
TuroYT:fix/viewer-world-json-read-error → main · +28 −9 · 2 files · 1 commits · open

Claimed in the coordination issue as: WORLD-JSON-READ-MISREPORTED (#14)

What was wrong

viewer/index.html, loadWorld() (line 119 before this change): world = await response.json(); ran inside the same try as the fetch. The catch chose its message from response, which is set once the fetch has returned. So when the status was 200 and the body read failed (for example a dropped connection), the viewer said world/world.json could not be read as valid JSON. The file may be valid, and the failure was the network.

Reproduction on main (a28927849), with a stubbed fetch and the function read from the file in memory:

200 + body read fails -> "world/world.json could not be read as valid JSON."
no response          -> "world/world.json could not be loaded."

What changed

  • loadWorld() reads the body with response.text() and parses it with JSON.parse as two separate steps. A stage variable records which step was running when the error was thrown.
  • The catch now returns:
    • ... could not be read. when the read failed,
    • ... could not be read as valid JSON. when the parse failed,
    • ... could not be loaded. when the fetch failed (unchanged),
    • ... download timed out. when the deadline fired (unchanged).
  • Test mocks that provided json: now provide text:. The test sandbox gets the host JSON, so parsed arrays compare as before.
  • New cases: a read that throws after a 200 response expects could not be read. and not valid JSON; a valid body still loads.

Overlap with open pull requests

  • #651 (VIEWER-CHUNK-IDLE-TIMEOUT) edits loadChunk in viewer/index.html and adds fixtures and tests after line 456 of the test file. This pull request does not touch those hunks. Its loadWorld hunk is at lines 110-130, and its test hunks are at lines 21-22, 68-95 and 110.
  • #93 changes one camera speed test in the test file. No overlap.
  • No other open pull request touches these two files for loadWorld.

Tested

node --test tests/test_viewer_streaming.cjs        # node v24.22.0
# tests 59, pass 59, fail 0
python3 -I tests/test_text_write_line_endings.py   # Ran 3 tests, OK
python3 -I scripts/check_no_game_data.py viewer/index.html tests/test_viewer_streaming.cjs   # exit 0
git diff --check                                   # no output

The new cases fail on the unfixed viewer/index.html (the main version, run with the new test file):

node --test --test-name-pattern="read failure|body read that fails|valid manifest body still loads|test mode, a missing" tests/test_viewer_streaming.cjs
# tests 3, pass 1, fail 2
# AssertionError: 'world/world.json could not be read as valid JSON.' does not match /could not be read\./

The valid-body case also fails on the unfixed code, because its mock provides only text() and the old code calls json().

Not tested: a real browser. The viewer was not served or opened, so no page load, console or network check was done. The change is inside loadWorld(), which runs once at startup. No GPU, no game files.

AI-assisted (Claude Code).

**@⁠TuroYT** on GitHub · 2026-11-24 15:11 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/918) `TuroYT:fix/viewer-world-json-read-error` → `main` · +28 −9 · 2 files · 1 commits · open Claimed in the coordination issue as: WORLD-JSON-READ-MISREPORTED (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14) ## What was wrong `viewer/index.html`, `loadWorld()` (line 119 before this change): `world = await response.json();` ran inside the same try as the fetch. The catch chose its message from `response`, which is set once the fetch has returned. So when the status was 200 and the body read failed (for example a dropped connection), the viewer said `world/world.json could not be read as valid JSON.` The file may be valid, and the failure was the network. Reproduction on `main` (`a28927849`), with a stubbed fetch and the function read from the file in memory: ``` 200 + body read fails -> "world/world.json could not be read as valid JSON." no response -> "world/world.json could not be loaded." ``` ## What changed - `loadWorld()` reads the body with `response.text()` and parses it with `JSON.parse` as two separate steps. A `stage` variable records which step was running when the error was thrown. - The catch now returns: - `... could not be read.` when the read failed, - `... could not be read as valid JSON.` when the parse failed, - `... could not be loaded.` when the fetch failed (unchanged), - `... download timed out.` when the deadline fired (unchanged). - Test mocks that provided `json:` now provide `text:`. The test sandbox gets the host `JSON`, so parsed arrays compare as before. - New cases: a read that throws after a 200 response expects `could not be read.` and not `valid JSON`; a valid body still loads. ## Overlap with open pull requests - #651 (VIEWER-CHUNK-IDLE-TIMEOUT) edits `loadChunk` in `viewer/index.html` and adds fixtures and tests after line 456 of the test file. This pull request does not touch those hunks. Its `loadWorld` hunk is at lines 110-130, and its test hunks are at lines 21-22, 68-95 and 110. - #93 changes one camera speed test in the test file. No overlap. - No other open pull request touches these two files for `loadWorld`. ## Tested ``` node --test tests/test_viewer_streaming.cjs # node v24.22.0 # tests 59, pass 59, fail 0 python3 -I tests/test_text_write_line_endings.py # Ran 3 tests, OK python3 -I scripts/check_no_game_data.py viewer/index.html tests/test_viewer_streaming.cjs # exit 0 git diff --check # no output ``` The new cases fail on the unfixed `viewer/index.html` (the `main` version, run with the new test file): ``` node --test --test-name-pattern="read failure|body read that fails|valid manifest body still loads|test mode, a missing" tests/test_viewer_streaming.cjs # tests 3, pass 1, fail 2 # AssertionError: 'world/world.json could not be read as valid JSON.' does not match /could not be read\./ ``` The valid-body case also fails on the unfixed code, because its mock provides only `text()` and the old code calls `json()`. Not tested: a real browser. The viewer was not served or opened, so no page load, console or network check was done. The change is inside `loadWorld()`, which runs once at startup. No GPU, no game files. AI-assisted (Claude Code).
loadWorld() read the body and parsed it in one try, so a 200 response whose
body read failed was reported as "could not be read as valid JSON".
The read and the parse now have separate stages, and the message names the
stage that failed. Tests cover a failed read, invalid JSON text and a valid body.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:28:13 +00:00
Author
Owner

@⁠MoonEyes on GitHub · 2026-11-24 17:39 UTC · original · approved these changes

LGTM. Measured on Windows 11 with Node 26, against main cf4c00cba:

  • node --test tests/test_viewer_streaming.cjs: main 57/57; this PR and a local merge into main 59/59.
  • Mutation: deleting the stage === "read" line from this PR's viewer/index.html turns the new read-failure test red (58 pass, 1 fail). The body-stall test now mocks text(), so the deadline still covers the read.
  • Not tried in a browser, only through the vm harness.

AI-assisted: yes (Claude Code ran the checks; I read them).

**@⁠MoonEyes** on GitHub · 2026-11-24 17:39 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/918#pullrequestreview-6044345387) · **approved these changes** LGTM. Measured on Windows 11 with Node 26, against main cf4c00cba: - `node --test tests/test_viewer_streaming.cjs`: main 57/57; this PR and a local merge into main 59/59. - Mutation: deleting the `stage === "read"` line from this PR's `viewer/index.html` turns the new read-failure test red (58 pass, 1 fail). The body-stall test now mocks `text()`, so the deadline still covers the read. - Not tried in a browser, only through the `vm` harness. AI-assisted: yes (Claude Code ran the checks; I read them).
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/918/head:github-bot/gh-918'
git switch 'github-bot/gh-918'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-918'
git switch 'github-bot/gh-918'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-918'
git switch 'github-bot/gh-918'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-918'
git switch 'main'
git merge --squash 'github-bot/gh-918'
git switch 'main'
git merge --ff-only 'github-bot/gh-918'
git switch 'main'
git merge 'github-bot/gh-918'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.