From ecab60a5e0310db49c3a1b8d158694215e2f6f19 Mon Sep 17 00:00:00 2001 From: YektaY Date: Fri, 25 Oct 2024 14:35:03 -0700 Subject: [PATCH] added traceback of exception or full call stack if exception isn't active --- src/badger/errors.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/badger/errors.py b/src/badger/errors.py index 1abe8fe2..2ec14d56 100644 --- a/src/badger/errors.py +++ b/src/badger/errors.py @@ -1,7 +1,12 @@ from PyQt5.QtWidgets import QMessageBox +import traceback +import sys class BadgerError(Exception): - def __init__(self, message="", detailed_text=""): + def __init__(self, message="", detailed_text=None): + if detailed_text is None: + detailed_text = self.capture_traceback_or_stack() + super().__init__(message) self.detailed_text = detailed_text self.show_message_box() @@ -17,6 +22,17 @@ def show_message_box(self): dialog.setIcon(QMessageBox.Critical) dialog.exec_() + def capture_traceback_or_stack(self): + """ + Captures the current traceback if an exception is active, otherwise captures the call stack. + """ + exc_type, exc_value, exc_traceback = sys.exc_info() + if exc_traceback: + return ''.join(traceback.format_exception(exc_type, exc_value, exc_traceback)) + else: + return ''.join(traceback.format_stack()) + + class BadgerConfigError(BadgerError): pass