Move the _pformat() method to be a classmethod

To avoid creating nested functions when a class one
will suffice just switch to using a class method for
the nested _pformat() function instead.

This also makes sure that the cause of a exception is
not none before attempting to format it (and its
children) and also passes along a lines list that is
appended/extended instead of each function call creating
its own version which is later merged.

Change-Id: Ie84b004c115e488c87f9829bfcf4d8f66ebb660f
This commit is contained in:
Joshua Harlow
2014-11-21 14:31:08 -08:00
parent a77d192f9c
commit 178f2799c4

View File

@@ -46,21 +46,26 @@ class TaskFlowException(Exception):
"""Pretty formats a taskflow exception + any connected causes.""" """Pretty formats a taskflow exception + any connected causes."""
if indent < 0: if indent < 0:
raise ValueError("indent must be greater than or equal to zero") raise ValueError("indent must be greater than or equal to zero")
return "\n".join(self._pformat(self, [], 0,
indent=indent, indent_text=indent_text))
def _format(excp, indent_by): @classmethod
lines = [] def _pformat(cls, excp, lines, current_indent, indent=2, indent_text=" "):
for line in traceback.format_exception_only(type(excp), excp): line_prefix = indent_text * current_indent
# We'll add our own newlines on at the end of formatting. for line in traceback.format_exception_only(type(excp), excp):
if line.endswith("\n"): # We'll add our own newlines on at the end of formatting.
line = line[0:-1] if line.endswith("\n"):
lines.append((indent_text * indent_by) + line) line = line[0:-1]
try: lines.append(line_prefix + line)
lines.extend(_format(excp.cause, indent_by + indent)) try:
except AttributeError: cause = excp.cause
pass except AttributeError:
return lines pass
else:
return "\n".join(_format(self, 0)) if cause is not None:
cls._pformat(cause, lines, current_indent + indent,
indent=indent, indent_text=indent_text)
return lines
# Errors related to storage or operations on storage units. # Errors related to storage or operations on storage units.