From 50b29b1ae35ad2f9ebc71b5b0bb618869236c593 Mon Sep 17 00:00:00 2001 From: Greenscreener Date: Sat, 17 Feb 2024 18:07:47 +0100 Subject: [PATCH] Improved error messages --- src/formatitko/nop_processor.py | 2 +- src/formatitko/output_generator.py | 28 +++++++++++++++++----------- test/test-files/test-partial.md | 9 +++++++++ 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/formatitko/nop_processor.py b/src/formatitko/nop_processor.py index 4cffa90..bc20a2a 100644 --- a/src/formatitko/nop_processor.py +++ b/src/formatitko/nop_processor.py @@ -124,7 +124,7 @@ class NOPProcessor: err.add_element(e) raise err except Exception as err: - raise FormatitkoRecursiveError(e, self.context.filename) from err + raise FormatitkoRecursiveError(e, self.context) from err def transform_list(self, e: list[Union[Element, ListContainer]]) -> list[Union[Element, ListContainer]]: for i in range(len(e)): diff --git a/src/formatitko/output_generator.py b/src/formatitko/output_generator.py index 46431f7..4f3cd24 100644 --- a/src/formatitko/output_generator.py +++ b/src/formatitko/output_generator.py @@ -21,11 +21,11 @@ class UnknownElementError(Exception): class FormatitkoRecursiveError(Exception): "A generic exception which wraps other exceptions and adds element-based traceback" elements: list[Union[Element, ListContainer, list[Union[Element, ListContainer]]]] - file: str + context: Context - def __init__(self, e: Union[Element, ListContainer, list[Union[Element, ListContainer]]], file: str, *args): + def __init__(self, e: Union[Element, ListContainer, list[Union[Element, ListContainer]]], context: Context, *args): self.elements = [e] - self.file = file + self.context = context super().__init__(args) def add_element(self, e: Union[Element, ListContainer, list[Union[Element, ListContainer]]]): @@ -34,15 +34,21 @@ class FormatitkoRecursiveError(Exception): def pretty_print(self): def eprint(*args, **kwargs): print(*args, file=sys.stderr, **kwargs) - eprint(f"Error occured in file {self.file} in ", end="") + + def print_filename_recursive(context: Context): + return context.filename +\ + ((" (included from " + print_filename_recursive(context.parent) + ")") if context.parent else "") + eprint(f"Error occured in file {print_filename_recursive(self.context)} in ", end="") + line = None for i in range(len(self.elements)-1, 0, -1): - if hasattr(self.elements[i], "content") and isinstance(self.elements[i].content[0], Inline): - eprint() - eprint('on line: "' + stringify(self.elements[i]) + '"', end="") - break - eprint(type(self.elements[i]).__name__ + "[" + str(self.elements[i-1].index) + "]", end=": ") + if hasattr(self.elements[i], "content") and len(self.elements[i].content) > 0 and isinstance(self.elements[i].content[0], Inline) and line is None: + line = self.elements[i] + eprint(type(self.elements[i]).__name__ + "[" + (str(self.elements[i-1].index) if isinstance(self.elements[i-1].index, int) else "") + "]", end=": ") + if line: + eprint() + eprint('on line: "' + stringify(line).strip() + '"', end="") eprint() - eprint("in element: " + str(self.elements[0])) + eprint("in element: " + str(self.elements[0]).replace("\n", "\\n")) sys.tracebacklimit = 0 raise self.__cause__ from None @@ -161,7 +167,7 @@ class OutputGenerator: err.add_element(e) raise err except Exception as err: - raise FormatitkoRecursiveError(e, self.context.filename) from err + raise FormatitkoRecursiveError(e, self.context) from err def escape_special_chars(self, text: str) -> str: return text diff --git a/test/test-files/test-partial.md b/test/test-files/test-partial.md index f1c9ab5..ee2fd6b 100644 --- a/test/test-files/test-partial.md +++ b/test/test-files/test-partial.md @@ -56,6 +56,15 @@ $$ $$ + + + + ![This is a figure, go figure...](logo.svg){width=25%}What ![This is a figure, go figure...](logo.pdf){width=50%}