Merge "Fix error handling for non-object JSON error bodies"
This commit is contained in:
commit
2e240a7245
3 files changed
+33
No files matched your search
@@ -0,0 +1,7 @@
|
||||
---
|
||||
fixes:
|
||||
- |
|
||||
Fixes ``AttributeError: 'str' object has no attribute 'get'`` during
|
||||
error handling. This occurs when a service returns an error response
|
||||
whose JSON body is not an object (for example, a plain string or
|
||||
``null``) instead of a Redfish error object.
|
||||
@@ -103,6 +103,8 @@ class HTTPError(SushyError):
|
||||
self.status_code = response.status_code
|
||||
try:
|
||||
body = response.json()
|
||||
if not isinstance(body, dict):
|
||||
raise ValueError
|
||||
except ValueError:
|
||||
LOG.warning('Error response from %(method)s %(url)s '
|
||||
'with status code %(code)s has no JSON body',
|
||||
|
||||
@@ -485,6 +485,30 @@ class ConnectorOpTestCase(base.TestCase):
|
||||
self.assertIn('unsupported media type',
|
||||
exc.extended_info['Resolution'])
|
||||
|
||||
def test_known_http_error_string_body(self):
|
||||
self.request.return_value.status_code = http_client.BAD_REQUEST
|
||||
self.request.return_value.json.return_value = 'EOF'
|
||||
|
||||
with self.assertRaisesRegex(exceptions.BadRequestError,
|
||||
'unknown error') as cm:
|
||||
self.conn._op('POST', 'http://foo.bar')
|
||||
exc = cm.exception
|
||||
self.assertEqual(http_client.BAD_REQUEST, exc.status_code)
|
||||
self.assertIsNone(exc.body)
|
||||
self.assertIsNone(exc.detail)
|
||||
|
||||
def test_known_http_error_null_body(self):
|
||||
self.request.return_value.status_code = http_client.BAD_REQUEST
|
||||
self.request.return_value.json.return_value = None
|
||||
|
||||
with self.assertRaisesRegex(exceptions.BadRequestError,
|
||||
'unknown error') as cm:
|
||||
self.conn._op('POST', 'http://foo.bar')
|
||||
exc = cm.exception
|
||||
self.assertEqual(http_client.BAD_REQUEST, exc.status_code)
|
||||
self.assertIsNone(exc.body)
|
||||
self.assertIsNone(exc.detail)
|
||||
|
||||
@mock.patch('time.sleep', autospec=True)
|
||||
def test_not_found_error(self, mock_sleep):
|
||||
self.request.return_value.status_code = http_client.NOT_FOUND
|
||||
|
||||
Reference in new issue
Block a user