Skip to content

Commit eea40b2

Browse files
committed
feat(cli): add global error handler with rich tracebacks
Implements a global error handler using a decorator to catch all unhandled exceptions. This improves user experience by providing a clear message and a link to the GitHub issues page. Tracebacks are now pretty-printed using the 'rich' library for better readability and easier debugging.
1 parent b0a54e8 commit eea40b2

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/mcpm/cli.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import click
66
from rich.console import Console
77
from rich.table import Table
8+
from rich.traceback import Traceback
89

910
from mcpm import __version__
1011
from mcpm.clients.client_config import ClientConfigManager
@@ -109,10 +110,28 @@ def print_logo():
109110
console.print("[bold cyan]" + "=" * terminal_width + "[/]")
110111

111112

113+
def handle_exceptions(func):
114+
"""Decorator to catch unhandled exceptions and provide a helpful error message."""
115+
116+
def wrapper(*args, **kwargs):
117+
try:
118+
return func(*args, **kwargs)
119+
except Exception:
120+
console.print(Traceback(show_locals=True))
121+
console.print("[bold red]An unexpected error occurred.[/bold red]")
122+
console.print(
123+
"Please report this issue on our GitHub repository: "
124+
"[link=https://github.com/pathintegral-institute/mcpm.sh/issues]https://github.com/pathintegral-institute/mcpm.sh/issues[/link]"
125+
)
126+
127+
return wrapper
128+
129+
112130
@click.group(context_settings=CONTEXT_SETTINGS, invoke_without_command=True)
113131
@click.option("-h", "--help", "help_flag", is_flag=True, help="Show this message and exit.")
114132
@click.option("-v", "--version", is_flag=True, help="Show version and exit.")
115133
@click.pass_context
134+
@handle_exceptions
116135
def main(ctx, help_flag, version):
117136
"""MCPM - Model Context Protocol Manager.
118137

0 commit comments

Comments
 (0)