From 9b25de3a3c7fc913b9487e2179b7e730ddfee831 Mon Sep 17 00:00:00 2001 From: Alexander Goscinski Date: Sat, 13 Jul 2024 15:26:18 +0200 Subject: [PATCH] Fix thrown exceptions is within functions in WCI When an exception is thrown within a WCI, the last frame is used as context to add the input lines. This does not work if the exception is thrown within a function within WCI. Therefore we now iterate through all traceback frames to find the one corresponding to WCI. --- src/widget_code_input/utils.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/widget_code_input/utils.py b/src/widget_code_input/utils.py index 1189de7..97174ee 100644 --- a/src/widget_code_input/utils.py +++ b/src/widget_code_input/utils.py @@ -104,13 +104,27 @@ def format_generic_error_msg(exc, code_widget): It will require also the code_widget instance, to get the actual source code. - :note: this must be called from withou the exception, as it will get the current traceback state. + :note: this must be called from without the exception, as it will get the current traceback state. :param exc: The exception that is being processed. :param code_widget: the instance of the code widget with the code that raised the exception. """ error_class, _, tb = sys.exc_info() - line_number = traceback.extract_tb(tb)[-1][1] + frame_summaries = traceback.extract_tb(tb) + # The correct frame summary corresponding to wci not allways at the end + # therefore we loop through all of them + wci_frame_summary = None + for frame_summary in frame_summaries: + if frame_summary.filename == "widget_code_input": + wci_frame_summary = frame_summary + if wci_frame_summary is None: + warning.warn( + "Could not find traceback frame corresponding to " + "widget_code_input, we output whole error message." + ) + + return exc + line_number = wci_frame_summary[1] code_lines = code_widget.full_function_code.splitlines() err_msg = f"{error_class.__name__} in code input: {str(exc)}\n"