Provide a clear error message when trying to access ironic (not inspector)
Apparently, it's a common enough case, especially with standalone ironic. Change-Id: Ic0ffa49aeb2f13cfd08b6027f3b61f2fcc750b4d
This commit is contained in:
parent
0f65bcf699
commit
5176452b49
|
@ -47,12 +47,31 @@ class ClientError(requests.HTTPError):
|
|||
# inspector returns error message in body
|
||||
msg = response.content.decode(_ERROR_ENCODING)
|
||||
try:
|
||||
msg = json.loads(msg)['error']['message']
|
||||
msg = json.loads(msg)
|
||||
except ValueError:
|
||||
LOG.debug('Old style error response returned, assuming '
|
||||
'ironic-discoverd')
|
||||
except (KeyError, TypeError):
|
||||
except TypeError:
|
||||
LOG.exception('Bad error response from Ironic Inspector')
|
||||
else:
|
||||
try:
|
||||
msg = msg['error']['message']
|
||||
except KeyError as exc:
|
||||
LOG.error('Invalid error response from Ironic Inspector: '
|
||||
'%(msg)s (missing key %(key)s)',
|
||||
{'msg': msg, 'key': exc})
|
||||
# It's surprisingly common to try accessing ironic URL with
|
||||
# ironic-inspector-client, handle this case
|
||||
try:
|
||||
msg = msg['error_message']
|
||||
except KeyError:
|
||||
pass
|
||||
else:
|
||||
msg = _('Received Ironic-style response %s. Are you '
|
||||
'trying to access Ironic URL instead of Ironic '
|
||||
'Inspector?') % msg
|
||||
except TypeError:
|
||||
LOG.exception('Bad error response from Ironic Inspector')
|
||||
|
||||
LOG.debug('Inspector returned error "%(msg)s" (HTTP %(code)s)',
|
||||
{'msg': msg, 'code': response.status_code})
|
||||
|
|
|
@ -192,3 +192,28 @@ class TestRequest(unittest.TestCase):
|
|||
|
||||
self.assertRaisesRegex(http.ClientError, 'boom',
|
||||
self.get_client().request, 'get', 'url')
|
||||
|
||||
def test_accessing_ironic(self):
|
||||
self.req.return_value.status_code = 400
|
||||
self.req.return_value.content = json.dumps(
|
||||
{"error_message": "{\"code\": 404, \"title\": \"Not Found\", "
|
||||
"\"description\": \"\"}"}).encode('utf-8')
|
||||
|
||||
self.assertRaisesRegex(http.ClientError,
|
||||
'Ironic-style response.*Not Found',
|
||||
self.get_client().request, 'get', 'url')
|
||||
|
||||
def test_error_non_sense(self):
|
||||
self.req.return_value.status_code = 400
|
||||
self.req.return_value.content = json.dumps(
|
||||
{'hello': 'world'}).encode('utf-8')
|
||||
|
||||
self.assertRaisesRegex(http.ClientError, 'hello',
|
||||
self.get_client().request, 'get', 'url')
|
||||
|
||||
def test_error_non_sense2(self):
|
||||
self.req.return_value.status_code = 400
|
||||
self.req.return_value.content = b'42'
|
||||
|
||||
self.assertRaisesRegex(http.ClientError, '42',
|
||||
self.get_client().request, 'get', 'url')
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
fixes:
|
||||
- |
|
||||
Provides a clear error message when trying to access an ironic URL with
|
||||
ironic-inspector-client.
|
Loading…
Reference in New Issue