Merge "Update ExpectedException handling"

This commit is contained in:
Jenkins 2014-02-11 15:52:25 +00:00 committed by Gerrit Code Review
commit 70dbe6a7cc
3 changed files with 27 additions and 7 deletions

View File

@ -38,12 +38,16 @@ class ExecutorBase(object):
_LOG.debug('Expected exception during message handling (%s)' % _LOG.debug('Expected exception during message handling (%s)' %
e.exc_info[1]) e.exc_info[1])
incoming.reply(failure=e.exc_info, log_failure=False) incoming.reply(failure=e.exc_info, log_failure=False)
except Exception: except Exception as e:
# sys.exc_info() is deleted by LOG.exception(). # sys.exc_info() is deleted by LOG.exception().
exc_info = sys.exc_info() exc_info = sys.exc_info()
_LOG.error('Exception during message handling', _LOG.error('Exception during message handling: %s', e,
exc_info=exc_info) exc_info=exc_info)
incoming.reply(failure=exc_info) incoming.reply(failure=exc_info)
# NOTE(dhellmann): Remove circular object reference
# between the current stack frame and the traceback in
# exc_info.
del exc_info
@abc.abstractmethod @abc.abstractmethod
def start(self): def start(self):

View File

@ -151,10 +151,13 @@ def expected_exceptions(*exceptions):
def inner(*args, **kwargs): def inner(*args, **kwargs):
try: try:
return func(*args, **kwargs) return func(*args, **kwargs)
except Exception as e: # Take advantage of the fact that we can catch
if type(e) in exceptions: # multiple exception types using a tuple of
raise ExpectedException() # exception classes, with subclass detection
else: # for free. Any exception that is not in or
raise # derived from the args passed to us will be
# ignored and thrown as normal.
except exceptions:
raise ExpectedException()
return inner return inner
return outer return outer

View File

@ -43,6 +43,19 @@ class TestExpectedExceptions(test_utils.BaseTestCase):
self.assertRaises(messaging.ExpectedException, naughty) self.assertRaises(messaging.ExpectedException, naughty)
def test_decorator_expected_subclass(self):
class FooException(Exception):
pass
class BarException(FooException):
pass
@messaging.expected_exceptions(FooException)
def naughty():
raise BarException()
self.assertRaises(messaging.ExpectedException, naughty)
def test_decorator_unexpected(self): def test_decorator_unexpected(self):
class FooException(Exception): class FooException(Exception):
pass pass