From 0f8409879c0d3e3cf937af4b21cf4b597ebdf5ff Mon Sep 17 00:00:00 2001 From: Keith Edmunds Date: Sat, 22 Mar 2025 09:27:55 +0000 Subject: [PATCH] Report correct line for ApplicationError --- app/log.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/app/log.py b/app/log.py index 178e31f..6c718de 100644 --- a/app/log.py +++ b/app/log.py @@ -79,9 +79,22 @@ log = logging.getLogger(Config.LOG_NAME) def handle_exception(exc_type, exc_value, exc_traceback): - error = str(exc_value) + """ + Inform user of exception + """ + + # Navigate to the inner stack frame + tb = exc_traceback + while tb.tb_next: + tb = tb.tb_next + + fname = os.path.basename(tb.tb_frame.f_code.co_filename) + lineno = tb.tb_lineno + msg = f"ApplicationError: {exc_value}\nat {fname}:{lineno}" + logmsg = f"ApplicationError: {exc_value} at {fname}:{lineno}" + if issubclass(exc_type, ApplicationError): - log.error(error) + log.error(logmsg) else: # Handle unexpected errors (log and display) error_msg = "".join(traceback.format_exception(exc_type, exc_value, exc_traceback)) @@ -104,7 +117,6 @@ def handle_exception(exc_type, exc_value, exc_traceback): ) if QApplication.instance() is not None: fname = os.path.split(exc_traceback.tb_frame.f_code.co_filename)[1] - msg = f"ApplicationError: {error}\nat {fname}:{exc_traceback.tb_lineno}" QMessageBox.critical(None, "Application Error", msg)