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
This commit is contained in:
Gregory Thiemonge 2020-11-23 15:46:17 +01:00
parent 0091bfc929
commit 4913b61713
2 changed files with 21 additions and 0 deletions

View File

@ -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)

View File

@ -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))