Skip to content

Commit

Permalink
Set as a server error with more descriptive message
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnyom committed Oct 2, 2019
1 parent 39751a5 commit 538e2ae
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 20 deletions.
17 changes: 9 additions & 8 deletions lib/faraday/error.rb
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 0 additions & 9 deletions spec/faraday/error_spec.rb
Expand Up @@ -24,15 +24,6 @@
it { expect(subject.inspect).to eq('#<Faraday::ClientError response={:status=>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('#<Faraday::ClientError response={:status=>nil}>') }
end

context 'with string' do
let(:exception) { 'custom message' }

Expand Down
5 changes: 2 additions & 3 deletions spec/faraday/response/raise_error_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 538e2ae

Please sign in to comment.