Skip to content

fix: defensive RPC response handling#2546

Open
al-munazzim wants to merge 1 commit intocryptoadvance:masterfrom
al-munazzim:fix/2506-rpc-keyerror
Open

fix: defensive RPC response handling#2546
al-munazzim wants to merge 1 commit intocryptoadvance:masterfrom
al-munazzim:fix/2506-rpc-keyerror

Conversation

@al-munazzim
Copy link
Contributor

Problem

Fixes #2506. When Bitcoin Core (or an RPC proxy) returns a non-standard JSON-RPC response, the current error handling in BitcoinRPC.__getattr__ can crash with KeyError:

  • r['error']['message'] assumes error is always a dict with a message key
  • r['result'] assumes the response always contains a result key
  • multi()[0] assumes the response is always a list

Root Cause

The JSON-RPC spec says error should be an object with code and message, but in practice:

  • Some proxies return error as a plain string
  • Some implementations omit the message field
  • Some return a single object instead of a batch array
  • Connection issues can produce responses missing result entirely

Fix

  1. __getattr__ / fn(): Check if error is a dict before accessing .get('message'); fall back to str(error). Also check for missing result key explicitly.
  2. multi(): Wrap single-dict responses in a list so [0] indexing always works.

Testing

Verified manually with mocked responses covering all 4 edge cases:

  • String error field → proper RpcError
  • Missing result key → proper RpcError
  • Single dict (non-batch) response → works correctly
  • Error dict without message key → proper RpcError with fallback text

Handle non-standard JSON-RPC responses that cause KeyError:
- Error field as string instead of dict (e.g. from proxies)
- Error dict missing 'message' key
- Response missing 'result' key entirely
- Single dict response instead of batch array

Fixes cryptoadvance#2506
@netlify
Copy link

netlify bot commented Feb 12, 2026

Deploy Preview for specter-desktop-docs canceled.

Name Link
🔨 Latest commit 9cfd165
🔍 Latest deploy log https://app.netlify.com/projects/specter-desktop-docs/deploys/698d5920984c180008a94787

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Exception Error

1 participant