Skip to content

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

Closed
nkuba wants to merge 8 commits intochecksum0:masterfrom
keep-network:master
Closed

Handle error string returned in the JSON-RPC response#8
nkuba wants to merge 8 commits intochecksum0:masterfrom
keep-network:master

Conversation

@nkuba
Copy link

@nkuba nkuba commented May 17, 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 and others added 4 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.
Handle error string returned in the JSON-RPC response
@nkuba
Copy link
Author

nkuba commented May 17, 2023

@checksum0 will you be interested in merging this PR? We confirmed errors are handled correctly with integration tests in threshold-network/keep-core#3567.

nkuba and others added 4 commits May 19, 2023 16:44
Here we add support for WebSocket protocol. With this change the client
will support all connection types defined by the electrum protocol:
`tcp`, `ssl`, `ws` and `wss`.

The WebSocket client initialization expects remote server details to be
provided as URL including a scheme and host, e.g. `tcp://electrum.io:50001`.
The `NewClient` function should be used to initialize the client.
The function resolves transport protocol from the URL.
It supports all four protocols: `tcp`, `ssl`, `ws` and `wss`.
After sending a close message to the server we need to wait for the
response or force the conncetion closure after a timeout.
@nkuba
Copy link
Author

nkuba commented May 24, 2023

Closing in favor of #9.

@nkuba nkuba closed this May 24, 2023
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