Revision 6956e9cd lib/errors.py

b/lib/errors.py
311 311
            issubclass(item, GenericError)):
312 312
      item = None
313 313
  return item
314

  
315

  
316
def EncodeException(err):
317
  """Encodes an exception into a format that L{MaybeRaise} will recognise.
318

  
319
  The passed L{err} argument will be formatted as a tuple (exception
320
  name, arguments) that the MaybeRaise function will recognise.
321

  
322
  @type err: GenericError child
323
  @param err: usually a child of GenericError (but any exception
324
      will be accepted)
325
  @rtype: tuple
326
  @return: tuple of (exception name, exception arguments)
327

  
328
  """
329
  return (err.__class__.__name__, err.args)
330

  
331

  
332
def MaybeRaise(result):
333
  """Is this looks like an encoded Ganeti exception, raise it.
334

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

  
338
  """
339
  tlt = (tuple, list)
340
  if (isinstance(result, tlt) and len(result) == 2 and
341
      isinstance(result[1], tlt)):
342
    # custom ganeti errors
343
    err_class = GetErrorClass(result[0])
344
    if err_class is not None:
345
      raise err_class, tuple(result[1])

Also available in: Unified diff