Fix failing creation of MagnumException subclasses

Change-Id: I8100c59a5608da2703860da0ba3e6628971af5f1
This commit is contained in:
OTSUKA, Yuanying 2014-12-12 14:55:20 +09:00
parent 9d46b771c5
commit bb5615495a
2 changed files with 240 additions and 29 deletions

View File

@ -31,6 +31,7 @@ import wsme
from magnum.common import safe_utils
from magnum.openstack.common._i18n import _
from magnum.openstack.common._i18n import _LE
from magnum.openstack.common import log as logging
@ -179,35 +180,40 @@ class MagnumException(Exception):
"""Base Magnum Exception
To correctly use this class, inherit from it and define
a 'msg_fmt' property. That msg_fmt will get printf'd
a 'message' property. That message will get printf'd
with the keyword arguments provided to the constructor.
"""
message = _("An unknown exception occurred.")
code = 500
def __init__(self, **kwargs):
def __init__(self, message=None, **kwargs):
self.kwargs = kwargs
if CONF.fatal_exception_format_errors:
assert isinstance(self.msg_fmt, six.text_type)
if 'code' not in self.kwargs:
try:
self.kwargs['code'] = self.code
except AttributeError:
pass
try:
self.message = self.msg_fmt % kwargs
except KeyError:
# kwargs doesn't match a variable in the message
# log the issue and the kwargs
LOG.exception(_('Exception in string format operation'),
extra=dict(
private=dict(
msg=self.msg_fmt,
args=kwargs
)
)
)
if not message:
try:
self.message = self.message % kwargs
if CONF.fatal_exception_format_errors:
raise
except Exception as e:
# kwargs doesn't match a variable in the message
# log the issue and the kwargs
LOG.exception(_LE('Exception in string format operation'))
for name, value in kwargs.iteritems():
LOG.error("%s: %s" % (name, value))
if CONF.fatal_exception_format_errors:
raise e
else:
# at least get the core message out if something happened
message = self.message
super(MagnumException, self).__init__(self.message)
def __str__(self):
if six.PY3:
@ -217,27 +223,33 @@ class MagnumException(Exception):
def __unicode__(self):
return self.message
def format_message(self):
if self.__class__.__name__.endswith('_Remote'):
return self.args[0]
else:
return six.text_type(self)
class ObjectNotFound(MagnumException):
msg_fmt = _("The %(name)s %(id)s could not be found.")
message = _("The %(name)s %(id)s could not be found.")
class ObjectNotUnique(MagnumException):
msg_fmt = _("The %(name)s already exists.")
message = _("The %(name)s already exists.")
class ResourceNotFound(ObjectNotFound):
msg_fmt = _("The %(name)s resource %(id)s could not be found.")
message = _("The %(name)s resource %(id)s could not be found.")
code = 404
class ResourceExists(ObjectNotUnique):
msg_fmt = _("The %(name)s resource already exists.")
message = _("The %(name)s resource already exists.")
code = 409
class AuthorizationFailure(MagnumException):
msg_fmt = _("%(client)s connection failed. %(message)s")
message = _("%(client)s connection failed. %(message)s")
class UnsupportedObjectError(MagnumException):
@ -274,11 +286,6 @@ class Conflict(MagnumException):
code = 409
class Invalid(MagnumException):
message = _("Unacceptable parameters.")
code = 400
class InvalidState(Conflict):
message = _("Invalid resource state.")

View File

