Display error description

When an error happens, display both the title and the description of the
returned error if they exist, to give more context to the user.

Change-Id: I84457bfe2d55a877e72a7fcb6dd6ba54c557c45e
Closes-Bug: #1555102
This commit is contained in:
Thomas Herve
2016-03-09 14:51:07 +01:00
parent 4db1aec97d
commit 3a4d6ecbcf
2 changed files with 10 additions and 0 deletions

View File

@@ -113,6 +113,9 @@ class _HTTPClient(adapter.Adapter):
try:
response_data = resp.json()
message = response_data['title']
description = response_data.get('description')
if description:
message = '{0}: {1}'.format(message, description)
except ValueError:
message = resp.content
return message

View File

@@ -265,6 +265,13 @@ class WhenTestingGetErrorMessage(TestClient):
msg = self.httpclient._get_error_message(resp)
self.assertEqual(msg, content)
def test_gets_error_message_from_description_in_json(self):
resp = mock.MagicMock()
resp.json.return_value = {'title': 'test_text',
'description': 'oopsie'}
msg = self.httpclient._get_error_message(resp)
self.assertEqual(msg, 'test_text: oopsie')
class BaseEntityResource(testtools.TestCase):