Skip to content

Commit ade3432

Browse files
authored
add JSONRPC error code to Client::RpcError (#392)
1 parent 69f1b1b commit ade3432

2 files changed

Lines changed: 23 additions & 6 deletions

File tree

lib/eth/client.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,28 @@ class Client
4040
# A custom error type if a contract interaction fails.
4141
class ContractExecutionError < StandardError; end
4242

43-
# Raised when an RPC call returns an error. Carries the optional
43+
# Raised when an RPC call returns an error. Carries the error code and the optional
4444
# hex-encoded error data to support custom error decoding.
4545
class RpcError < IOError
4646
attr_reader :data
47+
attr_reader :code
4748

4849
# Constructor for the {RpcError} class.
4950
#
5051
# @param message [String] the error message returned by the RPC.
5152
# @param data [String] optional hex encoded error data.
52-
def initialize(message, data = nil)
53+
# @param code [String] optional error code returned by the RPC.
54+
def initialize(message, data = nil, code = nil)
5355
super(message)
5456
@data = data
57+
@code = code
5558
end
5659
end
5760

5861
# Creates a new RPC-Client, either by providing an HTTP/S host, WS/S host,
5962
# or an IPC path. Supports basic authentication with username and password.
6063
#
61-
# **Note**, this sets the folling gas defaults: {Tx::DEFAULT_PRIORITY_FEE}
64+
# **Note**, this sets the following gas defaults: {Tx::DEFAULT_PRIORITY_FEE}
6265
# and {Tx::DEFAULT_GAS_PRICE. Use {#max_priority_fee_per_gas} and
6366
# {#max_fee_per_gas} to set custom values prior to submitting transactions.
6467
#
@@ -483,7 +486,7 @@ def send_command(command, args)
483486
}
484487
output = JSON.parse(send_request(payload.to_json))
485488
if (err = output["error"])
486-
raise RpcError.new(err["message"], err["data"])
489+
raise RpcError.new(err["message"], err["data"], err["code"])
487490
end
488491
output
489492
end

spec/eth/client_spec.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
describe Client do
44

5-
# run `geth --dev --http --ipcpath /tmp/geth.ipc`
6-
# to provide both http and ipc to pass these tests.
5+
# run `geth --dev --http --wc --ipcpath /tmp/geth.ipc`
6+
# to provide http, ws and ipc to pass these tests.
77
let(:geth_ipc_path) { "/tmp/geth.ipc" }
88
let(:geth_http_path) { "http://127.0.0.1:8545" }
99
let(:geth_ws_path) { "ws://127.0.0.1:8546" }
@@ -121,6 +121,20 @@
121121
end
122122
end
123123

124+
describe Client::RpcError do
125+
%i[http ws].each do |protocol|
126+
context "when #{protocol}" do
127+
it "carries the JSONRPC message and code returned by the server" do
128+
client = send("geth_#{protocol}")
129+
130+
expect { client.get_balance("0xinvalid") }.to(
131+
raise_error(an_instance_of(Client::RpcError).and have_attributes(message: /invalid argument 0:/, code: -32602))
132+
)
133+
end
134+
end
135+
end
136+
end
137+
124138
describe ".transfer .transfer_and_wait" do
125139
subject(:test_key) { Key.new }
126140
subject(:another_key) { Key.new }

0 commit comments

Comments
 (0)