Skip to content

bug: wallet CLI crashes with JSONDecodeError on non-JSON HTTP error responses #765

@wsimon1982

Description

@wsimon1982

Summary

cmd_balance, cmd_history, cmd_miners, and cmd_epoch in tools/rustchain_wallet_cli.py all call r.json() directly without first verifying that the HTTP response body is valid JSON. When the server returns a non-JSON error (e.g., 502 Bad Gateway with an HTML body, or a plain-text 503 Service Unavailable), a requests.exceptions.JSONDecodeError propagates out of the function.

Although main() catches Exception, the resulting error message (Expecting value: line 1 column 1 (char 0)) gives the user zero context about what actually went wrong.

Affected code

tools/rustchain_wallet_cli.py – functions: cmd_balance, cmd_history, cmd_miners, cmd_epoch, cmd_send

# cmd_balance (line ~175)
r = requests.get(url, params={"miner_id": args.wallet_id}, timeout=12, verify=VERIFY_SSL)
data = r.json()   # <-- crashes if server returns HTML/text on error

Steps to reproduce

# Point at a non-existent or misconfigured node
export RUSTCHAIN_NODE_URL=https://httpbin.org/status/502
python -m tools.rustchain_wallet_cli balance RTCabc123

Expected behavior: A clear error message such as Error: Server returned HTTP 502 (check node URL/connectivity).

Actual behavior:

Error: Expecting value: line 1 column 1 (char 0)

Fix

Call r.raise_for_status() before r.json(), or wrap the JSON decode in a try/except that re-raises with the HTTP status code included.

r = requests.get(url, params={"miner_id": args.wallet_id}, timeout=12, verify=VERIFY_SSL)
try:
    data = r.json()
except requests.exceptions.JSONDecodeError:
    print(f"Error: Server returned HTTP {r.status_code} with non-JSON body", file=sys.stderr)
    return 1

Labels

bug, wallet, error-handling

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions