From 4913b61713e39e7252c914c5fd476a71a9176106 Mon Sep 17 00:00:00 2001 From: Gregory Thiemonge Date: Mon, 23 Nov 2020 15:46:17 +0100 Subject: [PATCH] Fix exception parsing when using WSME Services that use WSME return errors using a json document that contains 'faultstring' in the root object instead of a dict within a dict. Change-Id: I25d8c64860de7eafb8d543ebcb519fc9664d563e Story: 2008381 Task: 41303 --- openstack/exceptions.py | 3 +++ openstack/tests/unit/test_exceptions.py | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/openstack/exceptions.py b/openstack/exceptions.py index f449e86dd..e2b1ca309 100644 --- a/openstack/exceptions.py +++ b/openstack/exceptions.py @@ -211,6 +211,9 @@ def raise_from_response(response, error_message=None): try: content = response.json() messages = [_extract_message(obj) for obj in content.values()] + if not any(messages): + # Exception dict may be the root dict in projects that use WSME + messages = [_extract_message(content)] # Join all of the messages together nicely and filter out any # objects that don't have a "message" attr. details = '\n'.join(msg for msg in messages if msg) diff --git a/openstack/tests/unit/test_exceptions.py b/openstack/tests/unit/test_exceptions.py index a3286719c..cf2d74736 100644 --- a/openstack/tests/unit/test_exceptions.py +++ b/openstack/tests/unit/test_exceptions.py @@ -213,3 +213,21 @@ class TestRaiseFromResponse(base.TestCase): self.assertEqual(response.status_code, exc.status_code) self.assertEqual(self.message, exc.details) self.assertIn(self.message, str(exc)) + + def test_raise_wsme_format(self): + response = mock.Mock() + response.status_code = 404 + response.headers = { + 'content-type': 'application/json', + } + response.json.return_value = { + 'faultstring': self.message, + 'faultcode': 'Client', + 'debuginfo': None, + } + exc = self.assertRaises(exceptions.NotFoundException, + self._do_raise, response, + error_message=self.message) + self.assertEqual(response.status_code, exc.status_code) + self.assertEqual(self.message, exc.details) + self.assertIn(self.message, str(exc))