Implement BuildHooksEnv for NoHooksLU
[ganeti-local] / lib / errors.py
index 6efd9aa..2040cae 100644 (file)
@@ -198,6 +198,10 @@ class UnitParseError(GenericError):
 
   """
 
+class TypeEnforcementError(GenericError):
+  """Unable to enforce data type.
+
+  """
 
 class SshKeyError(GenericError):
   """Invalid SSH key.
@@ -228,7 +232,8 @@ class QuitGanetiException(Exception):
   error should returned to the caller, and the second one will be the returned
   result (either as an error or as a normal result).
 
-  Examples:
+  Examples::
+
     # Return a result of "True" to the caller, but quit ganeti afterwards
     raise QuitGanetiException(False, True)
     # Send an error to the caller, and quit ganeti
@@ -252,6 +257,14 @@ class JobQueueDrainError(JobQueueError):
   """
 
 
+class JobQueueFull(JobQueueError):
+  """Job queue full error.
+
+  Raised when job queue size reached its hard limit.
+
+  """
+
+
 # errors should be added above
 
 
@@ -272,3 +285,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):
+  """If 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])