Translate requests exception to IronicException

This patch is just adding an exception handler around the post() method
from the requests library so we can: Translate it into an IronicException
and log a better error message.

Closes-Bug: #1559209
Change-Id: I17f181fa759246313d36c183a8bd8f9b3d519669
This commit is contained in:
Lucas Alvares Gomes 2016-03-18 16:52:49 +00:00
parent 8e81b964a5
commit ea238dcffa
2 changed files with 26 additions and 3 deletions

View File

@ -67,9 +67,15 @@ class AgentClient(object):
}
LOG.debug('Executing agent command %(method)s for node %(node)s',
{'node': node.uuid, 'method': method})
response = self.session.post(url,
params=request_params,
data=body)
try:
response = self.session.post(url, params=request_params, data=body)
except requests.RequestException as e:
msg = (_('Error invoking agent command %(method)s for node '
'%(node)s. Error: %(error)s') %
{'method': method, 'node': node.uuid, 'error': e})
LOG.error(msg)
raise exception.IronicException(msg)
# TODO(russellhaering): real error handling
try:

View File

@ -118,6 +118,23 @@ class TestAgentClient(base.TestCase):
data=body,
params={'wait': 'false'})
def test__command_fail_post(self):
error = 'Boom'
self.client.session.post.side_effect = requests.RequestException(error)
method = 'foo.bar'
params = {}
self.client._get_command_url(self.node)
self.client._get_command_body(method, params)
e = self.assertRaises(exception.IronicException,
self.client._command,
self.node, method, params)
self.assertEqual('Error invoking agent command %(method)s for node '
'%(node)s. Error: %(error)s' %
{'method': method, 'node': self.node.uuid,
'error': error}, str(e))
def test_get_commands_status(self):
with mock.patch.object(self.client.session, 'get',
autospec=True) as mock_get: