handlers: Make create_handler throw exceptions on errors.
authorMatthijs Kooijman <matthijs@stdin.nl>
Thu, 10 Jun 2010 06:28:26 +0000 (08:28 +0200)
committerMatthijs Kooijman <matthijs@stdin.nl>
Thu, 10 Jun 2010 06:28:26 +0000 (08:28 +0200)
src/lib/backupninja/handlers/__init__.py

index da07b6d495146a615e29ca4c8a22b807d8de39bb..990c4717a860d1c48d5c02c4716f37d5fca17b5c 100644 (file)
@@ -45,9 +45,7 @@ def create_handler(ty, conf):
     Create a new (subclass of) Handler object for an action with the
     given type. conf is the configuration to pass to the handler.
 
     Create a new (subclass of) Handler object for an action with the
     given type. conf is the configuration to pass to the handler.
 
-    If the handler cannot be loaded, it is logged and None is returned
-    (but any exceptions raised by the handler code itself are not
-    handled).
+    If the handler cannot be loaded, an exception is thrown.
     """
     modname = 'backupninja.handlers.%s' % ty
     # Load the handler if it is not loaded yet
     """
     modname = 'backupninja.handlers.%s' % ty
     # Load the handler if it is not loaded yet
@@ -55,27 +53,25 @@ def create_handler(ty, conf):
         try:
             __import__(modname, globals(), locals(), [])
         except ImportError, e:
         try:
             __import__(modname, globals(), locals(), [])
         except ImportError, e:
-            log.error('Cannot load action handler for "%s": %s'
-                     , ty, e)
-            return None
+            # Add some extra info, since the default exception does not
+            # show the full module name.
+            raise ImportError('Cannot load module %s: %s' % (modname, e))
     # Get the module from the module table
     module = sys.modules[modname]
 
     # Check that the module has a "handler" top level function, which
     # should create a new Handler object.
     if not hasattr(module, 'handler'):
     # Get the module from the module table
     module = sys.modules[modname]
 
     # Check that the module has a "handler" top level function, which
     # should create a new Handler object.
     if not hasattr(module, 'handler'):
-        log.error('Action handler for "%s" (in "%s) is not valid: it '
-                  'does not have a "handler" top level function.'
-                 , ty, module.__file__)
-        return None
+        raise ImportError('%s is not valid: it '
+                          'does not have a "handler" top level function.' 
+                          % (module.__file__))
 
     # Call the "handler" function to create the actual handler
     handler = module.handler(conf)
    
     # Check if the handler returned is really a subclass of Handler
     if not isinstance(handler, Handler):
 
     # Call the "handler" function to create the actual handler
     handler = module.handler(conf)
    
     # Check if the handler returned is really a subclass of Handler
     if not isinstance(handler, Handler):
-        log.error('Action handler for "%s" (in "%s) is not valid: it '
-                  'does not return a subclass of backupninja.handlers.Handler.'
-                 , ty, module.__file__)
-        return None
+        raise TypeError('%s is not valid, %s.handler did not return a '
+                        'subclass of backupninja.handlers.Handler.'
+                        % (module.__file__, modname))
     return handler
     return handler