Skip to content

Commit

Permalink
Add response_{status|headers|body} methods to Error
Browse files Browse the repository at this point in the history
  • Loading branch information
qsona committed Mar 15, 2020
1 parent 5d24afc commit c1bec29
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
17 changes: 15 additions & 2 deletions lib/faraday/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ module Faraday
class Error < StandardError
attr_reader :response, :wrapped_exception

def initialize(exc, response = nil)
def initialize(exc, deprecated_response = nil, response = nil)
@wrapped_exception = nil unless defined?(@wrapped_exception)
@response = nil unless defined?(@response)
super(exc_msg_and_response!(exc, response))
@v2_response = response
super(exc_msg_and_response!(exc, deprecated_response))
end

def backtrace
Expand All @@ -28,6 +29,18 @@ def inspect
%(#<#{self.class}#{inner}>)
end

def response_status
@v2_response&.status
end

def response_body
@v2_response&.body
end

def response_headers
@v2_response&.headers
end

protected

# Pulls out potential parent exception and response hash, storing them in
Expand Down
32 changes: 30 additions & 2 deletions spec/faraday/error_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

RSpec.describe Faraday::ClientError do
describe '.initialize' do
subject { described_class.new(exception, response) }
subject { described_class.new(exception, deprecated_response, response) }
let(:deprecated_response) { nil }
let(:response) { nil }

context 'with exception only' do
Expand All @@ -15,7 +16,7 @@
it { expect(subject.inspect).to eq('#<Faraday::ClientError wrapped=#<RuntimeError: test>>') }
end

context 'with response hash' do
context 'with deprecated_response hash' do
let(:exception) { { status: 400 } }

it { expect(subject.wrapped_exception).to be_nil }
Expand All @@ -41,5 +42,32 @@
it { expect(subject.message).to eq('["error1", "error2"]') }
it { expect(subject.inspect).to eq('#<Faraday::ClientError #<Faraday::ClientError: ["error1", "error2"]>>') }
end

context 'with exception string and deprecated_response hash' do
let(:exception) { 'custom message' }
let(:deprecated_response) { { status: 400 } }

it { expect(subject.wrapped_exception).to be_nil }
it { expect(subject.response).to eq(deprecated_response) }
it { expect(subject.message).to eq('custom message') }
it { expect(subject.inspect).to eq('#<Faraday::ClientError response={:status=>400}>') }
end

context 'with deprecated_response hash and response' do
let(:exception) { { status: 400 } }
let(:env) do
Faraday::Env.from(status: 400, body: 'yikes',
response_headers: { 'Content-Type' => 'text/plain' })
end
let(:response) { Faraday::Response.new(env) }

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.response_status).to eq(400) }
it { expect(subject.response_body).to eq('yikes') }
it { expect(subject.response_headers).to eq({ 'Content-Type' => 'text/plain' }) }
end
end
end

0 comments on commit c1bec29

Please sign in to comment.