Skip to content

Commit

Permalink
Fix implementation of Faraday::Error helpers.
Browse files Browse the repository at this point in the history
Fix #1509
  • Loading branch information
iMacTia committed Jun 15, 2023
1 parent de709cd commit 26e2e86
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
12 changes: 9 additions & 3 deletions lib/faraday/error.rb
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
@@ -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

0 comments on commit 26e2e86

Please sign in to comment.