Move the luxi error handling into errors.py
authorIustin Pop <iustin@google.com>
Thu, 27 Aug 2009 13:01:23 +0000 (15:01 +0200)
committerIustin Pop <iustin@google.com>
Thu, 27 Aug 2009 15:21:05 +0000 (17:21 +0200)
Currently the luxi error handling is hardcoded as special encoding on
the masterd-side and special decoding on the client side. This patch
moves it to errors.py such that other parts of the code can reuse the
same encoding.

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>

daemons/ganeti-masterd
lib/errors.py
lib/luxi.py

index 5b0c301..4c15a7d 100755 (executable)
@@ -171,7 +171,7 @@ class ClientRqHandler(SocketServer.BaseRequestHandler):
         success = True
       except errors.GenericError, err:
         success = False
-        result = (err.__class__.__name__, err.args)
+        result = errors.EncodeException(err)
       except:
         logging.error("Unexpected exception", exc_info=True)
         err = sys.exc_info()
index 82018c3..1cbf2db 100644 (file)
@@ -311,3 +311,35 @@ def GetErrorClass(name):
             issubclass(item, GenericError)):
       item = None
   return item
+
+
+def EncodeException(err):
+  """Encodes an exception into a format that L{MaybeRaise} will recognise.
+
+  The passed L{err} argument will be formatted as a tuple (exception
+  name, arguments) that the MaybeRaise function will recognise.
+
+  @type err: GenericError child
+  @param err: usually a child of GenericError (but any exception
+      will be accepted)
+  @rtype: tuple
+  @return: tuple of (exception name, exception arguments)
+
+  """
+  return (err.__class__.__name__, err.args)
+
+
+def MaybeRaise(result):
+  """Is this looks like an encoded Ganeti exception, raise it.
+
+  This function tries to parse the passed argument and if it looks
+  like an encoding done by EncodeException, it will re-raise it.
+
+  """
+  tlt = (tuple, list)
+  if (isinstance(result, tlt) and len(result) == 2 and
+      isinstance(result[1], tlt)):
+    # custom ganeti errors
+    err_class = GetErrorClass(result[0])
+    if err_class is not None:
+      raise err_class, tuple(result[1])
index 004eeff..2a3adaa 100644 (file)
@@ -325,14 +325,7 @@ class Client(object):
     result = data[KEY_RESULT]
 
     if not data[KEY_SUCCESS]:
-      # TODO: decide on a standard exception
-      if (isinstance(result, (tuple, list)) and len(result) == 2 and
-          isinstance(result[1], (tuple, list))):
-        # custom ganeti errors
-        err_class = errors.GetErrorClass(result[0])
-        if err_class is not None:
-          raise err_class, tuple(result[1])
-
+      errors.MaybeRaise(result)
       raise RequestError(result)
 
     return result