def __init__(self, message=None, **kwargs):
self.kwargs = kwargs
+ self.kwargs['message'] = message
if 'code' not in self.kwargs:
try:
if isinstance(v, Exception):
self.kwargs[k] = six.text_type(v)
- if not message:
+ if self._should_format():
try:
message = self.message % kwargs
self.msg = message
super(CinderException, self).__init__(message)
+ def _should_format(self):
+ return self.kwargs['message'] is None or '%(message)' in self.message
+
def __unicode__(self):
return unicode(self.msg)
exc1 = Exception(msg)
exc2 = exception.CinderException(kwarg1=exc1)
self.assertEqual(msg, exc2.kwargs['kwarg1'])
+
+ def test_message_in_format_string(self):
+ class FakeCinderException(exception.CinderException):
+ message = 'FakeCinderException: %(message)s'
+
+ exc = FakeCinderException(message='message')
+ self.assertEqual(unicode(exc), 'FakeCinderException: message')
+
+ def test_message_and_kwarg_in_format_string(self):
+ class FakeCinderException(exception.CinderException):
+ message = 'Error %(code)d: %(message)s'
+
+ exc = FakeCinderException(message='message', code=404)
+ self.assertEqual(unicode(exc), 'Error 404: message')
+
+ def test_message_is_exception_in_format_string(self):
+ class FakeCinderException(exception.CinderException):
+ message = 'Exception: %(message)s'
+
+ msg = 'test message'
+ exc1 = Exception(msg)
+ exc2 = FakeCinderException(message=exc1)
+ self.assertEqual(unicode(exc2), 'Exception: test message')