From 6adf09615c93f3fe2834dc91384526021685fcb1 Mon Sep 17 00:00:00 2001 From: Michael Herold Date: Fri, 27 Sep 2019 13:40:27 -0500 Subject: [PATCH 1/2] Raise exception on nil status --- lib/faraday/error.rb | 8 ++++++++ lib/faraday/response/raise_error.rb | 2 ++ spec/faraday/response/raise_error_spec.rb | 7 +++++++ 3 files changed, 17 insertions(+) diff --git a/lib/faraday/error.rb b/lib/faraday/error.rb index 43266e1db..18241a473 100644 --- a/lib/faraday/error.rb +++ b/lib/faraday/error.rb @@ -83,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/lib/faraday/response/raise_error.rb b/lib/faraday/response/raise_error.rb index 8a380fd40..4ac8b15c7 100644 --- a/lib/faraday/response/raise_error.rb +++ b/lib/faraday/response/raise_error.rb @@ -28,6 +28,8 @@ def on_complete(env) raise Faraday::ConflictError, response_values(env) when 422 raise Faraday::UnprocessableEntityError, response_values(env) + when nil + raise Faraday::NilStatusError, response_values(env) when ClientErrorStatuses raise Faraday::ClientError, response_values(env) when ServerErrorStatuses diff --git a/spec/faraday/response/raise_error_spec.rb b/spec/faraday/response/raise_error_spec.rb index 2f258a5d4..98d890a23 100644 --- a/spec/faraday/response/raise_error_spec.rb +++ b/spec/faraday/response/raise_error_spec.rb @@ -14,6 +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' => 'bailout' }, 'fail'] } stub.get('server-error') { [500, { 'X-Error' => 'bailout' }, 'fail'] } end end @@ -72,6 +73,12 @@ end end + 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('http status could not be derived from the server response') + end + end + it 'raises Faraday::ClientError for other 4xx responses' do expect { conn.get('4xx') }.to raise_error(Faraday::ClientError) do |ex| expect(ex.message).to eq('the server responded with status 499') From 2c81ed67ed6bfb7b25db0c3b5687faa338ca44ad Mon Sep 17 00:00:00 2001 From: Jonny Date: Fri, 4 Oct 2019 12:34:25 +0100 Subject: [PATCH 2/2] Pass the exception correctly --- lib/faraday/error.rb | 2 +- lib/faraday/response/raise_error.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/faraday/error.rb b/lib/faraday/error.rb index 18241a473..2152d6603 100644 --- a/lib/faraday/error.rb +++ b/lib/faraday/error.rb @@ -85,7 +85,7 @@ def initialize(exc = 'timeout', response = nil) # Raised by Faraday::Response::RaiseError in case of a nil status in response. class NilStatusError < ServerError - def initialize(response = nil) + def initialize(_exc, response: nil) message = 'http status could not be derived from the server response' super(message, response) end diff --git a/lib/faraday/response/raise_error.rb b/lib/faraday/response/raise_error.rb index 4ac8b15c7..3425d1a60 100644 --- a/lib/faraday/response/raise_error.rb +++ b/lib/faraday/response/raise_error.rb @@ -29,7 +29,7 @@ def on_complete(env) when 422 raise Faraday::UnprocessableEntityError, response_values(env) when nil - raise Faraday::NilStatusError, response_values(env) + raise Faraday::NilStatusError, response: response_values(env) when ClientErrorStatuses raise Faraday::ClientError, response_values(env) when ServerErrorStatuses