From 3a4d6ecbcf084a8715842ad9e4e2131c36ce098f Mon Sep 17 00:00:00 2001 From: Thomas Herve Date: Wed, 9 Mar 2016 14:51:07 +0100 Subject: [PATCH] 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 --- barbicanclient/client.py | 3 +++ barbicanclient/tests/test_client.py | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/barbicanclient/client.py b/barbicanclient/client.py index 29ab60d5..e38276b4 100644 --- a/barbicanclient/client.py +++ b/barbicanclient/client.py @@ -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 diff --git a/barbicanclient/tests/test_client.py b/barbicanclient/tests/test_client.py index a27cbc60..e862c5a6 100644 --- a/barbicanclient/tests/test_client.py +++ b/barbicanclient/tests/test_client.py @@ -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):