"""Error handlers for the CLI functions.""" import logging from collections.abc import Callable from functools import wraps from typing import TypeVar import typer from typing_extensions import ParamSpec logger: logging.Logger = logging.getLogger(__name__) P = ParamSpec("P") T = TypeVar("T") def handle_uncaught_errors(func: Callable[P, T]) -> Callable[P, T]: """Decorator to handle uncaught CLI exceptions.""" @wraps(func) def wrapper(*args: P.args, **kwargs: P.kwargs) -> T: try: return func(*args, **kwargs) except Exception as e: # Send uncaught exceptions to sentry. logger.exception("Unhandled error in '%s': %s", func.__name__, e) raise typer.Exit(1) from e return wrapper