Skip to content

Instantly share code, notes, and snippets.

@sandyarmstrong
Created March 29, 2011 22:58
Show Gist options
  • Save sandyarmstrong/893527 to your computer and use it in GitHub Desktop.
Save sandyarmstrong/893527 to your computer and use it in GitHub Desktop.
Custom python exception for printing multiple exceptions
class MultiErrorError(Exception):
'''Gather multiple errors and include all of their info in one exception'''
def __init__(self, msg, errors):
'''Create a MultiErrorError
Arguments:
msg -- text to show in top-level exception after "N errors raised while "
errors -- list of error tuples from calls to sys.exc_info
'''
self.msg = msg
self.errors = errors
def __str__(self):
base_msg = "%s errors raised while %s" % (len(self.errors), self.msg)
from traceback import format_exception
traces = map(lambda x: "".join(format_exception(*x)), self.errors)
return base_msg + reduce(lambda x,y: "\n\n%s\n\n%s" % (x,y), traces)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment