Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Change error parsing to check for 'message' #1083

Merged
merged 4 commits into from Nov 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/start.md
Expand Up @@ -94,7 +94,10 @@ request = collection.list(cents=5)
Creating a request does not actually call the API. To execute the request and get a response, call the `execute()` function:

```python
response = request.execute()
try:
response = request.execute()
except HttpError as e:
print('Error response status code : {0}, reason : {1}'.format(e.resp.status, e.error_details))
```

Alternatively, you can combine previous steps on a single line:
Expand Down
7 changes: 3 additions & 4 deletions googleapiclient/errors.py
Expand Up @@ -51,10 +51,9 @@ def _get_reason(self):
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 "detail" in data["error"]:
self.error_details = data["error"]["detail"]
error_detail_keyword = next((kw for kw in ["detail", "details", "message"] if kw in data["error"]), "")
if error_detail_keyword:
self.error_details = data["error"][error_detail_keyword]
elif isinstance(data, list) and len(data) > 0:
first_error = data[0]
reason = first_error["error"]["message"]
Expand Down
29 changes: 29 additions & 0 deletions tests/test_errors.py
Expand Up @@ -47,6 +47,24 @@
}
"""

JSON_ERROR_CONTENT_NO_DETAIL = b"""
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "country is required",
"locationType": "parameter",
"location": "country"
}
],
"code": 400,
"message": "country is required"
}
}
"""


def fake_response(data, headers, reason="Ok"):
response = httplib2.Response(headers)
Expand Down Expand Up @@ -112,3 +130,14 @@ def test_missing_reason(self):
resp, content = fake_response(b"}NOT OK", {"status": "400"}, reason=None)
error = HttpError(resp, content)
self.assertEqual(str(error), '<HttpError 400 "">')

def test_error_detail_for_missing_message_in_error(self):
"""Test handling of data with missing 'details' or 'detail' element."""
resp, content = fake_response(
JSON_ERROR_CONTENT_NO_DETAIL,
{"status": "400", "content-type": "application/json"},
reason="Failed",
)
error = HttpError(resp, content)
self.assertEqual(str(error), '<HttpError 400 when requesting None returned "country is required". Details: "country is required">')
self.assertEqual(error.error_details, 'country is required')
3 changes: 2 additions & 1 deletion tests/test_http.py
Expand Up @@ -1567,7 +1567,8 @@ def test_execute_batch_http_error(self):
expected = (
"<HttpError 403 when requesting "
"https://www.googleapis.com/someapi/v1/collection/?foo=bar returned "
'"Access Not Configured">'
'"Access Not Configured". '
'Details: "Access Not Configured">'
)
self.assertEqual(expected, str(callbacks.exceptions["2"]))

Expand Down