handlers: Add fail_on_exception handler.
[matthijs/projects/backupninja.git] / src / lib / backupninja / handlers / __init__.py
index f6559f9b692294b401244f7dc65eb47c203a8779..29dc75631823ed79d5ecda18b99698d6dcacb782 100644 (file)
@@ -24,6 +24,20 @@ import logging as log
 
 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.
@@ -38,6 +52,9 @@ class Action(object):
         # See backupninja.config.load_config for the structure of this
         # value.
         self.default_config = {}
+        # Assume we'll run succesfully. If anything fails in the
+        # meanwhile, set this to True.
+        self.failed = False
 
     def run(self, **kwargs):
         """