Skip to content

Commit

Permalink
Adds meaningful Ioki::Error::Base message
Browse files Browse the repository at this point in the history
  • Loading branch information
eikes authored and rmehner committed Feb 10, 2022
1 parent 5fc3791 commit 50a3f5f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/ioki/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def request(url:, method: :get, body: nil, headers: [], params: nil)

error_class = Ioki::Error.http_status_code_to_error_class(response.status)

raise error_class.new(http_response: response) if error_class
raise error_class, response if error_class

return response.body, response
end
Expand Down
13 changes: 10 additions & 3 deletions lib/ioki/error/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ module Error
class Base < StandardError
attr_accessor :http_response

def initialize(message = nil, http_response: nil)
super(message)

def initialize(http_response)
@http_response = http_response
super http_response_message
end

def http_response_message
[
http_response&.status,
http_response&.env&.url,
http_response&.body
].join ' | '
end

def api_error
Expand Down
18 changes: 18 additions & 0 deletions spec/ioki/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,24 @@ class NullApi
expect(e.http_response.env.response_body).to eq(sent_response_body)
expect(e.api_error).to eq(sent_response_body)
end

it 'will expose the HTTP status in the error message' do
client_response
rescue StandardError => e
expect(e.message).to include '422'
end

it 'will expose the requested URL in the error message' do
client_response
rescue StandardError => e
expect(e.message).to include passed_path.to_s
end

it 'will expose the HTTP response body in the error message' do
client_response
rescue StandardError => e
expect(e.message).to include sent_response_body.to_s
end
end

context 'when the underlying http adapter response is without error' do
Expand Down
25 changes: 25 additions & 0 deletions spec/ioki/error_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,29 @@
expect(described_class.http_status_code_to_error_class(123)).to eq(Ioki::Error::UnexpectedResponseCode)
end
end

describe 'message' do
subject(:error_message) { error.message }

let(:error) { Ioki::Error::NotAcceptable.new http_response }
let(:http_response) { instance_double 'Faraday::Response', status: 406, body: response_body.to_json, env: env }
let(:env) { instance_double 'Faraday::Env', url: url }
let(:url) { 'https://example.com/api' }
let(:response_body) do
{
'api_errors' => [
{ 'message' => message_1, 'code' => code },
{ 'message' => message_2, 'code' => code }
]
}
end
let(:message_1) { 'X-Client-Identifier must be set' }
let(:message_2) { 'X-Api-Version must be set' }
let(:code) { 'headers_missing_error' }

it { is_expected.to include url }
it { is_expected.to include code }
it { is_expected.to include message_1 }
it { is_expected.to include message_2 }
end
end

0 comments on commit 50a3f5f

Please sign in to comment.