Skip to content

Rich MarkupError when displaying error messages with square brackets #39

@lkakarla

Description

@lkakarla

The CLI application crashes with a rich.errors.MarkupError when trying to display error messages that contain square brackets. Rich interprets the brackets as markup tags, causing the application to fail.

Steps to Reproduce

  1. Run the metis CLI tool
  2. Trigger an error that contains square brackets in the error message
  3. The application crashes instead of displaying the error message

Expected Behavior

Error messages should be displayed properly regardless of whether they contain square brackets or other special characters.

Actual Behavior

Application crashes with rich.errors.MarkupError when error messages contain square brackets.

Environment

  • Python version: 3.13
  • Rich version: 14.1.0
  • OS: Ubuntu
  • Metis version: 0.1.2

Root Cause

The print_console() function in src/metis/cli/utils.py doesn't handle Rich markup errors gracefully. When error messages contain square brackets, Rich tries to parse them as markup tags, causing the crash.

Error:

Traceback (most recent call last):
  File "/home/metis/.venv/bin/metis", line 10, in <module>
    sys.exit(main())
             ~~~~^^
  File "/home/metis/src/metis/cli/entry.py", line 247, in main
    print_console(f"[bold red]Error:[/bold red] {e}", args.quiet)
    ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/metis/src/metis/cli/utils.py", line 50, in print_console
    console.print(message, **kwargs)
    ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
  File "/home/metis/.venv/lib/python3.13/site-packages/rich/console.py", line 1698, in print
    renderables = self._collect_renderables(
        objects,
    ...<5 lines>...
        highlight=highlight,
    )
  File "/home/metis/.venv/lib/python3.13/site-packages/rich/console.py", line 1558, in _collect_renderables
    self.render_str(
    ~~~~~~~~~~~~~~~^
        renderable,
        ^^^^^^^^^^^
    ...<3 lines>...
        highlighter=_highlighter,
        ^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/home/metis/.venv/lib/python3.13/site-packages/rich/console.py", line 1448, in render_str
    rich_text = render_markup(
        text,
    ...<2 lines>...
        emoji_variant=self._emoji_variant,
    )
  File "/home/metis/.venv/lib/python3.13/site-packages/rich/markup.py", line 167, in render
    raise MarkupError(
        f"closing tag '{tag.markup}' at position {position} doesn't match any open tag"
    ) from None
rich.errors.MarkupError: closing tag '[/*4**/]' at position 41 doesn't match any open tag

Proposed Solution

Modify the print_console() function to handle markup errors gracefully:

def print_console(message, quiet=False, **kwargs):
    if not quiet:
        try:
            console.print(message, **kwargs)
        except Exception:
            # If markup parsing fails, print without markup
            kwargs_no_markup = kwargs.copy()
            kwargs_no_markup['markup'] = False
            console.print(message, **kwargs_no_markup)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions