Skip to content

Commit

Permalink
Merge pull request #6 from NARKOZ/print-body-jsonrpc-error
Browse files Browse the repository at this point in the history
Include response's error message when JSONRPCError
  • Loading branch information
NARKOZ authored Feb 6, 2019
2 parents c2b919d + 62cc5ca commit a267c56
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
sudo: false
language: ruby
rvm:
- 2.4.2
- 2.6.0
before_install: gem install bundler -v 1.16.1
9 changes: 5 additions & 4 deletions lib/bitcoiner/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,11 @@ def parse_body(response)
if response.success?
JSON.parse(response.body)
else
error_message = %i[code return_code].map do |attr|
"#{attr}: `#{response.send(attr)}`"
end.join(', ')
raise JSONRPCError, "unsuccessful response; #{error_message}"
error_messages = %i[code return_code].each_with_object({}) do |attr, hash|
hash[attr] = response.send(attr)
end
error_messages[:body] = response.body
raise JSONRPCError, error_messages.map {|k, v| "#{k}: `#{v}`"}.join("; ")
end
end
end
Expand Down
19 changes: 13 additions & 6 deletions test/client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@ class ClientTest < Minitest::Test
context 'response is not successful' do
setup do
response = Typhoeus::Response.new(
code: 0,
return_code: :couldnt_connect
code: 500,
return_code: :ok,
# response supposedly includes body
# https://github.com/bitcoin/bitcoin/issues/12673#issuecomment-372334718
body: {some: "body"}.to_json,
)
Typhoeus.stub('http://127.0.0.1:8332', userpwd: "testuser:testpass").
and_return(response)
Expand All @@ -76,12 +79,16 @@ class ClientTest < Minitest::Test
end

should 'raise JSONRPCError' do
assert_raises(
Bitcoiner::Client::JSONRPCError,
'unsuccessful response; code: `0`, return_code: `couldnt_connect`'
) do
error = assert_raises(Bitcoiner::Client::JSONRPCError) do
@bcd.request('listtransactions')
end

expected_error_message = [
'code: `500`',
'return_code: `ok`',
'body: `{"some":"body"}`',
].join("; ")
assert_equal expected_error_message, error.message
end
end

Expand Down

0 comments on commit a267c56

Please sign in to comment.