Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix implementation of Faraday::Error helpers. #1510

Merged
merged 1 commit into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions lib/faraday/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,21 @@ def inspect
end

def response_status
@response[:status] if @response
return unless @response

@response.is_a?(Faraday::Response) ? @response.status : @response[:status]
end

def response_headers
@response[:headers] if @response
return unless @response

@response.is_a?(Faraday::Response) ? @response.headers : @response[:headers]
end

def response_body
@response[:body] if @response
return unless @response

@response.is_a?(Faraday::Response) ? @response.body : @response[:body]
end

protected
Expand Down
37 changes: 31 additions & 6 deletions spec/faraday/error_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

RSpec.describe Faraday::ClientError do
RSpec.describe Faraday::Error do
describe '.initialize' do
subject { described_class.new(exception, response) }
let(:response) { nil }
Expand All @@ -12,8 +12,10 @@
it { expect(subject.response).to be_nil }
it { expect(subject.message).to eq(exception.message) }
it { expect(subject.backtrace).to eq(exception.backtrace) }
it { expect(subject.inspect).to eq('#<Faraday::ClientError wrapped=#<RuntimeError: test>>') }
it { expect(subject.inspect).to eq('#<Faraday::Error wrapped=#<RuntimeError: test>>') }
it { expect(subject.response_status).to be_nil }
it { expect(subject.response_headers).to be_nil }
it { expect(subject.response_body).to be_nil }
end

context 'with response hash' do
Expand All @@ -22,8 +24,10 @@
it { expect(subject.wrapped_exception).to be_nil }
it { expect(subject.response).to eq(exception) }
it { expect(subject.message).to eq('the server responded with status 400') }
it { expect(subject.inspect).to eq('#<Faraday::ClientError response={:status=>400}>') }
it { expect(subject.inspect).to eq('#<Faraday::Error response={:status=>400}>') }
it { expect(subject.response_status).to eq(400) }
it { expect(subject.response_headers).to be_nil }
it { expect(subject.response_body).to be_nil }
end

context 'with string' do
Expand All @@ -32,8 +36,10 @@
it { expect(subject.wrapped_exception).to be_nil }
it { expect(subject.response).to be_nil }
it { expect(subject.message).to eq('custom message') }
it { expect(subject.inspect).to eq('#<Faraday::ClientError #<Faraday::ClientError: custom message>>') }
it { expect(subject.inspect).to eq('#<Faraday::Error #<Faraday::Error: custom message>>') }
it { expect(subject.response_status).to be_nil }
it { expect(subject.response_headers).to be_nil }
it { expect(subject.response_body).to be_nil }
end

context 'with anything else #to_s' do
Expand All @@ -42,8 +48,10 @@
it { expect(subject.wrapped_exception).to be_nil }
it { expect(subject.response).to be_nil }
it { expect(subject.message).to eq('["error1", "error2"]') }
it { expect(subject.inspect).to eq('#<Faraday::ClientError #<Faraday::ClientError: ["error1", "error2"]>>') }
it { expect(subject.inspect).to eq('#<Faraday::Error #<Faraday::Error: ["error1", "error2"]>>') }
it { expect(subject.response_status).to be_nil }
it { expect(subject.response_headers).to be_nil }
it { expect(subject.response_body).to be_nil }
end

context 'with exception string and response hash' do
Expand All @@ -53,8 +61,25 @@
it { expect(subject.wrapped_exception).to be_nil }
it { expect(subject.response).to eq(response) }
it { expect(subject.message).to eq('custom message') }
it { expect(subject.inspect).to eq('#<Faraday::ClientError response={:status=>400}>') }
it { expect(subject.inspect).to eq('#<Faraday::Error response={:status=>400}>') }
it { expect(subject.response_status).to eq(400) }
it { expect(subject.response_headers).to be_nil }
it { expect(subject.response_body).to be_nil }
end

context 'with exception and response object' do
let(:exception) { RuntimeError.new('test') }
let(:body) { { test: 'test' } }
let(:headers) { { 'Content-Type' => 'application/json' } }
let(:response) { Faraday::Response.new(status: 400, response_headers: headers, response_body: body) }

it { expect(subject.wrapped_exception).to eq(exception) }
it { expect(subject.response).to eq(response) }
it { expect(subject.message).to eq(exception.message) }
it { expect(subject.backtrace).to eq(exception.backtrace) }
it { expect(subject.response_status).to eq(400) }
it { expect(subject.response_headers).to eq(headers) }
it { expect(subject.response_body).to eq(body) }
end
end
end