Revision a6607331 lib/errors.py

b/lib/errors.py
285 285
            issubclass(item, GenericError)):
286 286
      item = None
287 287
  return item
288

  
289

  
290
def EncodeException(err):
291
  """Encodes an exception into a format that L{MaybeRaise} will recognise.
292

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

  
296
  @type err: GenericError child
297
  @param err: usually a child of GenericError (but any exception
298
      will be accepted)
299
  @rtype: tuple
300
  @return: tuple of (exception name, exception arguments)
301

  
302
  """
303
  return (err.__class__.__name__, err.args)
304

  
305

  
306
def MaybeRaise(result):
307
  """Is this looks like an encoded Ganeti exception, raise it.
308

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

  
312
  """
313
  tlt = (tuple, list)
314
  if (isinstance(result, tlt) and len(result) == 2 and
315
      isinstance(result[1], tlt)):
316
    # custom ganeti errors
317
    err_class = GetErrorClass(result[0])
318
    if err_class is not None:
319
      raise err_class, tuple(result[1])

Also available in: Unified diff