Commit 84f790e6 authored by Michael Hanselmann's avatar Michael Hanselmann

errors: Function to check whether value is encoded error

Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent e35344b4
...@@ -394,17 +394,33 @@ def EncodeException(err): ...@@ -394,17 +394,33 @@ def EncodeException(err):
return (err.__class__.__name__, err.args) return (err.__class__.__name__, err.args)
def MaybeRaise(result): def GetEncodedError(result):
"""If this looks like an encoded Ganeti exception, raise it. """If this looks like an encoded Ganeti exception, return it.
This function tries to parse the passed argument and if it looks This function tries to parse the passed argument and if it looks
like an encoding done by EncodeException, it will re-raise it. like an encoding done by EncodeException, it will return the class
object and arguments.
""" """
tlt = (tuple, list) tlt = (tuple, list)
if (isinstance(result, tlt) and len(result) == 2 and if (isinstance(result, tlt) and len(result) == 2 and
isinstance(result[1], tlt)): isinstance(result[1], tlt)):
# custom ganeti errors # custom ganeti errors
err_class = GetErrorClass(result[0]) errcls = GetErrorClass(result[0])
if err_class is not None: if errcls:
raise err_class, tuple(result[1]) return (errcls, tuple(result[1]))
return None
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.
"""
error = GetEncodedError(result)
if error:
(errcls, args) = error
raise errcls, args
...@@ -62,6 +62,15 @@ class TestErrors(testutils.GanetiTestCase): ...@@ -62,6 +62,15 @@ class TestErrors(testutils.GanetiTestCase):
self.assertRaises(errors.GenericError, errors.MaybeRaise, self.assertRaises(errors.GenericError, errors.MaybeRaise,
("GenericError", ["Hello"])) ("GenericError", ["Hello"]))
def testGetEncodedError(self):
self.assertEqualValues(errors.GetEncodedError(["GenericError",
("Hello", 123, "World")]),
(errors.GenericError, ("Hello", 123, "World")))
self.assertEqualValues(errors.GetEncodedError(["GenericError", []]),
(errors.GenericError, ()))
self.assertFalse(errors.GetEncodedError(["NoErrorClass",
("Hello", 123, "World")]))
if __name__ == "__main__": if __name__ == "__main__":
testutils.GanetiTestProgram() testutils.GanetiTestProgram()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment