pdnew/exceptions.py

43 lines
1.0 KiB
Python

class ExposedException(Exception):
pass
class CompoundException(ExposedException):
def __init__(self, exceptions=None):
self.exceptions = []
if exceptions is not None:
self.extend(exceptions)
@property
def message(self):
msg = "There were %d errors:" % len(self)
for exception in self:
msg += f"\n{str(exception)}"
return msg
def append(self, exception):
if not issubclass(exception.__class__, ExposedException):
raise TypeError("CompoundException members MUST be derived from ExposedException")
self.exceptions.append(exception)
def extend(self, exceptions):
for exception in exceptions:
if not issubclass(exception.__class__, ExposedException):
raise TypeError("CompoundException members MUST be derived from ExposedException")
self.exceptions.extend(exceptions)
def __len__(self):
return self.exceptions.__len__()
def __iter__(self):
return self.exceptions.__iter__()