Skip to content

fix: handle non-JSON HTTP error responses in wallet CLI commands#768

Merged
Scottcjn merged 1 commit intoScottcjn:mainfrom
wsimon1982:fix/wallet-cli-json-decode-error
Mar 9, 2026
Merged

fix: handle non-JSON HTTP error responses in wallet CLI commands#768
Scottcjn merged 1 commit intoScottcjn:mainfrom
wsimon1982:fix/wallet-cli-json-decode-error

Conversation

@wsimon1982
Copy link
Copy Markdown
Contributor

Fixes #765

Problem

cmd_balance, cmd_history, cmd_miners, cmd_epoch, and cmd_send in tools/rustchain_wallet_cli.py all called r.json() directly without checking whether the HTTP response body contained valid JSON.

When the node returns a non-JSON error (e.g. 502 Bad Gateway with an HTML body, plain-text 503, etc.) a requests.exceptions.JSONDecodeError propagated to the user with an opaque message:

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

Solution

Introduce a _safe_json(r) helper that catches JSONDecodeError and prints a descriptive HTTP status message:

Error: Server returned HTTP 502 with non-JSON body (check node URL / connectivity)

Returns (None, 1) so callers propagate the error exit-code cleanly. All five affected commands updated to use _safe_json.

Testing

export RUSTCHAIN_NODE_URL=https://httpbin.org/status/502
python -m tools.rustchain_wallet_cli balance RTCabc123
# Before: Error: Expecting value: line 1 column 1 (char 0)
# After:  Error: Server returned HTTP 502 with non-JSON body (check node URL / connectivity)

Fixes Scottcjn#765

cmd_balance, cmd_send, cmd_history, cmd_miners, and cmd_epoch all called
r.json() directly without checking whether the response body was valid
JSON.  When the node returns a non-JSON error (e.g. 502 Bad Gateway with
an HTML body), a requests.exceptions.JSONDecodeError propagated to the
user with an opaque message like "Expecting value: line 1 column 1".

Introduce a _safe_json() helper that catches JSONDecodeError and prints a
descriptive HTTP status message instead, returning (None, 1) so callers
can propagate the error exit-code cleanly.
@wsimon1982 wsimon1982 requested a review from Scottcjn as a code owner March 9, 2026 18:34
@github-actions github-actions bot added BCOS-L1 Beacon Certified Open Source tier BCOS-L1 (required for non-doc PRs) size/S PR: 11-50 lines labels Mar 9, 2026
Copy link
Copy Markdown
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Welcome to RustChain! Thanks for your first pull request.

Before we review, please make sure:

  • Your PR has a BCOS-L1 or BCOS-L2 label
  • New code files include an SPDX license header
  • You've tested your changes against the live node

Bounty tiers: Micro (1-10 RTC) | Standard (20-50) | Major (75-100) | Critical (100-150)

A maintainer will review your PR soon. Thanks for contributing!

Copy link
Copy Markdown
Owner

@Scottcjn Scottcjn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solid improvement. _safe_json() helper properly handles HTML error pages (502/503 from nginx) instead of crashing with JSONDecodeError. Clean, consistent application across all 5 wallet commands. Type annotation uses from __future__ import annotations already present in the file.

Verdict: MERGE — 15 RTC bounty (standard improvement).

🤖 Reviewed with GPT-5.4 + Claude Opus dual-brain analysis

@Scottcjn Scottcjn merged commit becf4bf into Scottcjn:main Mar 9, 2026
3 checks passed
createkr pushed a commit to createkr/Rustchain that referenced this pull request Mar 22, 2026
…ttcjn#768)

Fixes Scottcjn#765

cmd_balance, cmd_send, cmd_history, cmd_miners, and cmd_epoch all called
r.json() directly without checking whether the response body was valid
JSON.  When the node returns a non-JSON error (e.g. 502 Bad Gateway with
an HTML body), a requests.exceptions.JSONDecodeError propagated to the
user with an opaque message like "Expecting value: line 1 column 1".

Introduce a _safe_json() helper that catches JSONDecodeError and prints a
descriptive HTTP status message instead, returning (None, 1) so callers
can propagate the error exit-code cleanly.

Co-authored-by: wsimon1982 <wsimon1982@googlemail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

BCOS-L1 Beacon Certified Open Source tier BCOS-L1 (required for non-doc PRs) size/S PR: 11-50 lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants