Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f6e2661

Browse files
sonlacJon Wayne Parrott
authored and
Jon Wayne Parrott
committed
Fix details missing in googleapiclient.errors.HttpError (googleapis#412)
Resolves: googleapis#382
1 parent 52101a5 commit f6e2661

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

googleapiclient/errors.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def __init__(self, resp, content, uri=None):
4646
raise TypeError("HTTP content should be bytes")
4747
self.content = content
4848
self.uri = uri
49+
self.error_details = ''
4950

5051
def _get_reason(self):
5152
"""Calculate the reason for the error from the response content."""
@@ -54,17 +55,25 @@ def _get_reason(self):
5455
data = json.loads(self.content.decode('utf-8'))
5556
if isinstance(data, dict):
5657
reason = data['error']['message']
58+
if 'details' in data['error']:
59+
self.error_details = data['error']['details']
5760
elif isinstance(data, list) and len(data) > 0:
5861
first_error = data[0]
5962
reason = first_error['error']['message']
63+
if 'details' in first_error['error']:
64+
self.error_details = first_error['error']['details']
6065
except (ValueError, KeyError, TypeError):
6166
pass
6267
if reason is None:
6368
reason = ''
6469
return reason
6570

6671
def __repr__(self):
67-
if self.uri:
72+
reason = self._get_reason()
73+
if self.error_details:
74+
return '<HttpError %s when requesting %s returned "%s". Details: "%s">' % \
75+
(self.resp.status, self.uri, reason.strip(), self.error_details)
76+
elif self.uri:
6877
return '<HttpError %s when requesting %s returned "%s">' % (
6978
self.resp.status, self.uri, self._get_reason().strip())
7079
else:

tests/test_errors.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
}
4242
],
4343
"code": 400,
44-
"message": "country is required"
44+
"message": "country is required",
45+
"details": "error details"
4546
}
4647
}
4748
"""
@@ -61,7 +62,7 @@ def test_json_body(self):
6162
{'status':'400', 'content-type': 'application/json'},
6263
reason='Failed')
6364
error = HttpError(resp, content, uri='http://example.org')
64-
self.assertEqual(str(error), '<HttpError 400 when requesting http://example.org returned "country is required">')
65+
self.assertEqual(str(error), '<HttpError 400 when requesting http://example.org returned "country is required". Details: "error details">')
6566

6667
def test_bad_json_body(self):
6768
"""Test handling of bodies with invalid json."""

0 commit comments

Comments
 (0)