Skip to content

Commit

Permalink
Rescue Faraday::ServerErrror
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 23, 2022
1 parent e96dda8 commit 9794a88
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/oauth2/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def request(verb, url, opts = {})
req.params.update(opts[:params]) if opts[:params]
yield(req) if block_given?
end
rescue Faraday::ConnectionFailed => e
rescue Faraday::ConnectionFailed, Faraday::ServerError => e
raise ConnectionError, e
end

Expand Down
26 changes: 21 additions & 5 deletions spec/oauth2/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -430,16 +430,32 @@

context 'when errors are raised by Faraday' do
let(:connection) { instance_double(Faraday::Connection, build_url: double) }
let(:faraday_exception) { Faraday::ConnectionFailed.new('fail') }

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

context 'with Faraday::ConnectionFailed' do
it 'rescues the exception' do
expect { subject.request(:get, '/redirect') }.to raise_error do |e|
expect(e.class).to eq(OAuth2::ConnectionError)
expect(e.message).to eq('fail')
end
end
end

expect { subject.request(:get, '/redirect') }.to raise_error do |e|
expect(e.class).to eq(OAuth2::ConnectionError)
expect(e.message).to eq('fail')
context 'with Faraday::TimeoutError' do
let(:faraday_exception) { Faraday::TimeoutError.new('timeout') }

it 'rescues the exception' do
expect { subject.request(:get, '/redirect') }.to raise_error do |e|
expect(e.class).to eq(OAuth2::ConnectionError)
expect(e.message).to eq('timeout')
end
end
end
end
Expand Down

0 comments on commit 9794a88

Please sign in to comment.