Skip to content

Commit 13bbfd4

Browse files
committed
Progresss
1 parent 5ae3838 commit 13bbfd4

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

lib/pavilion/errors.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@
77
import shutil
88
import traceback
99

10+
from traceback import format_exception
11+
from typing import List
12+
1013
import lark
1114

1215
import yc_yaml
1316

17+
from pavilion.micro import flatten
18+
1419

1520
class PavilionError(RuntimeError):
1621
"""Base class for all Pavilion errors."""
@@ -47,12 +52,31 @@ def __str__(self):
4752
else:
4853
return self.msg
4954

50-
def pformat(self) -> str:
51-
"""Specially format the exception for printing."""
55+
@staticmethod
56+
def _wrap_lines(lines: List[str], width: int) -> List[str]:
57+
"""Given a list of lines, produce a new list of lines wrapped to the specified width."""
58+
59+
lines = map(lambda x: textwrap.wrap(x, width=width), lines)
60+
61+
return list(flatten(lines))
62+
63+
64+
def pformat(self, traceback: bool = False) -> str:
65+
"""Specially format the exception for printing. If traceback is True, return the full
66+
traceback associated with the error. Otherwise, return a summary of the error."""
67+
68+
width = shutil.get_terminal_size((80, 80)).columns
69+
70+
if traceback:
71+
lines = self._wrap_lines(format_exception(self))
72+
73+
# Remove newlines, for consistency with textwrap.wrap
74+
map(lambda x: x.rstrip("\n"), lines)
75+
76+
return "\n".join(lines)
5277

5378
lines = []
5479
next_exc = self.prior_error
55-
width = shutil.get_terminal_size((80, 80)).columns
5680
tab_level = 0
5781
for line in str(self.msg).split('\n'):
5882
lines.extend(textwrap.wrap(line, width=width))

lib/pavilion/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def main():
5454
if not '--show-tracebacks' in sys.argv:
5555
output.fprint(sys.stderr, "Error getting config, exiting.", err, color=output.RED)
5656
else:
57-
print(traceback.format_exc())
57+
PavilionError(err).pformat(traceback=True)
5858

5959
sys.exit(-1)
6060

@@ -68,7 +68,7 @@ def main():
6868
if not partial_args.show_tracebacks:
6969
output.fprint(sys.stderr, "Error initializing plugins.", err, color=output.RED)
7070
else:
71-
print(traceback.format_exc())
71+
PavilionError(err).pformat(traceback=True)
7272

7373
sys.exit(-1)
7474

0 commit comments

Comments
 (0)