Skip to content

Commit

Permalink
Catch ServerNotFoundError to retry the request (#532)
Browse files Browse the repository at this point in the history
  • Loading branch information
bashir2 authored and theacodes committed Jun 25, 2018
1 parent b0b1c1d commit c35150f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
2 changes: 2 additions & 0 deletions googleapiclient/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ def _retry_request(http, num_retries, req_type, sleep, rand, uri, method, *args,
'WSAETIMEDOUT', 'ETIMEDOUT', 'EPIPE', 'ECONNABORTED'}:
raise
exception = socket_error
except httplib2.ServerNotFoundError as server_not_found_error:
exception = server_not_found_error

if exception:
if retry_num == num_retries:
Expand Down
16 changes: 9 additions & 7 deletions tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ def request(self, *args, **kwargs):
self.num_errors -= 1
if self.num_errors == 1: # initial == 2
raise ssl.SSLError()
else: # initial != 2
if self.num_errors == 3: # initial == 4
raise httplib2.ServerNotFoundError()
else: # initial != 2,4
if self.num_errors == 2:
# first try a broken pipe error (#218)
ex = socket.error()
Expand Down Expand Up @@ -495,14 +497,14 @@ def test_media_io_base_download_handle_4xx(self):

def test_media_io_base_download_retries_connection_errors(self):
self.request.http = HttpMockWithErrors(
3, {'status': '200', 'content-range': '0-2/3'}, b'123')
4, {'status': '200', 'content-range': '0-2/3'}, b'123')

download = MediaIoBaseDownload(
fd=self.fd, request=self.request, chunksize=3)
download._sleep = lambda _x: 0 # do nothing
download._rand = lambda: 10

status, done = download.next_chunk(num_retries=3)
status, done = download.next_chunk(num_retries=4)

self.assertEqual(self.fd.getvalue(), b'123')
self.assertEqual(True, done)
Expand Down Expand Up @@ -839,12 +841,12 @@ def test_no_retry_connection_errors(self):
def test_retry_connection_errors_non_resumable(self):
model = JsonModel()
request = HttpRequest(
HttpMockWithErrors(3, {'status': '200'}, '{"foo": "bar"}'),
HttpMockWithErrors(4, {'status': '200'}, '{"foo": "bar"}'),
model.response,
u'https://www.example.com/json_api_endpoint')
request._sleep = lambda _x: 0 # do nothing
request._rand = lambda: 10
response = request.execute(num_retries=3)
response = request.execute(num_retries=4)
self.assertEqual({u'foo': u'bar'}, response)

def test_retry_connection_errors_resumable(self):
Expand All @@ -856,14 +858,14 @@ def test_retry_connection_errors_resumable(self):

request = HttpRequest(
HttpMockWithErrors(
3, {'status': '200', 'location': 'location'}, '{"foo": "bar"}'),
4, {'status': '200', 'location': 'location'}, '{"foo": "bar"}'),
model.response,
u'https://www.example.com/file_upload',
method='POST',
resumable=upload)
request._sleep = lambda _x: 0 # do nothing
request._rand = lambda: 10
response = request.execute(num_retries=3)
response = request.execute(num_retries=4)
self.assertEqual({u'foo': u'bar'}, response)

def test_retry(self):
Expand Down

0 comments on commit c35150f

Please sign in to comment.