Use string exception casting everywhere

Instead of the deprecated "message" member access,
casting to a string invokes the __str__ method of the exception
that is wired to return the message

Added a test of the failure cases of IpRouteCommand::delete_gateway
because they were missing

Running unit and functional tests locally no longer shows the warning
reported in the bug.

Change-Id: Ia79f526aa973ece1145615d65349f860aa3fd465
Closes-Bug: #1466542
This commit is contained in:
Martin Roy 2015-06-18 13:45:02 -04:00
parent ffb3658d0e
commit fe6654b250
11 changed files with 29 additions and 15 deletions

View File

@ -482,7 +482,7 @@ class IpRouteCommand(IpDeviceCommandBase):
self._as_root([ip_version], tuple(args))
except RuntimeError as rte:
with (excutils.save_and_reraise_exception()) as ctx:
if "Cannot find device" in rte.message:
if "Cannot find device" in str(rte):
ctx.reraise = False
raise exceptions.DeviceNotFoundError(
device_name=self.name)

View File

@ -182,7 +182,7 @@ def find_child_pids(pid):
# Unexpected errors are the responsibility of the caller
with excutils.save_and_reraise_exception() as ctxt:
# Exception has already been logged by execute
no_children_found = 'Exit code: 1' in e.message
no_children_found = 'Exit code: 1' in str(e)
if no_children_found:
ctxt.reraise = False
return []

View File

@ -179,7 +179,7 @@ def translate(translatable, locale):
elif isinstance(translatable, webob.exc.HTTPError):
translatable.detail = localize(translatable.detail, locale)
elif isinstance(translatable, Exception):
translatable.message = localize(translatable.message, locale)
translatable.message = localize(translatable, locale)
else:
return localize(translatable, locale)
return translatable

View File

@ -174,7 +174,7 @@ def ovsdb_native_supported():
except ImportError as ex:
LOG.error(_LE("Failed to import required modules. Ensure that the "
"python-openvswitch package is installed. Error: %s"),
ex.message)
ex)
except Exception as ex:
LOG.exception(six.text_type(ex))

View File

@ -108,10 +108,10 @@ class Dispatcher(object):
h_exc.BrokenInterface, h_exc.DvaCreationFailed,
h_exc.DvaCreationPending, h_exc.BrokenDva,
h_exc.ConfigurationFailed) as ex:
LOG.warning(p_con.error_map[type(ex)], ex.message)
LOG.warning(p_con.error_map[type(ex)], ex)
transient_state = p_con.Status.ERROR
except h_exc.DvaDeleteFailed as ex:
LOG.warning(p_con.error_map[type(ex)], ex.message)
LOG.warning(p_con.error_map[type(ex)], ex)
transient_state = p_con.Status.DELETED
finally:
# if the returned transient state is None, no operations

View File

@ -60,7 +60,7 @@ def _create_dva_and_assign_address(api, tenant_id, neutron_router,
neutron_router["admin_state_up"],
ip_allocation_info)
except h_exc.PreliminaryOperationsFailed as ex:
raise h_exc.BrokenInterface(err_msg=ex.message)
raise h_exc.BrokenInterface(err_msg=str(ex))
state = api.extract_dva_state(dva)
return state
@ -110,7 +110,7 @@ def _grow_dva_iface_and_assign_address(api, tenant_id, neutron_router,
neutron_router["admin_state_up"],
ip_allocation_info)
except h_exc.PreliminaryOperationsFailed as ex:
raise h_exc.BrokenInterface(err_msg=ex.message)
raise h_exc.BrokenInterface(err_msg=str(ex))
state = api.extract_dva_state(dva)
return state
@ -127,7 +127,7 @@ def _shrink_dva_iface(api, tenant_id, neutron_router, port_id):
return (p_con.Status.ACTIVE if neutron_router["admin_state_up"] else
p_con.Status.READY)
except h_exc.PreliminaryOperationsFailed as ex:
raise h_exc.BrokenInterface(err_msg=ex.message)
raise h_exc.BrokenInterface(err_msg=str(ex))
state = api.extract_dva_state(dva)
return state

View File

@ -49,7 +49,7 @@ class NamespaceManagerTestFramework(base.BaseSudoTestCase):
except RuntimeError as e:
# If the namespace didn't exist when delete was attempted, mission
# accomplished. Otherwise, re-raise the exception
if 'No such file or directory' not in e.message:
if 'No such file or directory' not in str(e):
raise e
def _namespace_exists(self, namespace):

View File

@ -271,7 +271,7 @@ class NotLockingAccounts(Accounts):
return getattr(user, cred_arg) != getattr(alt_user, cred_arg)
except exceptions.InvalidCredentials as ic:
msg = "At least one of the configured credentials is " \
"not valid: %s" % ic.message
"not valid: %s" % ic
raise exceptions.InvalidConfiguration(msg)
else:
# TODO(andreaf) Add a uniqueness check here

View File

@ -793,7 +793,7 @@ class TestIpRouteCommand(TestIPCmdBase):
'dev', self.parent.name,
'table', self.table))
def test_del_gateway(self):
def test_del_gateway_success(self):
self.route_cmd.delete_gateway(self.gateway, table=self.table)
self._assert_sudo([self.ip_version],
('del', 'default',
@ -801,6 +801,20 @@ class TestIpRouteCommand(TestIPCmdBase):
'dev', self.parent.name,
'table', self.table))
def test_del_gateway_cannot_find_device(self):
self.parent._as_root.side_effect = RuntimeError("Cannot find device")
exc = self.assertRaises(exceptions.DeviceNotFoundError,
self.route_cmd.delete_gateway,
self.gateway, table=self.table)
self.assertIn(self.parent.name, str(exc))
def test_del_gateway_other_error(self):
self.parent._as_root.side_effect = RuntimeError()
self.assertRaises(RuntimeError, self.route_cmd.delete_gateway,
self.gateway, table=self.table)
def test_get_gateway(self):
for test_case in self.test_cases:
self.parent._run = mock.Mock(return_value=test_case['sample'])

View File

@ -160,7 +160,7 @@ class ResourceExtensionTest(base.BaseTestCase):
# Shouldn't be reached
self.assertTrue(False)
except webtest.AppError as e:
self.assertIn('501', e.message)
self.assertIn('501', str(e))
def test_resource_can_be_added_as_extension(self):
res_ext = extensions.ResourceExtension(

View File

@ -77,7 +77,7 @@ class NeutronManagerTestCase(base.BaseTestCase):
"DummyServicePlugin"])
cfg.CONF.set_override("core_plugin", DB_PLUGIN_KLASS)
e = self.assertRaises(ValueError, manager.NeutronManager.get_instance)
self.assertIn(constants.DUMMY, e.message)
self.assertIn(constants.DUMMY, str(e))
def test_multiple_plugins_by_name_specified_for_service_type(self):
cfg.CONF.set_override("service_plugins", ["dummy", "dummy"])
@ -99,7 +99,7 @@ class NeutronManagerTestCase(base.BaseTestCase):
"neutron.tests.unit.test_manager."
"MultiServiceCorePlugin")
e = self.assertRaises(ValueError, manager.NeutronManager.get_instance)
self.assertIn(constants.DUMMY, e.message)
self.assertIn(constants.DUMMY, str(e))
def test_core_plugin_supports_services(self):
cfg.CONF.set_override("core_plugin",