Enhance JSON response parsing, prevent error leak #20
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
If the JSON response from speedtest does not contain a property with the value -
"type": "result", then a problem has occurred and the script should not attempt to extract the values from the JSON object or perform a database save.Currently, a non-user friendly error message is shown when a problem occurs, but we should prevent this script error and show a more friendly message.
Another side effect of not handling the error is that the script will exit with a non-zero status, in effect killing the container.
Network problems may be intermittent, and if the container is running the speed tests in a loop, we want to be able to handle those intermittent problems without the container dying.
Before this change:
docker run -e SPEEDTEST_SERVER_ID=123 -e LOOP=true -e LOOP_DELAY=5 barryevans80/speedtest:wip Running speedtest forever... ♾️ Running a Speed Test with Server ID 123... {"type":"log","timestamp":"2024-06-17T13:34:56Z","message":"Configuration - No servers defined (NoServersException)","level":"error"} ./speedtest.sh: 33: arithmetic expression: expecting primary: " / 125000 "After this change:
docker run -e SPEEDTEST_SERVER_ID=123 -e LOOP=true -e LOOP_DELAY=5 barryevans80/speedtest:wip Running speedtest forever... ♾️ Running a Speed Test with Server ID 123... {"type":"log","timestamp":"2024-06-17T13:33:29Z","message":"Configuration - No servers defined (NoServersException)","level":"error"} [error] Unable to retrieve JSON results from speedtest, please see previous log message Running next test in 5s... Running a Speed Test with Server ID 123... {"type":"log","timestamp":"2024-06-17T13:33:36Z","message":"Configuration - No servers defined (NoServersException)","level":"error"} [error] Unable to retrieve JSON results from speedtest, please see previous log message Running next test in 5s...The validation required to make the script more robust is achieved by using a simple jq
selectfunction:echo "$JSON" | jq 'select(.type == "result")'See more about the jq
selectfunction here:Some shellcheck fixes have been applied:
$/${} is unnecessary on arithmetic variables.- https://github.com/koalaman/shellcheck/wiki/SC2004Double quote to prevent globbing and word splitting.- https://github.com/koalaman/shellcheck/wiki/SC2086Sample successful json response
{ "type": "result", "timestamp": "2024-06-17T05:27:00Z", "ping": { "jitter": 0.852, "latency": 288.814, "low": 287.153, "high": 289.37 }, "download": { "bandwidth": 1591349, "bytes": 22220160, "elapsed": 15010, "latency": { "iqm": 286.068, "low": 282.946, "high": 293.54, "jitter": 1.568 } }, "upload": { "bandwidth": 38988010, "bytes": 493973670, "elapsed": 15005, "latency": { "iqm": 295.051, "low": 286.54, "high": 379.607, "jitter": 7.626 } }, ... }Sample failed json response
{ "type": "log", "timestamp": "2024-06-17T05:25:48Z", "message": "Configuration - No servers defined (NoServersException)", "level": "error" }