Skip to content

Handle error string returned in the JSON-RPC response#9

Open
nkuba wants to merge 3 commits intochecksum0:masterfrom
keep-network:json-rpc-error-unmarshal
Open

Handle error string returned in the JSON-RPC response#9
nkuba wants to merge 3 commits intochecksum0:masterfrom
keep-network:json-rpc-error-unmarshal

Conversation

@nkuba
Copy link

@nkuba nkuba commented May 24, 2023

Closes: #5

Here we provide a workaround for servers that don't follow the JSON-RPC 2.0 specification for error objects.

According to the specification, an error should be an object containing code, message, and data properties (see: https://www.jsonrpc.org/specification#error_object).
Unfortunately, Electrs returns an error as a string (see: Blockstream/esplora#453).

We define an error unmarshaling function to handle both types of errors.

Sample outputs from servers that the client has to handle:
ElectrumX

echo '{"jsonrpc": "2.0", "method": "blockchain.block.header", "params": [4294967295], "id": 0}' | netcat 49.12.127.114 10068
{"jsonrpc":"2.0","error":{"code":1,"message":"height 4,294,967,295 out of range"},"id":0}

Fulcrum

echo '{"jsonrpc": "2.0", "method": "blockchain.block.header", "params": [4294967295], "id": 0}' | netcat 203.132.94.196 51001
{"error":{"code":1,"message":"Invalid height"},"id":0,"jsonrpc":"2.0"}

Esplora/Electrs

echo '{"jsonrpc": "2.0", "method": "blockchain.block.header", "params": [4294967295], "id": 0}' | netcat 35.225.54.191 50001
{"error":"missing header","id":0,"jsonrpc":"2.0"}

nkuba added 3 commits May 16, 2023 14:03
Here we provide a workaround for servers that don't follow the
JSON-RPC 2.0 specification for error objects.

According to the specification, an error should be an object
containing `code`, `message`, and `data` properties
(see: https://www.jsonrpc.org/specification#error_object).
Unfortunately, Electrs returns an error as a string
(see: Blockstream/esplora#453).

We define an error unmarshaling function to handle both types of errors.
It would be usefull to include value that failed unmarshalling in the
debug mode log message.
@losh11
Copy link

losh11 commented Feb 24, 2026

Can this PR please be merged?

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.

Unmarshaling error from response fails with: cannot unmarshal object into Go struct field response.error of type string

2 participants