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

Skip to content

Commit a341c5a

Browse files
authored
docs: Change error parsing to check for 'message' (googleapis#1083)
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [x] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/google-api-python-client/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [x] Ensure the tests and linter pass - [x] Code coverage does not decrease (if any source code was changed) - [x] Appropriate docs were updated (if necessary) Fixes googleapis#1082 🦕
1 parent 8430fac commit a341c5a

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

docs/start.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ request = collection.list(cents=5)
9494
Creating a request does not actually call the API. To execute the request and get a response, call the `execute()` function:
9595

9696
```python
97-
response = request.execute()
97+
try:
98+
response = request.execute()
99+
except HttpError as e:
100+
print('Error response status code : {0}, reason : {1}'.format(e.resp.status, e.error_details))
98101
```
99102

100103
Alternatively, you can combine previous steps on a single line:

googleapiclient/errors.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,9 @@ def _get_reason(self):
5151
data = json.loads(self.content.decode("utf-8"))
5252
if isinstance(data, dict):
5353
reason = data["error"]["message"]
54-
if "details" in data["error"]:
55-
self.error_details = data["error"]["details"]
56-
elif "detail" in data["error"]:
57-
self.error_details = data["error"]["detail"]
54+
error_detail_keyword = next((kw for kw in ["detail", "details", "message"] if kw in data["error"]), "")
55+
if error_detail_keyword:
56+
self.error_details = data["error"][error_detail_keyword]
5857
elif isinstance(data, list) and len(data) > 0:
5958
first_error = data[0]
6059
reason = first_error["error"]["message"]

tests/test_errors.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,24 @@
4747
}
4848
"""
4949

50+
JSON_ERROR_CONTENT_NO_DETAIL = b"""
51+
{
52+
"error": {
53+
"errors": [
54+
{
55+
"domain": "global",
56+
"reason": "required",
57+
"message": "country is required",
58+
"locationType": "parameter",
59+
"location": "country"
60+
}
61+
],
62+
"code": 400,
63+
"message": "country is required"
64+
}
65+
}
66+
"""
67+
5068

5169
def fake_response(data, headers, reason="Ok"):
5270
response = httplib2.Response(headers)
@@ -112,3 +130,14 @@ def test_missing_reason(self):
112130
resp, content = fake_response(b"}NOT OK", {"status": "400"}, reason=None)
113131
error = HttpError(resp, content)
114132
self.assertEqual(str(error), '<HttpError 400 "">')
133+
134+
def test_error_detail_for_missing_message_in_error(self):
135+
"""Test handling of data with missing 'details' or 'detail' element."""
136+
resp, content = fake_response(
137+
JSON_ERROR_CONTENT_NO_DETAIL,
138+
{"status": "400", "content-type": "application/json"},
139+
reason="Failed",
140+
)
141+
error = HttpError(resp, content)
142+
self.assertEqual(str(error), '<HttpError 400 when requesting None returned "country is required". Details: "country is required">')
143+
self.assertEqual(error.error_details, 'country is required')

tests/test_http.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1567,7 +1567,8 @@ def test_execute_batch_http_error(self):
15671567
expected = (
15681568
"<HttpError 403 when requesting "
15691569
"https://www.googleapis.com/someapi/v1/collection/?foo=bar returned "
1570-
'"Access Not Configured">'
1570+
'"Access Not Configured". '
1571+
'Details: "Access Not Configured">'
15711572
)
15721573
self.assertEqual(expected, str(callbacks.exceptions["2"]))
15731574

0 commit comments

Comments
 (0)