Revision 84f790e6

b/lib/errors.py
394 394
  return (err.__class__.__name__, err.args)
395 395

  
396 396

  
397
def MaybeRaise(result):
398
  """If this looks like an encoded Ganeti exception, raise it.
397
def GetEncodedError(result):
398
  """If this looks like an encoded Ganeti exception, return it.
399 399

  
400 400
  This function tries to parse the passed argument and if it looks
401
  like an encoding done by EncodeException, it will re-raise it.
401
  like an encoding done by EncodeException, it will return the class
402
  object and arguments.
402 403

  
403 404
  """
404 405
  tlt = (tuple, list)
405 406
  if (isinstance(result, tlt) and len(result) == 2 and
406 407
      isinstance(result[1], tlt)):
407 408
    # custom ganeti errors
408
    err_class = GetErrorClass(result[0])
409
    if err_class is not None:
410
      raise err_class, tuple(result[1])
409
    errcls = GetErrorClass(result[0])
410
    if errcls:
411
      return (errcls, tuple(result[1]))
412

  
413
  return None
414

  
415

  
416
def MaybeRaise(result):
417
  """If this looks like an encoded Ganeti exception, raise it.
418

  
419
  This function tries to parse the passed argument and if it looks
420
  like an encoding done by EncodeException, it will re-raise it.
421

  
422
  """
423
  error = GetEncodedError(result)
424
  if error:
425
    (errcls, args) = error
426
    raise errcls, args
b/test/ganeti.errors_unittest.py
62 62
    self.assertRaises(errors.GenericError, errors.MaybeRaise,
63 63
                      ("GenericError", ["Hello"]))
64 64

  
65
  def testGetEncodedError(self):
66
    self.assertEqualValues(errors.GetEncodedError(["GenericError",
67
                                                   ("Hello", 123, "World")]),
68
                           (errors.GenericError, ("Hello", 123, "World")))
69
    self.assertEqualValues(errors.GetEncodedError(["GenericError", []]),
70
                           (errors.GenericError, ()))
71
    self.assertFalse(errors.GetEncodedError(["NoErrorClass",
72
                                             ("Hello", 123, "World")]))
73

  
65 74

  
66 75
if __name__ == "__main__":
67 76
  testutils.GanetiTestProgram()

Also available in: Unified diff