fix: handle non-JSON HTTP error responses in wallet CLI commands#768
Merged
Scottcjn merged 1 commit intoScottcjn:mainfrom Mar 9, 2026
Merged
Conversation
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.
Contributor
There was a problem hiding this comment.
Welcome to RustChain! Thanks for your first pull request.
Before we review, please make sure:
- Your PR has a
BCOS-L1orBCOS-L2label - 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!
Scottcjn
approved these changes
Mar 9, 2026
Owner
Scottcjn
left a comment
There was a problem hiding this comment.
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
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #765
Problem
cmd_balance,cmd_history,cmd_miners,cmd_epoch, andcmd_sendintools/rustchain_wallet_cli.pyall calledr.json()directly without checking whether the HTTP response body contained valid JSON.When the node returns a non-JSON error (e.g.
502 Bad Gatewaywith an HTML body, plain-text503, etc.) arequests.exceptions.JSONDecodeErrorpropagated to the user with an opaque message:Solution
Introduce a
_safe_json(r)helper that catchesJSONDecodeErrorand prints a descriptive HTTP status message:Returns
(None, 1)so callers propagate the error exit-code cleanly. All five affected commands updated to use_safe_json.Testing