@@ -94,6 +94,8 @@ def _should_retry_response(resp_status, content):
94
94
Returns:
95
95
True if the response should be retried, otherwise False.
96
96
"""
97
+ reason = None
98
+
97
99
# Retry on 5xx errors.
98
100
if resp_status >= 500 :
99
101
return True
@@ -113,9 +115,22 @@ def _should_retry_response(resp_status, content):
113
115
try :
114
116
data = json .loads (content .decode ("utf-8" ))
115
117
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 (error_detail , list ) and len (error_detail ) > 0 :
131
+ reason = error_detail [0 ]
132
+ if "reason" in reason :
133
+ reason = error ["reason" ]
119
134
else :
120
135
reason = data [0 ]["error" ]["errors" ]["reason" ]
121
136
except (UnicodeDecodeError , ValueError , KeyError ):
0 commit comments