]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph-deploy.git/commitdiff
create a catches decorator to handle exceptions
authorAlfredo Deza <alfredo@deza.pe>
Fri, 9 Aug 2013 22:38:48 +0000 (15:38 -0700)
committerAlfredo Deza <alfredo@deza.pe>
Fri, 9 Aug 2013 22:46:07 +0000 (15:46 -0700)
Signed-off-by: Alfredo Deza <alfredo@deza.pe>
ceph_deploy/util/decorators.py

index fee16c93e9475b804bef5ff6600a1fa0b3a1b347..977ccffb44f422ec33f0540a2d0fd51c121b04d0 100644 (file)
@@ -1,3 +1,6 @@
+import logging
+import sys
+from functools import wraps
 
 
 def remote_compile(client, fn):
@@ -19,3 +22,73 @@ def remote_compile(client, fn):
                 raise RemoteException(remote_trace.split('\n'), err)
         return inner
     return client.compile(outer)(client.compile(fn))
+
+
+def catches(catch=None, handler=None, exit=True):
+    """
+    Very simple decorator that tries any of the exception(s) passed in as
+    a single exception class or tuple (containing multiple ones) returning the
+    exception message and optionally handling the problem if it rises with the
+    handler if it is provided.
+
+    So instead of douing something like this::
+
+        def bar():
+            try:
+                some_call()
+                print "Success!"
+            except TypeError, exc:
+                print "Error while handling some call: %s" % exc
+                sys.exit(1)
+
+    You would need to decorate it like this to have the same effect::
+
+        @catches(TypeError)
+        def bar():
+            some_call()
+            print "Success!"
+
+    If multiple exceptions need to be caught they need to be provided as a
+    tuple::
+
+        @catches((TypeError, AttributeError))
+        def bar():
+            some_call()
+            print "Success!"
+    """
+    catch = catch or Exception
+    logger = logging.getLogger('ceph_deploy')
+
+    def decorate(f):
+
+        @wraps(f)
+        def newfunc(*a, **kw):
+            try:
+                return f(*a, **kw)
+            except catch as e:
+                if handler:
+                    return handler(e)
+                else:
+                    logger.error(make_exception_message(e))
+                    if exit:
+                        sys.exit(1)
+        return newfunc
+
+    return decorate
+
+#
+# Decorator helpers
+#
+
+
+def make_exception_message(exc):
+    """
+    An exception is passed in and this function
+    returns the proper string depending on the result
+    so it is readable enough.
+    """
+    if str(exc):
+        return '%s: %s\n' % (exc.__class__.__name__, exc)
+    else:
+        return '%s\n' % (exc.__class__.__name__)
+