Remove some not-so-useful internal exception types

Some exception types are consolidated into a more generic version. The
intent is to make the code more maintainable. The internal errors or
exceptions only make sense to developers.

Change-Id: I795f2803e57974ca803234dd593d5bd91f420be0
This commit is contained in:
tengqm 2015-07-19 23:51:17 -04:00
parent e1c3761c72
commit 8bba5b7df3
5 changed files with 122 additions and 111 deletions

View File

@ -61,7 +61,7 @@ class TrustMiddleware(wsgi.Middleware):
# Create a trust if no existing one found
try:
trust = kc.trust_create(ctx.user, admin_id, ctx.project)
except exception.TrustCreationFailure as ex:
except exception.ResourceCreationFailure as ex:
raise webob.exc.HTTPInternalServerError(six.text_type(ex))
# update cache

View File

@ -260,18 +260,19 @@ class ResourceBusyError(InternalError):
msg_fmt = _("The %(resource_type)s (%(resource_id)s) is busy now.")
class UserNotFound(InternalError):
# Internal exception, not to be exposed to end user.
msg_fmt = _("The user (%(user)s) could not be found.")
class TrustNotFound(InternalError):
# Internal exception, not to be exposed to end user.
msg_fmt = _("The trust for trustor (%(trustor)s) could not be found.")
class TrustCreationFailure(InternalError):
msg_fmt = _("Failed in creating trust: %(reason)s.")
class ResourceCreationFailure(InternalError):
# Used when creating resources in other services
msg_fmt = _("Failed in creating %(rtype)s.")
class ResourceDeletionFailure(InternalError):
# Used when deleting resources from other services
msg_fmt = _("Failed in deleting %(resource)s.")
class ResourceNotFound(InternalError):

View File

@ -50,8 +50,10 @@ class KeystoneClient(base.DriverBase):
def user_get_by_name(self, user_name):
try:
user = self.conn.identity.find_user(user_name)
except sdk.exc.HttpException:
raise exception.UserNotFound(user=user_name)
except sdk.exc.HttpException as ex:
LOG.exception(_('Failed in getting user: %s'), six.text_type(ex))
res = _('user:%s') % user_name
raise exception.ResourceNotFound(resource=res)
return user.id
@ -151,7 +153,9 @@ class KeystoneClient(base.DriverBase):
try:
result = self.conn.identity.create_trust(**params)
except sdk.exc.HttpException as ex:
raise exception.TrustCreationFailure(reason=six.text_type(ex))
LOG.exception(_('Failed in creating trust: %s'),
six.text_type(ex))
raise exception.ResourceCreationFailure(rtype='trust')
return result

View File

