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

Retain error details in string representation of errors generated by ApiTools. #266

Merged
merged 1 commit into from
May 7, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions apitools/base/py/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,23 @@ class HttpError(CommunicationError):

def __init__(self, response, content, url,
method_config=None, request=None):
super(HttpError, self).__init__()
error_message = HttpError._build_message(response, content, url)
super(HttpError, self).__init__(error_message)
self.response = response
self.content = content
self.url = url
self.method_config = method_config
self.request = request

def __str__(self):
content = self.content
return HttpError._build_message(self.response, self.content, self.url)

@staticmethod
def _build_message(response, content, url):
if isinstance(content, bytes):
content = self.content.decode('ascii', 'replace')
content = content.decode('ascii', 'replace')
return 'HttpError accessing <%s>: response: <%s>, content <%s>' % (
self.url, self.response, content)
url, response, content)

@property
def status_code(self):
Expand Down
6 changes: 6 additions & 0 deletions apitools/base/py/exceptions_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ def testForbidden(self):
self.assertIsInstance(err, exceptions.HttpForbiddenError)
self.assertEquals(err.status_code, 403)

def testExceptionMessageIncludesErrorDetails(self):
err = exceptions.HttpError.FromResponse(_MakeResponse(403))
self.assertIn('403', repr(err))
self.assertIn('http://www.google.com', repr(err))
self.assertIn('{"field": "abc"}', repr(err))

def testNotFound(self):
err = exceptions.HttpError.FromResponse(_MakeResponse(404))
self.assertIsInstance(err, exceptions.HttpError)
Expand Down