Skip to content

fix: defensive RPC response handling#2546

Merged
k9ert merged 1 commit intocryptoadvance:masterfrom
al-munazzim:fix/2506-rpc-keyerror
Mar 19, 2026
Merged

fix: defensive RPC response handling#2546
k9ert merged 1 commit intocryptoadvance:masterfrom
al-munazzim:fix/2506-rpc-keyerror

Conversation

@al-munazzim
Copy link
Copy Markdown
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

@netlify
Copy link
Copy Markdown

netlify Bot commented Feb 12, 2026

Deploy Preview for specter-desktop-docs canceled.

Name Link
🔨 Latest commit 6bb7e36
🔍 Latest deploy log https://app.netlify.com/projects/specter-desktop-docs/deploys/69bbecdb14f9430008a6cf7d

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
@k9ert k9ert force-pushed the fix/2506-rpc-keyerror branch from 9cfd165 to 6bb7e36 Compare March 19, 2026 12:32
@k9ert k9ert merged commit 7fa79ed into cryptoadvance:master Mar 19, 2026
9 checks passed
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

2 participants