File: errors.py

package info (click to toggle)
mkdocs-macros-plugin 1.3.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 316 kB
  • sloc: python: 1,199; makefile: 4
file content (47 lines) | stat: -rw-r--r-- 1,073 bytes parent folder | download
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()