How to Manage Google API Errors in Python -
i'm doing lot of stuff bigquery, , using lot of try... except...
. looks every error bigquery apiclient.errors.httperror, different strings attached them, i.e.:
<httperror 409 when requesting https://www.googleapis.com/bigquery/v2/projects/some_id/datasets/some_dataset/tables?alt=json returned "already exists: table some_id:some_dataset.some_table">
<httperror 404 when requesting https://www.googleapis.com/bigquery/v2/projects/some_id/jobs/sdfgsdfg?alt=json returned "not found: job some_id:sdfgsdfg">
among many others. right way see handle these run regexs on error messages, messy , not ideal. there better way?
bigquery rest api... errors uses follow standard http error conventions. in python, httperror has resp.status field returns http status code. show above, 409 'conflict', 404 'not found'.
for example:
try: ... except httperror err: # if error rate limit or connection error, # wait , try again. if err.resp.status in [403, 500, 503]: time.sleep(5) else: raise
the response json object... better way parse json , read error reason field:
if err.resp.get('content-type', '').startswith('application/json'): reason = json.loads(e.content).reason
this can be: notfound, duplicate, accessdenied, invalidquery, backenderror, resourcesexceeded, invalid, quotaexceeded, ratelimitexceeded, timeout, etc.
Comments
Post a Comment