diff --git a/lib/faraday/error.rb b/lib/faraday/error.rb index b095fe62f..18241a473 100644 --- a/lib/faraday/error.rb +++ b/lib/faraday/error.rb @@ -16,10 +16,7 @@ def initialize(exc, response = nil) super(exc.message) @wrapped_exception = exc elsif exc.respond_to?(:each_key) - nil_status_message = 'the server responded with a nil status' - status_exists_msg = "the server responded with status #{exc[:status]}" - message = exc[:status].nil? ? nil_status_message : status_exists_msg - super(message) + super("the server responded with status #{exc[:status]}") @response = exc else super(exc.to_s) @@ -75,10 +72,6 @@ class ConflictError < ClientError class UnprocessableEntityError < ClientError end - # Raised by Faraday::Response::RaiseError in case of a nil status in response. - class NilStatusError < ClientError - end - # Faraday server error class. Represents 5xx status responses. class ServerError < Error end @@ -90,6 +83,14 @@ def initialize(exc = 'timeout', response = nil) end end + # Raised by Faraday::Response::RaiseError in case of a nil status in response. + class NilStatusError < ServerError + def initialize(response = nil) + message = 'http status could not be derived from the server response' + super(message, response) + end + end + # A unified error for failed connections. class ConnectionFailed < Error end diff --git a/spec/faraday/error_spec.rb b/spec/faraday/error_spec.rb index 33619f871..9371f57d8 100644 --- a/spec/faraday/error_spec.rb +++ b/spec/faraday/error_spec.rb @@ -24,15 +24,6 @@ it { expect(subject.inspect).to eq('#400}>') } end - context 'with response hash but nil status' do - let(:exception) { { status: nil } } - - 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 a nil status') } - it { expect(subject.inspect).to eq('#nil}>') } - end - context 'with string' do let(:exception) { 'custom message' } diff --git a/spec/faraday/response/raise_error_spec.rb b/spec/faraday/response/raise_error_spec.rb index f70af7460..98d890a23 100644 --- a/spec/faraday/response/raise_error_spec.rb +++ b/spec/faraday/response/raise_error_spec.rb @@ -14,7 +14,7 @@ stub.get('conflict') { [409, { 'X-Reason' => 'because' }, 'keep looking'] } stub.get('unprocessable-entity') { [422, { 'X-Reason' => 'because' }, 'keep looking'] } stub.get('4xx') { [499, { 'X-Reason' => 'because' }, 'keep looking'] } - stub.get('nil-status') { [nil, { 'X-Reason' => 'because' }, 'keep looking'] } + stub.get('nil-status') { [nil, { 'X-Reason' => 'bailout' }, 'fail'] } stub.get('server-error') { [500, { 'X-Error' => 'bailout' }, 'fail'] } end end @@ -75,8 +75,7 @@ it 'raises Faraday::NilStatusError for nil status in response' do expect { conn.get('nil-status') }.to raise_error(Faraday::NilStatusError) do |ex| - expect(ex.message).to eq('the server responded with a nil status') - expect(ex.response[:headers]['X-Reason']).to eq('because') + expect(ex.message).to eq('http status could not be derived from the server response') end end