Skip to content

Commit 2dc5535

Browse files
authored
feat(core): add few more errors as retriable errors (#10026)
1 parent f1feb45 commit 2dc5535

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

google-apis-core/lib/google/apis/core/http_command.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ module Core
2727
class HttpCommand
2828
include Logging
2929

30-
RETRIABLE_ERRORS = [Google::Apis::ServerError, Google::Apis::RateLimitError, Google::Apis::TransmissionError]
30+
RETRIABLE_ERRORS = [Google::Apis::ServerError,
31+
Google::Apis::RateLimitError,
32+
Google::Apis::TransmissionError,
33+
Google::Apis::RequestTimeOutError]
3134

3235
begin
3336
require 'opencensus'
@@ -225,6 +228,9 @@ def check_status(status, header = nil, body = nil, message = nil)
225228
when 429
226229
message ||= 'Rate limit exceeded'
227230
raise Google::Apis::RateLimitError.new(message, status_code: status, header: header, body: body)
231+
when 408
232+
message ||= 'Request time out'
233+
raise Google::Apis::RequestTimeOutError.new(message, status_code: status, header: header, body: body)
228234
when 304, 400, 402...500
229235
message ||= 'Invalid request'
230236
raise Google::Apis::ClientError.new(message, status_code: status, header: header, body: body)
@@ -278,7 +284,7 @@ def error(err, rethrow: false, &block)
278284
rescue Google::Apis::Error => e
279285
err = e
280286
end
281-
elsif err.is_a?(HTTPClient::TimeoutError) || err.is_a?(SocketError)
287+
elsif err.is_a?(HTTPClient::TimeoutError) || err.is_a?(SocketError) || err.is_a?(HTTPClient::KeepAliveDisconnected) || err.is_a?(Errno::ECONNREFUSED) || err.is_a?(Errno::ETIMEDOUT)
282288
err = Google::Apis::TransmissionError.new(err)
283289
end
284290
block.call(nil, err) if block_given?

google-apis-core/lib/google/apis/errors.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ class RedirectError < Error
6666
class ClientError < Error
6767
end
6868

69-
# A 4xx class HTTP error occurred.
69+
# A 408 HTTP error occurred.
70+
class RequestTimeOutError < ClientError
71+
end
72+
73+
# A 429 HTTP error occurred.
7074
class RateLimitError < Error
7175
end
7276

google-apis-core/spec/google/apis/core/http_command_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,13 +473,41 @@ class DecryptResponse
473473
expect { command.execute(client) }.to raise_error(Google::Apis::TransmissionError)
474474
end
475475

476+
it 'should raise transmission error instead of connection reset' do
477+
stub_request(:get, 'https://www.googleapis.com/zoo/animals').to_raise(HTTPClient::KeepAliveDisconnected)
478+
command = Google::Apis::Core::HttpCommand.new(:get, 'https://www.googleapis.com/zoo/animals')
479+
command.options.retries = 0
480+
expect { command.execute(client) }.to raise_error(Google::Apis::TransmissionError)
481+
end
482+
483+
it 'should raise transmission error instead of connection timeout' do
484+
stub_request(:get, 'https://www.googleapis.com/zoo/animals').to_raise(Errno::ETIMEDOUT)
485+
command = Google::Apis::Core::HttpCommand.new(:get, 'https://www.googleapis.com/zoo/animals')
486+
command.options.retries = 0
487+
expect { command.execute(client) }.to raise_error(Google::Apis::TransmissionError)
488+
end
489+
490+
it 'should raise transmission error instead of connection refused' do
491+
stub_request(:get, 'https://www.googleapis.com/zoo/animals').to_raise(Errno::ECONNREFUSED)
492+
command = Google::Apis::Core::HttpCommand.new(:get, 'https://www.googleapis.com/zoo/animals')
493+
command.options.retries = 0
494+
expect { command.execute(client) }.to raise_error(Google::Apis::TransmissionError)
495+
end
496+
476497
it 'should raise rate limit error for 429 status codes' do
477498
stub_request(:get, 'https://www.googleapis.com/zoo/animals').to_return(status: [429, ''])
478499
command = Google::Apis::Core::HttpCommand.new(:get, 'https://www.googleapis.com/zoo/animals')
479500
command.options.retries = 0
480501
expect { command.execute(client) }.to raise_error(Google::Apis::RateLimitError)
481502
end
482503

504+
it 'should raise request timeout error for 408 status codes' do
505+
stub_request(:get, 'https://www.googleapis.com/zoo/animals').to_return(status: [408, ''])
506+
command = Google::Apis::Core::HttpCommand.new(:get, 'https://www.googleapis.com/zoo/animals')
507+
command.options.retries = 0
508+
expect { command.execute(client) }.to raise_error(Google::Apis::RequestTimeOutError)
509+
end
510+
483511
it 'should not normalize unicode values by default' do
484512
stub_request(:get, 'https://www.googleapis.com/Cafe%CC%81').to_return(status: [200, ''])
485513
template = Addressable::Template.new('https://www.googleapis.com/{path}')

0 commit comments

Comments
 (0)