From 8590a3d651288913981c07d66cb0bedff9c3d633 Mon Sep 17 00:00:00 2001 From: Mattia Giuffrida Date: Thu, 31 Dec 2020 11:53:01 +0000 Subject: [PATCH 1/2] Improves consistency with Faraday::Error and Faraday::RaiseError Fixes #956 --- lib/faraday/error.rb | 12 +++++++++ spec/faraday/error_spec.rb | 10 ++++++++ spec/faraday/response/raise_error_spec.rb | 30 +++++++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/lib/faraday/error.rb b/lib/faraday/error.rb index 8b494411c..a201abcf7 100644 --- a/lib/faraday/error.rb +++ b/lib/faraday/error.rb @@ -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 diff --git a/spec/faraday/error_spec.rb b/spec/faraday/error_spec.rb index 98a31b84e..4bfbd699e 100644 --- a/spec/faraday/error_spec.rb +++ b/spec/faraday/error_spec.rb @@ -41,5 +41,15 @@ it { expect(subject.message).to eq('["error1", "error2"]') } it { expect(subject.inspect).to eq('#>') } 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('#400}>') } + end end end diff --git a/spec/faraday/response/raise_error_spec.rb b/spec/faraday/response/raise_error_spec.rb index f08455a4b..a80472dc0 100644 --- a/spec/faraday/response/raise_error_spec.rb +++ b/spec/faraday/response/raise_error_spec.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 From fa108eafe674f6f08b5cdf9291b115bfe1f5c448 Mon Sep 17 00:00:00 2001 From: Mattia Giuffrida Date: Thu, 31 Dec 2020 12:19:17 +0000 Subject: [PATCH 2/2] Adds tests --- spec/faraday/error_spec.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/spec/faraday/error_spec.rb b/spec/faraday/error_spec.rb index 4bfbd699e..bea9f0c34 100644 --- a/spec/faraday/error_spec.rb +++ b/spec/faraday/error_spec.rb @@ -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('#>') } + it { expect(subject.response_status).to be_nil } end context 'with response hash' do @@ -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('#400}>') } + it { expect(subject.response_status).to eq(400) } end context 'with string' do @@ -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('#>') } + it { expect(subject.response_status).to be_nil } end context 'with anything else #to_s' do @@ -40,6 +43,7 @@ it { expect(subject.response).to be_nil } it { expect(subject.message).to eq('["error1", "error2"]') } it { expect(subject.inspect).to eq('#>') } + it { expect(subject.response_status).to be_nil } end context 'with exception string and response hash' do @@ -50,6 +54,7 @@ it { expect(subject.response).to eq(response) } it { expect(subject.message).to eq('custom message') } it { expect(subject.inspect).to eq('#400}>') } + it { expect(subject.response_status).to eq(400) } end end end