@ -31,9 +31,10 @@ class NeutronClient(base.DriverBase):
try:
network = self.conn.network.find_network(name_or_id)
except sdk.exc.HttpException as ex:
msg = _('Failed in getting network %(value)s: %(ex)s'
) % {'value': name_or_id, 'ex': six.text_type(ex)}
raise exception.Error(msg=msg)
LOG.exception(_('Failed in getting network: %s'),
six.text_type(ex))
res = _('network:%s') % name_or_id
raise exception.ResourceNotFound(resource=res)
return network
@ -41,11 +42,10 @@ class NeutronClient(base.DriverBase):
try:
subnet = self.conn.network.find_subnet(name_or_id)
except sdk.exc.HttpException as ex:
msg = _('Failed in getting subnet %(value)s: %(ex)s'
) % {'value': name_or_id, 'ex': six.text_type(ex)}
# TODO(Yanyan Hu): choose more proper exception type,
# e.g. ResourceNotFound.
raise exception.Error(msg=msg)
LOG.exception(_('Failed in getting subnet: %s.'),
six.text_type(ex))
res = _('subnet:%s') % name_or_id
raise exception.ResourceNotFound(resource=res)
return subnet
@ -53,9 +53,10 @@ class NeutronClient(base.DriverBase):
try:
lb = self.conn.network.find_load_balancer(name_or_id)
except sdk.exc.HttpException as ex:
msg = _('Failed in getting loadbalancer %(value)s: %(ex)s'
) % {'value': name_or_id, 'ex': six.text_type(ex)}
raise exception.Error(msg=msg)
LOG.exception(_('Failed in getting loadbalancer: %s.'),
six.text_type(ex))
res = _('loadbalancer:%s') % name_or_id
raise exception.ResourceNotFound(resource=res)
return lb
@ -63,9 +64,9 @@ class NeutronClient(base.DriverBase):
try:
lbs = [lb for lb in self.conn.network.load_balancers()]
except sdk.exc.HttpException as ex:
msg = _('Failed in listing loadbalancer: %(ex)s'
) % {'ex': six.text_type(ex)}
raise exception.Error(msg=msg)
LOG.exception(_('Failed in getting loadbalancer: %s.'),
six.text_type(ex))
raise exception.ResourceNotFound(resource=_('loadbalancer:*'))
return lbs
@ -87,9 +88,10 @@ class NeutronClient(base.DriverBase):
try:
res = self.conn.network.create_load_balancer(**kwargs)
except sdk.exc.HttpException as ex:
msg = _('Failed in creating loadbalancer: %(ex)s'
) % {'ex': six.text_type(ex)}
raise exception.Error(msg=msg)
msg = six.text_type(ex)
LOG.exception(_('Failed in creating loadbalancer: %s.'), msg)
raise exception.ResourceCreationFailure(rtype='loadbalancer',
reason=msg)
return res
@ -98,9 +100,10 @@ class NeutronClient(base.DriverBase):
self.conn.network.delete_load_balancer(
lb_id, ignore_missing=ignore_missing)
except sdk.exc.HttpException as ex:
msg = _('Failed in deleting loadbalancer %(id)s: %(ex)s'
) % {'id': lb_id, 'ex': six.text_type(ex)}
raise exception.Error(msg=msg)
LOG.exception(_('Failed in deleting loadbalancer: %s.'),
six.text_type(ex))
res = _('loadbalancer:%s') % lb_id
raise exception.ResourceDeletionFailure(resource=res)
return
@ -108,9 +111,10 @@ class NeutronClient(base.DriverBase):
try:
listener = self.conn.network.find_listener(name_or_id)
except sdk.exc.HttpException as ex:
msg = _('Failed in getting lb listener %(value)s: %(ex)s'
) % {'value': name_or_id, 'ex': six.text_type(ex)}
raise exception.Error(msg=msg)
LOG.exception(_('Failed in getting lb listener: %s.'),
six.text_type(ex))
res = _('listener:%s') % name_or_id
raise exception.ResourceNotFound(resource=res)
return listener
@ -118,9 +122,9 @@ class NeutronClient(base.DriverBase):
try:
listeners = [i for i in self.conn.network.listeners()]
except sdk.exc.HttpException as ex:
msg = _('Failed in listing lb listener: %(ex)s'
) % {'ex': six.text_type(ex)}
raise exception.Error(msg=msg)
LOG.exception(_('Failed in listing lb listener: %s.'),
six.text_type(ex))
raise exception.ResourceNotFound(resource=_('listener:*'))
return listeners
@ -145,9 +149,9 @@ class NeutronClient(base.DriverBase):
try:
res = self.conn.network.create_listener(**kwargs)
except sdk.exc.HttpException as ex:
msg = _('Failed in creating lb listener: %(ex)s'
) % {'ex': six.text_type(ex)}
raise exception.Error(msg=msg)
LOG.exception(_('Failed in creating lb listener: %s.'),
six.text_type(ex))
raise exception.ResourceCreationFailure(rtype='listener')
return res
@ -156,9 +160,10 @@ class NeutronClient(base.DriverBase):
self.conn.network.delete_listener(listener_id,
ignore_missing=ignore_missing)
except sdk.exc.HttpException as ex:
msg = _('Failed in deleting lb listener %(id)s: %(ex)s'
) % {'id': listener_id, 'ex': six.text_type(ex)}
raise exception.Error(msg=msg)
LOG.exception(_('Failed in deleting lb listener: %s.'),
six.text_type(ex))
res = _('listener:%s') % listener_id
raise exception.ResourceDeletionFailure(resource=res)
return
@ -166,9 +171,10 @@ class NeutronClient(base.DriverBase):
try:
pool = self.conn.network.find_pool(name_or_id)
except sdk.exc.HttpException as ex:
msg = _('Failed in getting lb pool %(value)s: %(ex)s'
) % {'value': name_or_id, 'ex': six.text_type(ex)}
raise exception.Error(msg=msg)
LOG.exception(_('Failed in getting lb pool: %s.'),
six.text_type(ex))
res = _('pool:%s') % name_or_id
raise exception.ResourceNotFound(resource=res)
return pool
@ -176,9 +182,9 @@ class NeutronClient(base.DriverBase):
try:
pools = [p for p in self.conn.network.pools()]
except sdk.exc.HttpException as ex:
msg = _('Failed in listing lb pool: %(ex)s'
) % {'ex': six.text_type(ex)}
raise exception.Error(msg=msg)
LOG.exception(_('Failed in listing lb pool: %s.'),
six.text_type(ex))
raise exception.ResourceNotFound(resource=_('pool:*'))
return pools
@ -200,9 +206,9 @@ class NeutronClient(base.DriverBase):
try:
res = self.conn.network.create_pool(**kwargs)
except sdk.exc.HttpException as ex:
msg = _('Failed in creating lb pool: %(ex)s'
) % {'ex': six.text_type(ex)}
raise exception.Error(msg=msg)
LOG.exception(_('Failed in creating lb pool: %s.'),
six.text_type(ex))
raise exception.ResourceCreationFailure(rtype='pool')
return res
@ -211,9 +217,10 @@ class NeutronClient(base.DriverBase):
self.conn.network.delete_pool(pool_id,
ignore_missing=ignore_missing)
except sdk.exc.HttpException as ex:
msg = _('Failed in deleting lb pool %(id)s: %(ex)s'
) % {'id': pool_id, 'ex': six.text_type(ex)}
raise exception.Error(msg=msg)
LOG.exception(_('Failed in deleting lb pool: %s.'),
six.text_type(ex))
res = _('pool:%s') % pool_id
raise exception.ResourceDeletionFailure(resource=res)
return
@ -222,9 +229,10 @@ class NeutronClient(base.DriverBase):
member = self.conn.network.find_pool_member(name_or_id,
pool_id)
except sdk.exc.HttpException as ex:
msg = _('Failed in getting lb pool_member %(value)s: %(ex)s'
) % {'value': name_or_id, 'ex': six.text_type(ex)}
raise exception.Error(msg=msg)
LOG.exception(_('Failed in getting lb pool member: %s.'),
six.text_type(ex))
res = _('member:%(p)s:%(m)s') % {'p': pool_id, 'm': name_or_id}
raise exception.ResourceNotFound(resource=res)
return member
@ -232,9 +240,10 @@ class NeutronClient(base.DriverBase):
try:
members = [m for m in self.conn.network.pool_members(pool_id)]
except sdk.exc.HttpException as ex:
msg = _('Failed in listing lb members of pool %(id)s: %(ex)s'
) % {'id': pool_id, 'ex': six.text_type(ex)}
raise exception.Error(msg=msg)
LOG.exception(_('Failed in listing lb pool member: %s.'),
six.text_type(ex))
res = _('member:%s:*') % pool_id
raise exception.ResourceNotFound(resource=res)
return members
@ -255,9 +264,9 @@ class NeutronClient(base.DriverBase):
try:
res = self.conn.network.create_pool_member(**kwargs)
except sdk.exc.HttpException as ex:
msg = _('Failed in adding member to lb pool %(id)s: %(ex)s'
) % {'id': pool_id, 'ex': six.text_type(ex)}
raise exception.Error(msg=msg)
LOG.exception(_('Failed in creating lb pool member: %s.'),
six.text_type(ex))
raise exception.ResourceCreationFailure(rtype='lb:member')
return res
@ -266,10 +275,10 @@ class NeutronClient(base.DriverBase):
self.conn.network.delete_pool_member(
member_id, pool_id, ignore_missing=ignore_missing)
except sdk.exc.HttpException as ex:
msg = _('Failed in deleting lb member %(id)s from pool %(pool)s: '
'%(ex)s') % {'id': member_id, 'pool': pool_id,
'ex': six.text_type(ex)}
raise exception.Error(msg=msg)
LOG.exception(_('Failed in deleting lb pool member: %s.'),
six.text_type(ex))
res = _('member:%(p)s:%(m)s') % {'p': pool_id, 'm': member_id}
raise exception.ResourceDeletionFailure(resource=res)
return
@ -277,9 +286,10 @@ class NeutronClient(base.DriverBase):
try:
hm = self.conn.network.find_health_monitor(name_or_id)
except sdk.exc.HttpException as ex:
msg = _('Failed in getting lb healthmonitor %(value)s: %(ex)s'
) % {'value': name_or_id, 'ex': six.text_type(ex)}
raise exception.Error(msg=msg)
LOG.exception(_('Failed in getting lb health-monitor: %s.'),
six.text_type(ex))
res = _('healthmonitor:%s') % name_or_id
raise exception.ResourceNotFound(resource=res)
return hm
@ -287,9 +297,9 @@ class NeutronClient(base.DriverBase):
try:
hms = [hm for hm in self.conn.network.list_health_monitors()]
except sdk.exc.HttpException as ex:
msg = _('Failed in listing lb healthmonitor: %(ex)s'
) % {'ex': six.text_type(ex)}
raise exception.Error(msg=msg)
LOG.exception(_('Failed in listing lb health-monitor: %s.'),
six.text_type(ex))
raise exception.ResourceNotFound(resource=_('healthmonitor:*'))
return hms
@ -317,9 +327,9 @@ class NeutronClient(base.DriverBase):
try:
res = self.conn.network.create_pool(**kwargs)
except sdk.exc.HttpException as ex:
msg = _('Failed in creating lb healthmonitor: %(ex)s'
) % {'ex': six.text_type(ex)}
raise exception.Error(msg=msg)
LOG.exception(_('Failed in creating lb health-monitor: %s.'),
six.text_type(ex))
raise exception.ResourceCreationFailure(rtype='healthmonitor')
return res
@ -328,8 +338,9 @@ class NeutronClient(base.DriverBase):
self.conn.network.delete_health_monitor(
hm_id, ignore_missing=ignore_missing)
except sdk.exc.HttpException as ex:
msg = _('Failed in deleting lb healthmonitor %(id)s: %(ex)s'
) % {'id': hm_id, 'ex': six.text_type(ex)}
raise exception.Error(msg=msg)
LOG.exception(_('Failed in deleting lb health-monitor: %s.'),
six.text_type(ex))
res = _('healthmonitor:%s') % hm_id
raise exception.ResourceDeletionFailure(resource=res)
return

