From 6de4ea2743ca86f044b585c5c4ddf68704102868 Mon Sep 17 00:00:00 2001 From: Greenscreener Date: Thu, 15 Feb 2024 18:19:10 +0100 Subject: [PATCH] Error handling now contains filename. --- src/formatitko/nop_processor.py | 11 +++++++++-- src/formatitko/output_generator.py | 8 +++++--- src/formatitko/transform_processor.py | 7 +------ 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/formatitko/nop_processor.py b/src/formatitko/nop_processor.py index 5a89228..4cffa90 100644 --- a/src/formatitko/nop_processor.py +++ b/src/formatitko/nop_processor.py @@ -7,16 +7,20 @@ from typing import Union, Callable from .whitespace import NBSP from .elements import FQuoted -from .context import Group, InlineGroup, BlockGroup +from .context import Group, InlineGroup, BlockGroup, Context from .whitespace import Whitespace from .command import BlockCommand, InlineCommand, CodeCommand, Command from .output_generator import FormatitkoRecursiveError ELCl = Union[Element, ListContainer, list[Union[Element, ListContainer]]] +class DoubleDocError(Exception): + "TransformProcessor should only ever see a single Doc." + pass class NOPProcessor: TYPE_DICT: dict[type, Callable] + context: Union[Context, None] = None class UnknownElementError(Exception): f"An unknown Element has been passed to the NOPProcessor, probably because panflute introduced a new one." @@ -120,7 +124,7 @@ class NOPProcessor: err.add_element(e) raise err except Exception as err: - raise FormatitkoRecursiveError(e) from err + raise FormatitkoRecursiveError(e, self.context.filename) from err def transform_list(self, e: list[Union[Element, ListContainer]]) -> list[Union[Element, ListContainer]]: for i in range(len(e)): @@ -301,6 +305,9 @@ class NOPProcessor: return e def transform_Doc(self, e: Doc) -> Doc: + if self.context is not None: + raise DoubleDocError() + self.context = Context(e, self.root_file_path) e.content = self.transform(e.content) return e diff --git a/src/formatitko/output_generator.py b/src/formatitko/output_generator.py index 9869b87..46431f7 100644 --- a/src/formatitko/output_generator.py +++ b/src/formatitko/output_generator.py @@ -21,9 +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 - def __init__(self, e: Union[Element, ListContainer, list[Union[Element, ListContainer]]], *args): + def __init__(self, e: Union[Element, ListContainer, list[Union[Element, ListContainer]]], file: str, *args): self.elements = [e] + self.file = file super().__init__(args) def add_element(self, e: Union[Element, ListContainer, list[Union[Element, ListContainer]]]): @@ -32,7 +34,7 @@ class FormatitkoRecursiveError(Exception): def pretty_print(self): def eprint(*args, **kwargs): print(*args, file=sys.stderr, **kwargs) - eprint("Error occured in ", end="") + eprint(f"Error occured in file {self.file} in ", end="") 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() @@ -159,7 +161,7 @@ class OutputGenerator: err.add_element(e) raise err except Exception as err: - raise FormatitkoRecursiveError(e) from err + raise FormatitkoRecursiveError(e, self.context.filename) from err def escape_special_chars(self, text: str) -> str: return text diff --git a/src/formatitko/transform_processor.py b/src/formatitko/transform_processor.py index 32a0306..60cba5a 100644 --- a/src/formatitko/transform_processor.py +++ b/src/formatitko/transform_processor.py @@ -20,15 +20,10 @@ from .context import Context, CommandCallable from .whitespace import Whitespace, bavlna from .command import BlockCommand, InlineCommand, CodeCommand, Command from .command_util import handle_command_define, parse_command -from .nop_processor import NOPProcessor, ELCl - -class DoubleDocError(Exception): - "TransformProcessor should only ever see a single Doc." - pass +from .nop_processor import NOPProcessor, ELCl, DoubleDocError class TransformProcessor(NOPProcessor): - context: Union[Context, None] = None root_file_path: str root_highlight_style: str = "default" _command_modules: list[tuple[Union[dict[str, CommandCallable], ModuleType], str]] = []