GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix: Improve support for error_details by parthea · Pull Request #1126 · googleapis/google-api-python-client

fix: Improve support for error_details #1126

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.

Sign up for GitHub

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

Merged
gcf-merge-on-green merged 3 commits into googleapis:master from parthea:allow-non-json-errors
Dec 9, 2020

Uh oh!

There was an error while loading. Please reload this page.

Merged

fix: Improve support for error_details #1126

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
3 commits
Select commit Hold shift + click to select a range
8746a7d
fix: Improve support for error_details(HttpErrors)
parthea Dec 6, 2020
64849b7
Catch JSONDecodeError instead of all exceptions
parthea Dec 8, 2020
4ad9969
Merge branch 'master' into allow-non-json-errors
parthea Dec 9, 2020
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

8 changes: 7 additions & 1 deletion 8 googleapiclient/errors.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ def _get_reason(self):
"""Calculate the reason for the error from the response content."""
reason = self.resp.reason
try:
data = json.loads(self.content.decode("utf-8"))
try:
data = json.loads(self.content.decode("utf-8"))
except json.JSONDecodeError:
# In case it is not json
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"]), "")
Expand All @@ -59,6 +63,8 @@ def _get_reason(self):
reason = first_error["error"]["message"]
if "details" in first_error["error"]:
self.error_details = first_error["error"]["details"]
else:
self.error_details = data
except (ValueError, KeyError, TypeError):
pass
if reason is None:
Expand Down
10 changes: 5 additions & 5 deletions 10 tests/test_errors.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def test_bad_json_body(self):
b"{", {"status": "400", "content-type": "application/json"}, reason="Failed"
)
error = HttpError(resp, content)
self.assertEqual(str(error), '<HttpError 400 "Failed">')
self.assertEqual(str(error), '<HttpError 400 when requesting None returned "Failed". Details: "{">')

def test_with_uri(self):
"""Test handling of passing in the request uri."""
Expand All @@ -106,7 +106,7 @@ def test_with_uri(self):
error = HttpError(resp, content, uri="http://example.org")
self.assertEqual(
str(error),
'<HttpError 400 when requesting http://example.org returned "Failure">',
'<HttpError 400 when requesting http://example.org returned "Failure". Details: "{">',
)

def test_missing_message_json_body(self):
Expand All @@ -121,15 +121,15 @@ def test_missing_message_json_body(self):

def test_non_json(self):
"""Test handling of non-JSON bodies"""
resp, content = fake_response(b"}NOT OK", {"status": "400"})
resp, content = fake_response(b"Invalid request", {"status": "400"})
error = HttpError(resp, content)
self.assertEqual(str(error), '<HttpError 400 "Ok">')
self.assertEqual(str(error), '<HttpError 400 when requesting None returned "Ok". Details: "Invalid request">')

def test_missing_reason(self):
"""Test an empty dict with a missing resp.reason."""
resp, content = fake_response(b"}NOT OK", {"status": "400"}, reason=None)
error = HttpError(resp, content)
self.assertEqual(str(error), '<HttpError 400 "">')
self.assertEqual(str(error), '<HttpError 400 when requesting None returned "". Details: "}NOT OK">')

def test_error_detail_for_missing_message_in_error(self):
"""Test handling of data with missing 'details' or 'detail' element."""
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.