Skip to content

breaking: Catch and wrap Faraday client errors #470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 16, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions lib/twilio-ruby/http/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ def _request(request)

@last_request = request
@last_response = nil
response = @connection.send(request.method.downcase.to_sym,
request.url,
request.method == 'GET' ? request.params : request.data)

response = send(request)
if response.body && !response.body.empty?
object = response.body
elsif response.status == 400
Expand All @@ -52,6 +50,14 @@ def _request(request)
twilio_response
end

def send(request)
@connection.send(request.method.downcase.to_sym,
request.url,
request.method == 'GET' ? request.params : request.data)
rescue Faraday::ClientError => e
raise Twilio::REST::TwilioError, e
end

def request(host, port, method, url, params = {}, data = {}, headers = {}, auth = nil, timeout = nil)
request = Twilio::Request.new(host, port, method, url, params, data, headers, auth, timeout)
_request(request)
Expand Down
4 changes: 2 additions & 2 deletions spec/http/http_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
expect(Faraday).to receive(:new).and_return(Faraday::Connection.new)
allow_any_instance_of(Faraday::Connection).to receive(:send).and_raise(Faraday::ConnectionFailed.new('BOOM'))

expect { @client.request('host', 'port', 'GET', 'url', nil, nil, {}, ['a', 'b']) }.to raise_exception(Faraday::ConnectionFailed)
expect { @client.request('host', 'port', 'GET', 'url', nil, nil, {}, ['a', 'b']) }.to raise_exception(Twilio::REST::TwilioError)
expect(@client.last_response).to be_nil
expect(@client.last_request).to_not be_nil
expect(@client.last_request.host).to eq('host')
Expand All @@ -116,7 +116,7 @@
it 'previous last_response should be cleared' do
expect(Faraday).to receive(:new).and_return(Faraday::Connection.new)
allow_any_instance_of(Faraday::Connection).to receive(:send).and_raise(Faraday::ConnectionFailed.new('BOOM'))
expect { @client.request('host', 'port', 'GET', 'url', nil, nil, {}, ['a', 'b']) }.to raise_exception(Faraday::ConnectionFailed)
expect { @client.request('host', 'port', 'GET', 'url', nil, nil, {}, ['a', 'b']) }.to raise_exception(Twilio::REST::TwilioError)
expect(@client.last_response).to be_nil
end
end