Experimental error handling with snippets of input for OutputGenerator. #54
3 changed files with 15 additions and 11 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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]] = []
|
||||
|
|
Loading…
Reference in a new issue