|
| 1 | +"""Render :class:`YmprintAuthoringError` instances as compact, friendly output. |
| 2 | +
|
| 3 | +Python-block tracebacks are truncated to their *top and bottom*: the first few |
| 4 | +frames (the calling context — which block triggered the failure) and the last |
| 5 | +few frames (the exact line that blew up), with the middle collapsed. This keeps |
| 6 | +even a deep stack readable while still telling the author both *where their |
| 7 | +document caused it* and *what to fix*. |
| 8 | +""" |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import traceback |
| 12 | +from typing import Optional |
| 13 | + |
| 14 | +from rich.console import Group, RenderableType |
| 15 | +from rich.text import Text |
| 16 | + |
| 17 | +from ..errors import PythonBlockError, YamlSyntaxError, YmprintAuthoringError |
| 18 | + |
| 19 | +# How many stack frames to keep from each end before collapsing the middle. |
| 20 | +HEAD_FRAMES = 2 |
| 21 | +TAIL_FRAMES = 3 |
| 22 | + |
| 23 | + |
| 24 | +def format_authoring_error(exc: YmprintAuthoringError) -> RenderableType: |
| 25 | + """Return a rich renderable describing an authoring error.""" |
| 26 | + if isinstance(exc, YamlSyntaxError): |
| 27 | + return _format_yaml_error(exc) |
| 28 | + if isinstance(exc, PythonBlockError): |
| 29 | + return _format_python_error(exc) |
| 30 | + return Text(str(exc), style="red") |
| 31 | + |
| 32 | + |
| 33 | +def _format_yaml_error(exc: YamlSyntaxError) -> RenderableType: |
| 34 | + location = exc.filepath.name |
| 35 | + if exc.line is not None: |
| 36 | + location += f", line {exc.line}, column {exc.column}" |
| 37 | + |
| 38 | + parts: list[RenderableType] = [ |
| 39 | + Text(f"YAML syntax error in {location}", style="bold red") |
| 40 | + ] |
| 41 | + if exc.problem: |
| 42 | + parts.append(Text(exc.problem, style="red")) |
| 43 | + if exc.snippet: |
| 44 | + parts.append(Text(exc.snippet, style="yellow")) |
| 45 | + parts.append(Text("Fix the YAML above, then save to reload.", style="dim italic")) |
| 46 | + return Group(*parts) |
| 47 | + |
| 48 | + |
| 49 | +def _format_python_error(exc: PythonBlockError) -> RenderableType: |
| 50 | + original = exc.original |
| 51 | + parts: list[RenderableType] = [ |
| 52 | + Text(f"Error in Python block '{exc.block_key}'", style="bold red") |
| 53 | + ] |
| 54 | + |
| 55 | + if isinstance(original, SyntaxError): |
| 56 | + # A SyntaxError fails at compile time, so there is no `<string>` frame in |
| 57 | + # the traceback. Use the exception's own line/offset instead, and detect |
| 58 | + # the common cause: forgetting the `|` block scalar, which folds the code |
| 59 | + # into a single line. |
| 60 | + parts.extend(_syntax_error_parts(exc, original)) |
| 61 | + message = original.msg |
| 62 | + else: |
| 63 | + frames = traceback.extract_tb(original.__traceback__) |
| 64 | + source_lines = exc.source.splitlines() |
| 65 | + failing = _failing_source_line(frames, source_lines) |
| 66 | + if failing is not None: |
| 67 | + parts.append(failing) |
| 68 | + parts.append(Text("Traceback (most relevant frames):", style="dim")) |
| 69 | + parts.extend(_compact_frames(frames, source_lines)) |
| 70 | + message = str(original) |
| 71 | + |
| 72 | + parts.append(Text(f"{type(original).__name__}: {message}", style="bold red")) |
| 73 | + return Group(*parts) |
| 74 | + |
| 75 | + |
| 76 | +def _syntax_error_parts( |
| 77 | + exc: PythonBlockError, original: SyntaxError |
| 78 | +) -> list[RenderableType]: |
| 79 | + parts: list[RenderableType] = [] |
| 80 | + single_line = "\n" not in exc.source.strip() |
| 81 | + text = (original.text or "").rstrip("\n") |
| 82 | + lineno = original.lineno or 1 |
| 83 | + |
| 84 | + if text: |
| 85 | + line = Text() |
| 86 | + line.append(f"→ line {lineno}: ", style="bold yellow") |
| 87 | + line.append(text.strip(), style="yellow") |
| 88 | + parts.append(line) |
| 89 | + |
| 90 | + if single_line: |
| 91 | + # The code collapsed onto one line — almost always a missing block scalar. |
| 92 | + parts.append( |
| 93 | + Text( |
| 94 | + "Hint: this block parsed as a single line. If the code was meant " |
| 95 | + "to span multiple lines, use a YAML block scalar — write " |
| 96 | + "'source: |' and indent the code beneath it.", |
| 97 | + style="yellow", |
| 98 | + ) |
| 99 | + ) |
| 100 | + return parts |
| 101 | + |
| 102 | + |
| 103 | +def _frame_source(frame: traceback.FrameSummary, source_lines: list[str]) -> Optional[str]: |
| 104 | + """Text of the frame's line, mapping exec'd `<string>` frames to the block.""" |
| 105 | + if frame.line: |
| 106 | + return frame.line.strip() |
| 107 | + if frame.filename == "<string>" and 1 <= frame.lineno <= len(source_lines): |
| 108 | + return source_lines[frame.lineno - 1].strip() |
| 109 | + return None |
| 110 | + |
| 111 | + |
| 112 | +def _render_frame(frame: traceback.FrameSummary, source_lines: list[str]) -> Text: |
| 113 | + where = "your Python block" if frame.filename == "<string>" else frame.filename |
| 114 | + text = Text(" ") |
| 115 | + text.append(where, style="cyan") |
| 116 | + text.append(f", line {frame.lineno}, in {frame.name}", style="dim") |
| 117 | + line = _frame_source(frame, source_lines) |
| 118 | + if line: |
| 119 | + text.append("\n ") |
| 120 | + text.append(line, style="white") |
| 121 | + return text |
| 122 | + |
| 123 | + |
| 124 | +def _compact_frames( |
| 125 | + frames: list[traceback.FrameSummary], source_lines: list[str] |
| 126 | +) -> list[RenderableType]: |
| 127 | + if len(frames) <= HEAD_FRAMES + TAIL_FRAMES: |
| 128 | + return [_render_frame(f, source_lines) for f in frames] |
| 129 | + |
| 130 | + hidden = len(frames) - HEAD_FRAMES - TAIL_FRAMES |
| 131 | + head = [_render_frame(f, source_lines) for f in frames[:HEAD_FRAMES]] |
| 132 | + tail = [_render_frame(f, source_lines) for f in frames[-TAIL_FRAMES:]] |
| 133 | + marker = Text(f" … {hidden} frame(s) hidden …", style="dim italic") |
| 134 | + return [*head, marker, *tail] |
| 135 | + |
| 136 | + |
| 137 | +def _failing_source_line( |
| 138 | + frames: list[traceback.FrameSummary], source_lines: list[str] |
| 139 | +) -> Optional[Text]: |
| 140 | + """Highlight the author's own line that raised (the deepest `<string>` frame).""" |
| 141 | + string_frames = [f for f in frames if f.filename == "<string>"] |
| 142 | + if not string_frames: |
| 143 | + return None |
| 144 | + lineno = string_frames[-1].lineno |
| 145 | + if not (1 <= lineno <= len(source_lines)): |
| 146 | + return None |
| 147 | + text = Text() |
| 148 | + text.append(f"→ line {lineno}: ", style="bold yellow") |
| 149 | + text.append(source_lines[lineno - 1].strip(), style="yellow") |
| 150 | + return text |
0 commit comments