Improve reporting of unexpected exceptions

This commit is contained in:
Joscha 2021-05-22 18:36:25 +02:00
parent 54dd2f8337
commit dfde0e2310

View File

@ -113,24 +113,27 @@ class Log:
self.print(f"[red]{escape(text)}") self.print(f"[red]{escape(text)}")
def unexpected_exception(self) -> None: def unexpected_exception(self) -> None:
"""
Call this in an "except" clause to log an unexpected exception.
"""
t, v, tb = sys.exc_info() t, v, tb = sys.exc_info()
if t is None or v is None or tb is None:
self.error("An unexpected exception occurred") # We're not currently handling an exception, so somebody probably
self.error_contd("") # called this function where they shouldn't.
self.error("Something unexpected happened")
for line in traceback.format_tb(tb): self.error_contd("")
self.error_contd(line[:-1]) # Without trailing newline for line in traceback.format_stack():
self.error_contd(line[:-1]) # Without the newline
if str(v): self.error_contd("")
self.error_contd(f"{t.__name__}: {v}")
else: else:
self.error_contd(t.__name__) self.error("An unexpected exception occurred")
self.error_contd("")
self.error_contd(traceback.format_exc())
self.error_contd("")
self.error_contd(""" self.error_contd("""
An unexpected exception occurred. This usually shouldn't happen. Please copy Please copy your program output and send it to the PFERD maintainers, either
your program output and send it to the PFERD maintainers, either directly or as directly or as a GitHub issue: https://github.com/Garmelon/PFERD/issues/new
a GitHub issue: https://github.com/Garmelon/PFERD/issues/new
""".strip()) """.strip())
def explain_topic(self, text: str) -> None: def explain_topic(self, text: str) -> None: