Skip to content

Commit

Permalink
Improves consistency with Faraday::Error and Faraday::RaiseError
Browse files Browse the repository at this point in the history
Fixes #956
  • Loading branch information
iMacTia committed Dec 31, 2020
1 parent 453b26f commit 8590a3d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/faraday/error.rb
Original file line number Diff line number Diff line change
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
10 changes: 10 additions & 0 deletions spec/faraday/error_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,15 @@
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 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}>') }
end
end
end
30 changes: 30 additions & 0 deletions spec/faraday/response/raise_error_spec.rb
Original file line number Diff line number Diff line change
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 8590a3d

Please sign in to comment.