Add possibility to create Failure from exception
In some cases it's great to have possibility to create misc.Failure object from an exception directly. This commit adds the 'from_exception' class method to create misc.Failure in such way. Change-Id: I44ae6720196b7bbe1e4ad538018487f0761a1af5
This commit is contained in:
@@ -99,6 +99,13 @@ class ReCreatedFailureTestCase(test.TestCase, GeneralFailureObjTestsMixin):
|
|||||||
self.assertIs(exc.check(RuntimeError), RuntimeError)
|
self.assertIs(exc.check(RuntimeError), RuntimeError)
|
||||||
|
|
||||||
|
|
||||||
|
class FromExceptionTestCase(test.TestCase, GeneralFailureObjTestsMixin):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
super(FromExceptionTestCase, self).setUp()
|
||||||
|
self.fail_obj = misc.Failure.from_exception(RuntimeError('Woot!'))
|
||||||
|
|
||||||
|
|
||||||
class FailureObjectTestCase(test.TestCase):
|
class FailureObjectTestCase(test.TestCase):
|
||||||
|
|
||||||
def test_dont_catch_base_exception(self):
|
def test_dont_catch_base_exception(self):
|
||||||
|
|||||||
@@ -416,6 +416,10 @@ class Failure(object):
|
|||||||
'Failure.__init__ got unexpected keyword argument(s): %s'
|
'Failure.__init__ got unexpected keyword argument(s): %s'
|
||||||
% ', '.join(six.iterkeys(kwargs)))
|
% ', '.join(six.iterkeys(kwargs)))
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_exception(cls, exception):
|
||||||
|
return cls((type(exception), exception, None))
|
||||||
|
|
||||||
def _matches(self, other):
|
def _matches(self, other):
|
||||||
if self is other:
|
if self is other:
|
||||||
return True
|
return True
|
||||||
|
|||||||
Reference in New Issue
Block a user