Skip to content

Commit

Permalink
simplify traceback logging
Browse files Browse the repository at this point in the history
  • Loading branch information
achidlow committed Sep 10, 2024
1 parent ae9ae1b commit 3733f07
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
8 changes: 7 additions & 1 deletion scripts/compile_all_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,13 @@ def main(options: CompileAllOptions) -> None:
print("Compilation failures:")
for name, stdout in sorted(failures, key=operator.itemgetter(0)):
print(f" ~~~ {name} ~~~ ")
print("\n".join(ln for ln in stdout.splitlines() if not ln.startswith("debug: ")))
print(
"\n".join(
ln
for ln in stdout.splitlines()
if (ln.startswith("debug: Traceback ") or not ln.startswith("debug: "))
)
)
print("Updating sizes.txt")
if limit_to:
existing = ProgramSizes.read_file(SIZE_TALLY_PATH)
Expand Down
23 changes: 7 additions & 16 deletions src/puya/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,22 @@ class CodeError(PuyaError):
"""Base class for all exceptions that indicate a fault in the code being compiled."""


def _crash_report() -> None:
# Adapted from report_internal_error in mypy
tb = traceback.extract_stack()[:-4]
# Excise all the traceback from the test runner
for i, x in enumerate(tb):
if x.name == "pytest_runtest_call":
tb = tb[i + 1 :]
break
*_, tb_type = sys.exc_info()
tb2 = traceback.extract_tb(tb_type)[1:]
output = ["Traceback (most recent call last):"]
output.extend(s.rstrip("\n") for s in traceback.format_list(tb + tb2))
logger.debug("\n".join(output))


@contextlib.contextmanager
def log_exceptions(fallback_location: SourceLocation | None = None) -> Iterator[None]:
try:
yield
except CodeError as ex:
logger.error(ex.msg, location=ex.location or fallback_location) # noqa: TRY400
except InternalError as ex:
_log_traceback()
logger.critical(ex.msg, location=ex.location or fallback_location)
sys.exit(ErrorExitCode.internal)
except Exception as ex:
_crash_report()
_log_traceback()
logger.critical(f"{type(ex).__name__}: {ex}", location=fallback_location)
sys.exit(ErrorExitCode.internal)


def _log_traceback() -> None:
traceback_lines = traceback.format_exc()
logger.debug(traceback_lines.rstrip("\n"))

0 comments on commit 3733f07

Please sign in to comment.