|
7 | 7 | import shutil
|
8 | 8 | import traceback
|
9 | 9 |
|
| 10 | +from traceback import format_exception |
| 11 | +from typing import List |
| 12 | + |
10 | 13 | import lark
|
11 | 14 |
|
12 | 15 | import yc_yaml
|
13 | 16 |
|
| 17 | +from pavilion.micro import flatten |
| 18 | + |
14 | 19 |
|
15 | 20 | class PavilionError(RuntimeError):
|
16 | 21 | """Base class for all Pavilion errors."""
|
@@ -47,12 +52,31 @@ def __str__(self):
|
47 | 52 | else:
|
48 | 53 | return self.msg
|
49 | 54 |
|
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) |
52 | 77 |
|
53 | 78 | lines = []
|
54 | 79 | next_exc = self.prior_error
|
55 |
| - width = shutil.get_terminal_size((80, 80)).columns |
56 | 80 | tab_level = 0
|
57 | 81 | for line in str(self.msg).split('\n'):
|
58 | 82 | lines.extend(textwrap.wrap(line, width=width))
|
|
0 commit comments