Skip to content

Commit f41544a

Browse files
committed
fix: Resolve issue where num_retries would have no effect
1 parent b9f1d0b commit f41544a

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

googleapiclient/http.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ def _should_retry_response(resp_status, content):
9494
Returns:
9595
True if the response should be retried, otherwise False.
9696
"""
97+
reason = None
98+
9799
# Retry on 5xx errors.
98100
if resp_status >= 500:
99101
return True
@@ -113,9 +115,22 @@ def _should_retry_response(resp_status, content):
113115
try:
114116
data = json.loads(content.decode("utf-8"))
115117
if isinstance(data, dict):
116-
reason = data["error"].get("status")
117-
if reason is None:
118-
reason = data["error"]["errors"][0]["reason"]
118+
# There are many variations of the error json so we need
119+
# to determine the keyword which has the error detail. Make sure
120+
# that the order of the keywords below isn't changed as it can
121+
# break user code. If the "errors" key exists, we must use that
122+
# first.
123+
# See Issue #1243
124+
# https://github.com/googleapis/google-api-python-client/issues/1243
125+
error_detail_keyword = next((kw for kw in ["errors", "status", "message"] if kw in data["error"]), "")
126+
127+
if error_detail_keyword:
128+
reason = data["error"][error_detail_keyword]
129+
130+
if isinstance(reason, list) and len(reason) > 0:
131+
reason = reason[0]
132+
if "reason" in reason:
133+
reason = reason["reason"]
119134
else:
120135
reason = data[0]["error"]["errors"]["reason"]
121136
except (UnicodeDecodeError, ValueError, KeyError):

0 commit comments

Comments
 (0)