Skip to content

Commit c35150f

Browse files
bashir2theacodes
authored andcommitted
Catch ServerNotFoundError to retry the request (#532)
1 parent b0b1c1d commit c35150f

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

googleapiclient/http.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ def _retry_request(http, num_retries, req_type, sleep, rand, uri, method, *args,
173173
'WSAETIMEDOUT', 'ETIMEDOUT', 'EPIPE', 'ECONNABORTED'}:
174174
raise
175175
exception = socket_error
176+
except httplib2.ServerNotFoundError as server_not_found_error:
177+
exception = server_not_found_error
176178

177179
if exception:
178180
if retry_num == num_retries:

tests/test_http.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ def request(self, *args, **kwargs):
127127
self.num_errors -= 1
128128
if self.num_errors == 1: # initial == 2
129129
raise ssl.SSLError()
130-
else: # initial != 2
130+
if self.num_errors == 3: # initial == 4
131+
raise httplib2.ServerNotFoundError()
132+
else: # initial != 2,4
131133
if self.num_errors == 2:
132134
# first try a broken pipe error (#218)
133135
ex = socket.error()
@@ -495,14 +497,14 @@ def test_media_io_base_download_handle_4xx(self):
495497

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

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

505-
status, done = download.next_chunk(num_retries=3)
507+
status, done = download.next_chunk(num_retries=4)
506508

507509
self.assertEqual(self.fd.getvalue(), b'123')
508510
self.assertEqual(True, done)
@@ -839,12 +841,12 @@ def test_no_retry_connection_errors(self):
839841
def test_retry_connection_errors_non_resumable(self):
840842
model = JsonModel()
841843
request = HttpRequest(
842-
HttpMockWithErrors(3, {'status': '200'}, '{"foo": "bar"}'),
844+
HttpMockWithErrors(4, {'status': '200'}, '{"foo": "bar"}'),
843845
model.response,
844846
u'https://www.example.com/json_api_endpoint')
845847
request._sleep = lambda _x: 0 # do nothing
846848
request._rand = lambda: 10
847-
response = request.execute(num_retries=3)
849+
response = request.execute(num_retries=4)
848850
self.assertEqual({u'foo': u'bar'}, response)
849851

850852
def test_retry_connection_errors_resumable(self):
@@ -856,14 +858,14 @@ def test_retry_connection_errors_resumable(self):
856858

857859
request = HttpRequest(
858860
HttpMockWithErrors(
859-
3, {'status': '200', 'location': 'location'}, '{"foo": "bar"}'),
861+
4, {'status': '200', 'location': 'location'}, '{"foo": "bar"}'),
860862
model.response,
861863
u'https://www.example.com/file_upload',
862864
method='POST',
863865
resumable=upload)
864866
request._sleep = lambda _x: 0 # do nothing
865867
request._rand = lambda: 10
866-
response = request.execute(num_retries=3)
868+
response = request.execute(num_retries=4)
867869
self.assertEqual({u'foo': u'bar'}, response)
868870

869871
def test_retry(self):

0 commit comments

Comments
 (0)