Fix details missing in googleapiclient.errors.HttpError (#412)
Resolves: #382
diff --git a/googleapiclient/errors.py b/googleapiclient/errors.py
index 6a741f8..bab1418 100644
--- a/googleapiclient/errors.py
+++ b/googleapiclient/errors.py
@@ -46,6 +46,7 @@
raise TypeError("HTTP content should be bytes")
self.content = content
self.uri = uri
+ self.error_details = ''
def _get_reason(self):
"""Calculate the reason for the error from the response content."""
@@ -54,9 +55,13 @@
data = json.loads(self.content.decode('utf-8'))
if isinstance(data, dict):
reason = data['error']['message']
+ if 'details' in data['error']:
+ self.error_details = data['error']['details']
elif isinstance(data, list) and len(data) > 0:
first_error = data[0]
reason = first_error['error']['message']
+ if 'details' in first_error['error']:
+ self.error_details = first_error['error']['details']
except (ValueError, KeyError, TypeError):
pass
if reason is None:
@@ -64,7 +69,11 @@
return reason
def __repr__(self):
- if self.uri:
+ reason = self._get_reason()
+ if self.error_details:
+ return '<HttpError %s when requesting %s returned "%s". Details: "%s">' % \
+ (self.resp.status, self.uri, reason.strip(), self.error_details)
+ elif self.uri:
return '<HttpError %s when requesting %s returned "%s">' % (
self.resp.status, self.uri, self._get_reason().strip())
else:
diff --git a/tests/test_errors.py b/tests/test_errors.py
index 8a58030..e4d2f09 100644
--- a/tests/test_errors.py
+++ b/tests/test_errors.py
@@ -41,7 +41,8 @@
}
],
"code": 400,
- "message": "country is required"
+ "message": "country is required",
+ "details": "error details"
}
}
"""
@@ -61,7 +62,7 @@
{'status':'400', 'content-type': 'application/json'},
reason='Failed')
error = HttpError(resp, content, uri='http://example.org')
- self.assertEqual(str(error), '<HttpError 400 when requesting http://example.org returned "country is required">')
+ self.assertEqual(str(error), '<HttpError 400 when requesting http://example.org returned "country is required". Details: "error details">')
def test_bad_json_body(self):
"""Test handling of bodies with invalid json."""