Brick fix BrickException message formatting
This patch fixes an issue when passing in a formatting key and value for formatted text messages for all Brick exceptions. Added unit tests for brick's exception Change-Id: I76dcfc6872ca20305d1844162dd0ae28c4bb565c Related-Bug: 1238085
This commit is contained in:
parent
a5c4cf84c0
commit
f134e3becb
@ -14,8 +14,6 @@
|
||||
|
||||
"""Exceptions for the Brick library."""
|
||||
|
||||
import sys
|
||||
|
||||
from cinder.openstack.common.gettextutils import _
|
||||
from cinder.openstack.common import log as logging
|
||||
|
||||
@ -30,7 +28,7 @@ class BrickException(Exception):
|
||||
a 'msg_fmt' property. That msg_fmt will get printf'd
|
||||
with the keyword arguments provided to the constructor.
|
||||
"""
|
||||
msg_fmt = _("An unknown exception occurred.")
|
||||
message = _("An unknown exception occurred.")
|
||||
code = 500
|
||||
headers = {}
|
||||
safe = False
|
||||
@ -46,23 +44,28 @@ class BrickException(Exception):
|
||||
|
||||
if not message:
|
||||
try:
|
||||
message = self.msg_fmt % kwargs
|
||||
message = self.message % kwargs
|
||||
|
||||
except Exception:
|
||||
exc_info = sys.exc_info()
|
||||
# kwargs doesn't match a variable in the message
|
||||
# log the issue and the kwargs
|
||||
msg = (_("Exception in string format operation. msg='%s'")
|
||||
% self.msg_fmt)
|
||||
% self.message)
|
||||
LOG.exception(msg)
|
||||
for name, value in kwargs.iteritems():
|
||||
LOG.error("%s: %s" % (name, value))
|
||||
|
||||
# at least get the core message out if something happened
|
||||
message = self.msg_fmt
|
||||
message = self.message
|
||||
|
||||
# Put the message in 'msg' so that we can access it. If we have it in
|
||||
# message it will be overshadowed by the class' message attribute
|
||||
self.msg = message
|
||||
super(BrickException, self).__init__(message)
|
||||
|
||||
def __unicode__(self):
|
||||
return unicode(self.msg)
|
||||
|
||||
|
||||
class NotFound(BrickException):
|
||||
message = _("Resource could not be found.")
|
||||
|
62
cinder/tests/brick/test_brick_exception.py
Normal file
62
cinder/tests/brick/test_brick_exception.py
Normal file
@ -0,0 +1,62 @@
|
||||
|
||||
# Copyright 2010 United States Government as represented by the
|
||||
# Administrator of the National Aeronautics and Space Administration.
|
||||
# All Rights Reserved.
|
||||
#
|
||||
# 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 cinder.brick import exception
|
||||
from cinder import test
|
||||
|
||||
|
||||
class BrickExceptionTestCase(test.TestCase):
|
||||
def test_default_error_msg(self):
|
||||
class FakeBrickException(exception.BrickException):
|
||||
message = "default message"
|
||||
|
||||
exc = FakeBrickException()
|
||||
self.assertEqual(unicode(exc), 'default message')
|
||||
|
||||
def test_error_msg(self):
|
||||
self.assertEqual(unicode(exception.BrickException('test')), 'test')
|
||||
|
||||
def test_default_error_msg_with_kwargs(self):
|
||||
class FakeBrickException(exception.BrickException):
|
||||
message = "default message: %(code)s"
|
||||
|
||||
exc = FakeBrickException(code=500)
|
||||
self.assertEqual(unicode(exc), 'default message: 500')
|
||||
|
||||
def test_error_msg_exception_with_kwargs(self):
|
||||
# NOTE(dprince): disable format errors for this test
|
||||
self.flags(fatal_exception_format_errors=False)
|
||||
|
||||
class FakeBrickException(exception.BrickException):
|
||||
message = "default message: %(mispelled_code)s"
|
||||
|
||||
exc = FakeBrickException(code=500)
|
||||
self.assertEqual(unicode(exc), 'default message: %(mispelled_code)s')
|
||||
|
||||
def test_default_error_code(self):
|
||||
class FakeBrickException(exception.BrickException):
|
||||
code = 404
|
||||
|
||||
exc = FakeBrickException()
|
||||
self.assertEqual(exc.kwargs['code'], 404)
|
||||
|
||||
def test_error_code_from_kwarg(self):
|
||||
class FakeBrickException(exception.BrickException):
|
||||
code = 500
|
||||
|
||||
exc = FakeBrickException(code=404)
|
||||
self.assertEqual(exc.kwargs['code'], 404)
|
Loading…
Reference in New Issue
Block a user