Created
March 29, 2011 22:58
-
-
Save sandyarmstrong/893527 to your computer and use it in GitHub Desktop.
Custom python exception for printing multiple exceptions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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