Skip to content

Commit

Permalink
Rescue Faraday::TimeoutError (#604)
Browse files Browse the repository at this point in the history
* Rescue Faraday::TimeoutError

If Faraday hits a timeout, it will raise a `Faraday::TimeoutError`.
Re-raise this as an `OAuth2::ConnectionError`, reusing the logic in
#549.

This came up in omniauth/omniauth-oauth2#129.

* Break out OAuth2::Client#request

This resolves several Rubocop lint errors.
  • Loading branch information
stanhu committed Jun 24, 2022
1 parent 4529f82 commit c9a9b87
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 17 deletions.
32 changes: 20 additions & 12 deletions lib/oauth2/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

module OAuth2
ConnectionError = Class.new(Faraday::ConnectionFailed)
TimeoutError = Class.new(Faraday::TimeoutError)

# The OAuth2::Client class
class Client # rubocop:disable Metrics/ClassLength
RESERVED_PARAM_KEYS = %w[headers parse].freeze
Expand Down Expand Up @@ -108,18 +110,7 @@ def token_url(params = nil)
# @option opts [Symbol] :parse @see Response::initialize
# @yield [req] The Faraday request
def request(verb, url, opts = {})
url = connection.build_url(url).to_s

begin
response = connection.run_request(verb, url, opts[:body], opts[:headers]) do |req|
req.params.update(opts[:params]) if opts[:params]
yield(req) if block_given?
end
rescue Faraday::ConnectionFailed => e
raise ConnectionError, e
end

response = Response.new(response, parse: opts[:parse])
response = execute_request(verb, url, opts)

case response.status
when 301, 302, 303, 307
Expand Down Expand Up @@ -251,6 +242,23 @@ def redirection_params

private

def execute_request(verb, url, opts = {})
url = connection.build_url(url).to_s

begin
response = connection.run_request(verb, url, opts[:body], opts[:headers]) do |req|
req.params.update(opts[:params]) if opts[:params]
yield(req) if block_given?
end
rescue Faraday::ConnectionFailed => e
raise ConnectionError, e
rescue Faraday::TimeoutError => e
raise TimeoutError, e
end

Response.new(response, parse: opts[:parse])
end

# Returns the authenticator object
#
# @return [Authenticator] the initialized Authenticator
Expand Down
28 changes: 23 additions & 5 deletions spec/oauth2/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -431,17 +431,35 @@
context 'when errors are raised by Faraday' do
let(:connection) { instance_double(Faraday::Connection, build_url: double) }

it 'rescues Faraday::ConnectionFailed' do
before do
allow(connection).to(
receive(:run_request).and_raise(Faraday::ConnectionFailed.new('fail'))
receive(:run_request).and_raise(faraday_exception)
)
allow(subject).to receive(:connection).and_return(connection) # rubocop:disable RSpec/SubjectStub
end

expect { subject.request(:get, '/redirect') }.to raise_error do |e|
expect(e.class).to eq(OAuth2::ConnectionError)
expect(e.message).to eq('fail')
shared_examples 'failed connection handler' do
it 'rescues the exception' do
expect { subject.request(:get, '/redirect') }.to raise_error do |e|
expect(e.class).to eq(expected_exception)
expect(e.message).to eq(faraday_exception.message)
end
end
end

context 'with Faraday::ConnectionFailed' do
let(:faraday_exception) { Faraday::ConnectionFailed.new('fail') }
let(:expected_exception) { OAuth2::ConnectionError }

it_behaves_like 'failed connection handler'
end

context 'with Faraday::TimeoutError' do
let(:faraday_exception) { Faraday::TimeoutError.new('timeout') }
let(:expected_exception) { OAuth2::TimeoutError }

it_behaves_like 'failed connection handler'
end
end
end

Expand Down

0 comments on commit c9a9b87

Please sign in to comment.