Browse Source

Improved error messages

error-handling
Jan Černohorský 9 months ago
parent
commit
50b29b1ae3
  1. 2
      src/formatitko/nop_processor.py
  2. 26
      src/formatitko/output_generator.py
  3. 9
      test/test-files/test-partial.md

2
src/formatitko/nop_processor.py

@ -124,7 +124,7 @@ class NOPProcessor:
err.add_element(e) err.add_element(e)
raise err raise err
except Exception as 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]]: def transform_list(self, e: list[Union[Element, ListContainer]]) -> list[Union[Element, ListContainer]]:
for i in range(len(e)): for i in range(len(e)):

26
src/formatitko/output_generator.py

@ -21,11 +21,11 @@ class UnknownElementError(Exception):
class FormatitkoRecursiveError(Exception): class FormatitkoRecursiveError(Exception):
"A generic exception which wraps other exceptions and adds element-based traceback" "A generic exception which wraps other exceptions and adds element-based traceback"
elements: list[Union[Element, ListContainer, list[Union[Element, ListContainer]]]] 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.elements = [e]
self.file = file self.context = context
super().__init__(args) super().__init__(args)
def add_element(self, e: Union[Element, ListContainer, list[Union[Element, ListContainer]]]): def add_element(self, e: Union[Element, ListContainer, list[Union[Element, ListContainer]]]):
@ -34,15 +34,21 @@ class FormatitkoRecursiveError(Exception):
def pretty_print(self): def pretty_print(self):
def eprint(*args, **kwargs): def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **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): for i in range(len(self.elements)-1, 0, -1):
if hasattr(self.elements[i], "content") and isinstance(self.elements[i].content[0], Inline): 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()
eprint('on line: "' + stringify(self.elements[i]) + '"', end="") eprint('on line: "' + stringify(line).strip() + '"', end="")
break
eprint(type(self.elements[i]).__name__ + "[" + str(self.elements[i-1].index) + "]", end=": ")
eprint() eprint()
eprint("in element: " + str(self.elements[0])) eprint("in element: " + str(self.elements[0]).replace("\n", "\\n"))
sys.tracebacklimit = 0 sys.tracebacklimit = 0
raise self.__cause__ from None raise self.__cause__ from None
@ -161,7 +167,7 @@ class OutputGenerator:
err.add_element(e) err.add_element(e)
raise err raise err
except Exception as 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: def escape_special_chars(self, text: str) -> str:
return text return text

9
test/test-files/test-partial.md

@ -56,6 +56,15 @@ $$
$$ $$
<!--There is an inline *emphasis with $math \error$*.-->
<!--
```python {.run}
print("bruh")
raise Exception("Jsem piča")
```
-->
![This is a figure, go figure...](logo.svg){width=25%}What ![This is a figure, go figure...](logo.svg){width=25%}What
![This is a figure, go figure...](logo.pdf){width=50%} ![This is a figure, go figure...](logo.pdf){width=50%}

Loading…
Cancel
Save