1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
import textwrap
import traceback
from functools import singledispatch
from jinja2 import TemplateSyntaxError
from mkdocs.structure.pages import Page
@singledispatch
def format_error(error: Exception, markdown: str, page: Page) -> str:
"""Default error message for a generic exception."""
error_type = type(error).__name__
return textwrap.dedent(
f'''
# _Macro Rendering Error_
_File_: `{page.file.src_path}`
_{error_type}_: {error}
```
%s
```
''',
).strip() % traceback.format_exc()
@format_error.register
def _format_template_syntax_error(
error: TemplateSyntaxError,
markdown: str,
page: Page,
) -> str:
"""Template rendering failed."""
line = markdown.splitlines()[error.lineno - 1]
return textwrap.dedent(
f'''
# _Macro Syntax Error_
_File_: `{page.file.src_path}`
_Line {error.lineno} in Markdown file:_ **{error.message}**
```markdown
{line}
```
'''
).strip()
|