Skip to content

Commit

Permalink
Merge pull request #56 from uc-cdis/fix/patch-google
Browse files Browse the repository at this point in the history
fix(google): get reason from content
  • Loading branch information
philloooo authored Feb 19, 2019
2 parents 8c34270 + 94ec25b commit 0219f1d
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions cirrus/google_cloud/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,23 @@ def log_backoff_giveup(details):
)
)

def get_reason(http_error):
"""
temporary solution to work around googleapiclient bug that doesn't
parse reason from server response
"""
reason = http_error.resp.reason
try:
data = json.loads(http_error.content.decode("utf-8"))
if isinstance(data, dict):
reason = data["error"].get("reason")
if "errors" in data["error"] and len(data["error"]["errors"]) > 0:
reason = data["error"]["errors"][0]["reason"]
except (ValueError, KeyError, TypeError):
pass
if reason is None:
reason = ""
return reason

def exception_do_not_retry(e):
"""
Expand All @@ -139,11 +156,13 @@ def exception_do_not_retry(e):
# Valid rate limit reasons from DIRECTORY API
# developers.google.com/admin-sdk/directory/v1/limits
directory_rlreasons = ["userRateLimitExceeded", "quotaExceeded"]
reason = get_reason(e) or e.resp.reason
logger.info("Get 403 from google with reason {}".format(reason))
# Valid rate limit reasons from IAM API
# IAM API doesn't seem to return rate-limit 403s.
return (
e.resp.reason not in resource_rlreasons
and e.resp.reason not in directory_rlreasons
reason not in resource_rlreasons
and reason not in directory_rlreasons
)
return False

Expand Down

0 comments on commit 0219f1d

Please sign in to comment.