View File

@ -38,60 +38,56 @@ class TestNeutronV2Driver(base.SenlinTestCase):
mock_create_connection.assert_called_once_with(params)
def test_network_get(self):
name_or_id = 'network_identifier'
net_id = 'network_identifier'
network_obj = mock.Mock()
self.conn.network.find_network.return_value = network_obj
res = self.nc.network_get(name_or_id)
self.conn.network.find_network.assert_called_once_with(
name_or_id)
res = self.nc.network_get(net_id)
self.conn.network.find_network.assert_called_once_with(net_id)
self.assertEqual(network_obj, res)
exception_info = 'Exception happened when getting network.'
fake_exception = sdk.exc.HttpException(exception_info)
self.conn.network.find_network.side_effect = fake_exception
ex = self.assertRaises(exception.Error, self.nc.network_get,
name_or_id)
msg = _('Failed in getting network %(value)s: HttpException: '
'%(ex)s') % {'value': name_or_id, 'ex': exception_info}
ex = self.assertRaises(exception.ResourceNotFound,
self.nc.network_get, net_id)
msg = _('The resource (network:%s) could not be found.') % net_id
self.assertEqual(msg, six.text_type(ex))
def test_subnet_get(self):
name_or_id = 'subnet_identifier'
subnet_id = 'subnet_identifier'
subnet_obj = mock.Mock()
self.conn.network.find_subnet.return_value = subnet_obj
res = self.nc.subnet_get(name_or_id)
self.conn.network.find_subnet.assert_called_once_with(
name_or_id)
res = self.nc.subnet_get(subnet_id)
self.conn.network.find_subnet.assert_called_once_with(subnet_id)
self.assertEqual(subnet_obj, res)
exception_info = 'Exception happened when getting subnet.'
fake_exception = sdk.exc.HttpException(exception_info)
self.conn.network.find_subnet.side_effect = fake_exception
ex = self.assertRaises(exception.Error, self.nc.subnet_get,
name_or_id)
msg = _('Failed in getting subnet %(value)s: HttpException: '
'%(ex)s') % {'value': name_or_id, 'ex': exception_info}
ex = self.assertRaises(exception.ResourceNotFound,
self.nc.subnet_get, subnet_id)
msg = _('The resource (subnet:%s) could not be found.') % subnet_id
self.assertEqual(msg, six.text_type(ex))
def test_loadbalancer_get(self):
name_or_id = 'loadbalancer_identifier'
lb_id = 'loadbalancer_identifier'
loadbalancer_obj = mock.Mock()
self.conn.network.find_load_balancer.return_value = loadbalancer_obj
res = self.nc.loadbalancer_get(name_or_id)
self.conn.network.find_load_balancer.assert_called_once_with(
name_or_id)
res = self.nc.loadbalancer_get(lb_id)
self.conn.network.find_load_balancer.assert_called_once_with(lb_id)
self.assertEqual(loadbalancer_obj, res)
exception_info = 'Exception happened when getting loadbalancer.'
fake_exception = sdk.exc.HttpException(exception_info)
self.conn.network.find_load_balancer.side_effect = fake_exception
ex = self.assertRaises(exception.Error, self.nc.loadbalancer_get,
name_or_id)
msg = _('Failed in getting loadbalancer %(value)s: HttpException: '
'%(ex)s') % {'value': name_or_id, 'ex': exception_info}
ex = self.assertRaises(exception.ResourceNotFound,
self.nc.loadbalancer_get, lb_id)
msg = _('The resource (loadbalancer:%s) could not be found.') % lb_id
self.assertEqual(msg, six.text_type(ex))
def test_loadbalancer_list(self):
@ -104,9 +100,9 @@ class TestNeutronV2Driver(base.SenlinTestCase):
exception_info = 'Exception happened when listing loadbalancer.'
fake_exception = sdk.exc.HttpException(exception_info)
self.conn.network.load_balancers.side_effect = fake_exception
ex = self.assertRaises(exception.Error, self.nc.loadbalancer_list)
msg = _('Failed in listing loadbalancer: HttpException: %(ex)s'
) % {'ex': exception_info}
ex = self.assertRaises(exception.ResourceNotFound,
self.nc.loadbalancer_list)
msg = _('The resource (loadbalancer:*) could not be found.')
self.assertEqual(msg, six.text_type(ex))
def test_loadbalancer_create(self):
@ -139,10 +135,9 @@ class TestNeutronV2Driver(base.SenlinTestCase):
exception_info = 'Exception happened when creating loadbalancer.'
fake_exception = sdk.exc.HttpException(exception_info)
self.conn.network.create_load_balancer.side_effect = fake_exception
ex = self.assertRaises(exception.Error, self.nc.loadbalancer_create,
vip_subnet_id)
msg = _('Failed in creating loadbalancer: HttpException: %(ex)s'
) % {'ex': exception_info}
ex = self.assertRaises(exception.ResourceCreationFailure,
self.nc.loadbalancer_create, vip_subnet_id)
msg = _('Failed in creating loadbalancer.')
self.assertEqual(msg, six.text_type(ex))
def test_loadbalancer_delete(self):