handlers: Add fail_on_exception handler.
authorMatthijs Kooijman <matthijs@stdin.nl>
Sat, 7 Aug 2010 18:48:53 +0000 (20:48 +0200)
committerMatthijs Kooijman <matthijs@stdin.nl>
Sat, 7 Aug 2010 18:48:53 +0000 (20:48 +0200)
This decorator is meant for methods on Action subclasses and records
failure when the decorated method throws an exception.

src/lib/backupninja/handlers/__init__.py

index dbc403e04585e21f167410da25d8da99f938bd06..29dc75631823ed79d5ecda18b99698d6dcacb782 100644 (file)
@@ -24,6 +24,20 @@ import logging as log
 
 from backupninja import config
 
 
 from backupninja import config
 
+def fail_on_exception(f):
+    """
+    This is a decorator meant for methods on the Action class. It
+    catches any exceptions thrown, sets the failed attribute to True and
+    rethrows the exception.
+    """
+    def inner(self, *args, **kwargs):
+        try:
+            f(self, *args, **kwargs)
+        except:
+            self.failed = True
+            raise
+    return inner
+
 class Action(object):
     """
     Subclasses of Action represent handlers for various action types.
 class Action(object):
     """
     Subclasses of Action represent handlers for various action types.