Skip to content

Commit

Permalink
Improves consistency with Faraday::Error and Faraday::RaiseError (#1229)
Browse files Browse the repository at this point in the history
  • Loading branch information
iMacTia committed Dec 31, 2020
1 parent 453b26f commit 023cc41
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/faraday/error.rb
Expand Up @@ -28,6 +28,18 @@ def inspect
%(#<#{self.class}#{inner}>)
end

def response_status
@response[:status] if @response
end

def response_headers
@response[:headers] if @response
end

def response_body
@response[:body] if @response
end

protected

# Pulls out potential parent exception and response hash, storing them in
Expand Down
15 changes: 15 additions & 0 deletions spec/faraday/error_spec.rb
Expand Up @@ -13,6 +13,7 @@
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.response_status).to be_nil }
end

context 'with response hash' do
Expand All @@ -22,6 +23,7 @@
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) }
end

context 'with string' do
Expand All @@ -31,6 +33,7 @@
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.response_status).to be_nil }
end

context 'with anything else #to_s' do
Expand All @@ -40,6 +43,18 @@
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.response_status).to be_nil }
end

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

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.response_status).to eq(400) }
end
end
end
30 changes: 30 additions & 0 deletions spec/faraday/response/raise_error_spec.rb
Expand Up @@ -29,6 +29,9 @@
expect(ex.message).to eq('the server responded with status 400')
expect(ex.response[:headers]['X-Reason']).to eq('because')
expect(ex.response[:status]).to eq(400)
expect(ex.response_status).to eq(400)
expect(ex.response_body).to eq('keep looking')
expect(ex.response_headers['X-Reason']).to eq('because')
end
end

Expand All @@ -37,6 +40,9 @@
expect(ex.message).to eq('the server responded with status 401')
expect(ex.response[:headers]['X-Reason']).to eq('because')
expect(ex.response[:status]).to eq(401)
expect(ex.response_status).to eq(401)
expect(ex.response_body).to eq('keep looking')
expect(ex.response_headers['X-Reason']).to eq('because')
end
end

Expand All @@ -45,6 +51,9 @@
expect(ex.message).to eq('the server responded with status 403')
expect(ex.response[:headers]['X-Reason']).to eq('because')
expect(ex.response[:status]).to eq(403)
expect(ex.response_status).to eq(403)
expect(ex.response_body).to eq('keep looking')
expect(ex.response_headers['X-Reason']).to eq('because')
end
end

Expand All @@ -53,6 +62,9 @@
expect(ex.message).to eq('the server responded with status 404')
expect(ex.response[:headers]['X-Reason']).to eq('because')
expect(ex.response[:status]).to eq(404)
expect(ex.response_status).to eq(404)
expect(ex.response_body).to eq('keep looking')
expect(ex.response_headers['X-Reason']).to eq('because')
end
end

Expand All @@ -61,6 +73,9 @@
expect(ex.message).to eq('407 "Proxy Authentication Required"')
expect(ex.response[:headers]['X-Reason']).to eq('because')
expect(ex.response[:status]).to eq(407)
expect(ex.response_status).to eq(407)
expect(ex.response_body).to eq('keep looking')
expect(ex.response_headers['X-Reason']).to eq('because')
end
end

Expand All @@ -69,6 +84,9 @@
expect(ex.message).to eq('the server responded with status 409')
expect(ex.response[:headers]['X-Reason']).to eq('because')
expect(ex.response[:status]).to eq(409)
expect(ex.response_status).to eq(409)
expect(ex.response_body).to eq('keep looking')
expect(ex.response_headers['X-Reason']).to eq('because')
end
end

Expand All @@ -77,6 +95,9 @@
expect(ex.message).to eq('the server responded with status 422')
expect(ex.response[:headers]['X-Reason']).to eq('because')
expect(ex.response[:status]).to eq(422)
expect(ex.response_status).to eq(422)
expect(ex.response_body).to eq('keep looking')
expect(ex.response_headers['X-Reason']).to eq('because')
end
end

Expand All @@ -85,6 +106,9 @@
expect(ex.message).to eq('http status could not be derived from the server response')
expect(ex.response[:headers]['X-Reason']).to eq('nil')
expect(ex.response[:status]).to be_nil
expect(ex.response_status).to be_nil
expect(ex.response_body).to eq('fail')
expect(ex.response_headers['X-Reason']).to eq('nil')
end
end

Expand All @@ -93,6 +117,9 @@
expect(ex.message).to eq('the server responded with status 499')
expect(ex.response[:headers]['X-Reason']).to eq('because')
expect(ex.response[:status]).to eq(499)
expect(ex.response_status).to eq(499)
expect(ex.response_body).to eq('keep looking')
expect(ex.response_headers['X-Reason']).to eq('because')
end
end

Expand All @@ -101,6 +128,9 @@
expect(ex.message).to eq('the server responded with status 500')
expect(ex.response[:headers]['X-Error']).to eq('bailout')
expect(ex.response[:status]).to eq(500)
expect(ex.response_status).to eq(500)
expect(ex.response_body).to eq('fail')
expect(ex.response_headers['X-Error']).to eq('bailout')
end
end

Expand Down

0 comments on commit 023cc41

Please sign in to comment.