@ -0,0 +1,204 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from magnum.common import exception
from magnum.openstack.common._i18n import _
from magnum.tests import base
class TestMagnumException(exception.MagnumException):
message = _("templated %(name)s")
class TestException(base.BaseTestCase):
def setUp(self):
super(TestException, self).setUp()
def raise_(self, ex):
raise ex
def test_message_is_templated(self):
ex = TestMagnumException(name="NAME")
self.assertEqual(ex.message, "templated NAME")
def test_ObjectNotFound(self):
self.assertRaises(exception.ObjectNotFound,
lambda: self.raise_(exception.ObjectNotFound()))
def test_ObjectNotUnique(self):
self.assertRaises(exception.ObjectNotUnique,
lambda: self.raise_(exception.ObjectNotUnique()))
def test_ResourceNotFound(self):
self.assertRaises(exception.ResourceNotFound,
lambda: self.raise_(exception.ResourceNotFound()))
def test_ResourceExists(self):
self.assertRaises(exception.ResourceExists,
lambda: self.raise_(exception.ResourceExists()))
def test_AuthorizationFailure(self):
self.assertRaises(exception.AuthorizationFailure,
lambda: self.raise_(exception.AuthorizationFailure()))
def test_UnsupportedObjectError(self):
self.assertRaises(exception.UnsupportedObjectError,
lambda: self.raise_(exception.UnsupportedObjectError()))
def test_IncompatibleObjectVersion(self):
self.assertRaises(exception.IncompatibleObjectVersion,
lambda: self.raise_(exception.IncompatibleObjectVersion()))
def test_OrphanedObjectError(self):
self.assertRaises(exception.OrphanedObjectError,
lambda: self.raise_(exception.OrphanedObjectError()))
def test_Invalid(self):
self.assertRaises(exception.Invalid,
lambda: self.raise_(exception.Invalid()))
def test_InvalidUUID(self):
self.assertRaises(exception.InvalidUUID,
lambda: self.raise_(exception.InvalidUUID()))
def test_InvalidIdentity(self):
self.assertRaises(exception.InvalidIdentity,
lambda: self.raise_(exception.InvalidIdentity()))
def test_HTTPNotFound(self):
self.assertRaises(exception.HTTPNotFound,
lambda: self.raise_(exception.HTTPNotFound()))
def test_Conflict(self):
self.assertRaises(exception.Conflict,
lambda: self.raise_(exception.Conflict()))
def test_InvalidState(self):
self.assertRaises(exception.InvalidState,
lambda: self.raise_(exception.InvalidState()))
def test_InvalidParameterValue(self):
self.assertRaises(exception.InvalidParameterValue,
lambda: self.raise_(exception.InvalidParameterValue()))
def test_InstanceAssociated(self):
self.assertRaises(exception.InstanceAssociated,
lambda: self.raise_(exception.InstanceAssociated()))
def test_InstanceNotFound(self):
self.assertRaises(exception.InstanceNotFound,
lambda: self.raise_(exception.InstanceNotFound()))
def test_PatchError(self):
self.assertRaises(exception.PatchError,
lambda: self.raise_(exception.PatchError()))
def test_NotAuthorized(self):
self.assertRaises(exception.NotAuthorized,
lambda: self.raise_(exception.NotAuthorized()))
def test_OperationNotPermitted(self):
self.assertRaises(exception.OperationNotPermitted,
lambda: self.raise_(exception.OperationNotPermitted()))
def test_InvalidMAC(self):
self.assertRaises(exception.InvalidMAC,
lambda: self.raise_(exception.InvalidMAC()))
def test_SSHConnectFailed(self):
self.assertRaises(exception.SSHConnectFailed,
lambda: self.raise_(exception.SSHConnectFailed()))
def test_FileSystemNotSupported(self):
self.assertRaises(exception.FileSystemNotSupported,
lambda: self.raise_(exception.FileSystemNotSupported()))
def test_BayNotFound(self):
self.assertRaises(exception.BayNotFound,
lambda: self.raise_(exception.BayNotFound()))
def test_BayAssociated(self):
self.assertRaises(exception.BayAssociated,
lambda: self.raise_(exception.BayAssociated()))
def test_BayAlreadyExists(self):
self.assertRaises(exception.BayAlreadyExists,
lambda: self.raise_(exception.BayAlreadyExists()))
def test_BayLocked(self):
self.assertRaises(exception.BayLocked,
lambda: self.raise_(exception.BayLocked()))
def test_BayNotLocked(self):
self.assertRaises(exception.BayNotLocked,
lambda: self.raise_(exception.BayNotLocked()))
def test_ContainerNotFound(self):
self.assertRaises(exception.ContainerNotFound,
lambda: self.raise_(exception.ContainerNotFound()))
def test_ContainerAssociated(self):
self.assertRaises(exception.ContainerAssociated,
lambda: self.raise_(exception.ContainerAssociated()))
def test_ContainerAlreadyExists(self):
self.assertRaises(exception.ContainerAlreadyExists,
lambda: self.raise_(exception.ContainerAlreadyExists()))
def test_ContainerLocked(self):
self.assertRaises(exception.ContainerLocked,
lambda: self.raise_(exception.ContainerLocked()))
def test_ContainerNotLocked(self):
self.assertRaises(exception.ContainerNotLocked,
lambda: self.raise_(exception.ContainerNotLocked()))
def test_PodNotFound(self):
self.assertRaises(exception.PodNotFound,
lambda: self.raise_(exception.PodNotFound()))
def test_PodAssociated(self):
self.assertRaises(exception.PodAssociated,
lambda: self.raise_(exception.PodAssociated()))
def test_PodAlreadyExists(self):
self.assertRaises(exception.PodAlreadyExists,
lambda: self.raise_(exception.PodAlreadyExists()))
def test_PodLocked(self):
self.assertRaises(exception.PodLocked,
lambda: self.raise_(exception.PodLocked()))
def test_PodNotLocked(self):
self.assertRaises(exception.PodNotLocked,
lambda: self.raise_(exception.PodNotLocked()))
def test_ServiceNotFound(self):
self.assertRaises(exception.ServiceNotFound,
lambda: self.raise_(exception.ServiceNotFound()))
def test_ServiceAssociated(self):
self.assertRaises(exception.ServiceAssociated,
lambda: self.raise_(exception.ServiceAssociated()))
def test_ServiceAlreadyExists(self):
self.assertRaises(exception.ServiceAlreadyExists,
lambda: self.raise_(exception.ServiceAlreadyExists()))
def test_ServiceLocked(self):
self.assertRaises(exception.ServiceLocked,
lambda: self.raise_(exception.ServiceLocked()))
def test_ServiceNotLocked(self):
self.assertRaises(exception.ServiceNotLocked,
lambda: self.raise_(exception.ServiceNotLocked()))