Skip to content

Commit

Permalink
feat: Adds support for errors.py to also use 'errors' for error_detai…
Browse files Browse the repository at this point in the history
…ls (#1281)

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 #1279 🦕
  • Loading branch information
vinay-google committed Apr 3, 2021
1 parent cc717a1 commit a5d2081
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion googleapiclient/errors.py
Expand Up @@ -61,7 +61,7 @@ def _get_reason(self):
data = self.content.decode("utf-8")
if isinstance(data, dict):
reason = data["error"]["message"]
error_detail_keyword = next((kw for kw in ["detail", "details", "message"] if kw in data["error"]), "")
error_detail_keyword = next((kw for kw in ["detail", "details", "errors", "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:
Expand Down
5 changes: 3 additions & 2 deletions tests/test_errors.py
Expand Up @@ -141,5 +141,6 @@ def test_error_detail_for_missing_message_in_error(self):
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')
expected_error_details = "[{'domain': 'global', 'reason': 'required', 'message': 'country is required', 'locationType': 'parameter', 'location': 'country'}]"
self.assertEqual(str(error), '<HttpError 400 when requesting None returned "country is required". Details: "%s">' % expected_error_details)
self.assertEqual(str(error.error_details), expected_error_details)
2 changes: 1 addition & 1 deletion tests/test_http.py
Expand Up @@ -1587,7 +1587,7 @@ def test_execute_batch_http_error(self):
"<HttpError 403 when requesting "
"https://www.googleapis.com/someapi/v1/collection/?foo=bar returned "
'"Access Not Configured". '
'Details: "Access Not Configured">'
"Details: \"[{'domain': 'usageLimits', 'reason': 'accessNotConfigured', 'message': 'Access Not Configured', 'debugInfo': 'QuotaState: BLOCKED'}]\">"
)
self.assertEqual(expected, str(callbacks.exceptions["2"]))

Expand Down

0 comments on commit a5d2081

Please sign in to comment.