Skip to content

Commit

Permalink
Rescue Faraday::TimeoutError
Browse files Browse the repository at this point in the history
If Faraday hits a timeout, it will raise a `Faraday::TimeoutError`.
Re-raise this as an `OAuth2::ConnectionError`, reusing the logic in
oauth-xx#549.

This came up in omniauth/omniauth-oauth2#129.
  • Loading branch information
stanhu committed Jun 24, 2022
1 parent e96dda8 commit a392e88
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
4 changes: 4 additions & 0 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 @@ -115,6 +117,8 @@ def request(verb, url, opts = {})
end
rescue Faraday::ConnectionFailed => e
raise ConnectionError, e
rescue Faraday::TimeoutError => e
raise TimeoutError, e
end

response = Response.new(response, parse: opts[:parse])
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 a392e88

Please sign in to comment.