From 769edabbd32eeaf2b9f787c8d21d1f3a753d1e31 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Sun, 26 Jan 2025 14:27:46 +0200 Subject: [PATCH] Add the exceptions and helpers. --- .../shared_kernel/ports/Exceptions/__init__.py | 4 ++++ .../ports/Exceptions/app_exception.py | 3 +++ .../ports/Exceptions/command_exception.py | 6 ++++++ .../errors/applications/exceptions/__init__.py | 3 +++ .../exceptions/exception_helpers.py | 18 ++++++++++++++++++ 5 files changed, 34 insertions(+) create mode 100644 tui_rsync/core/shared_kernel/ports/Exceptions/__init__.py create mode 100644 tui_rsync/core/shared_kernel/ports/Exceptions/app_exception.py create mode 100644 tui_rsync/core/shared_kernel/ports/Exceptions/command_exception.py create mode 100644 tui_rsync/user_interface/cli/shared_kernel/components/errors/applications/exceptions/__init__.py create mode 100644 tui_rsync/user_interface/cli/shared_kernel/components/errors/applications/exceptions/exception_helpers.py diff --git a/tui_rsync/core/shared_kernel/ports/Exceptions/__init__.py b/tui_rsync/core/shared_kernel/ports/Exceptions/__init__.py new file mode 100644 index 0000000..1c85b47 --- /dev/null +++ b/tui_rsync/core/shared_kernel/ports/Exceptions/__init__.py @@ -0,0 +1,4 @@ +from .app_exception import AppException +from .command_exception import CommandException + +__all__ = ['AppException', 'CommandException'] diff --git a/tui_rsync/core/shared_kernel/ports/Exceptions/app_exception.py b/tui_rsync/core/shared_kernel/ports/Exceptions/app_exception.py new file mode 100644 index 0000000..a95db4e --- /dev/null +++ b/tui_rsync/core/shared_kernel/ports/Exceptions/app_exception.py @@ -0,0 +1,3 @@ + +class AppException(Exception): + pass diff --git a/tui_rsync/core/shared_kernel/ports/Exceptions/command_exception.py b/tui_rsync/core/shared_kernel/ports/Exceptions/command_exception.py new file mode 100644 index 0000000..d717922 --- /dev/null +++ b/tui_rsync/core/shared_kernel/ports/Exceptions/command_exception.py @@ -0,0 +1,6 @@ +from . import AppException + + +class CommandException(AppException): + """Command failed to change persistence data.""" + pass diff --git a/tui_rsync/user_interface/cli/shared_kernel/components/errors/applications/exceptions/__init__.py b/tui_rsync/user_interface/cli/shared_kernel/components/errors/applications/exceptions/__init__.py new file mode 100644 index 0000000..8b96dc2 --- /dev/null +++ b/tui_rsync/user_interface/cli/shared_kernel/components/errors/applications/exceptions/__init__.py @@ -0,0 +1,3 @@ +from .exception_helpers import catch_exception + +__all__ = ['catch_exception'] diff --git a/tui_rsync/user_interface/cli/shared_kernel/components/errors/applications/exceptions/exception_helpers.py b/tui_rsync/user_interface/cli/shared_kernel/components/errors/applications/exceptions/exception_helpers.py new file mode 100644 index 0000000..6725ea6 --- /dev/null +++ b/tui_rsync/user_interface/cli/shared_kernel/components/errors/applications/exceptions/exception_helpers.py @@ -0,0 +1,18 @@ +from functools import wraps + +import typer +from rich.console import Console + + +def catch_exception(which_exception, exit_code=1): + def decorator(func): + @wraps(func) + def wrapper(*args, **kwargs): + try: + return func(*args, **kwargs) + except which_exception as e: + console = Console() + console.print(f'[red b][ERROR] {str(e)}[/]') + raise typer.Exit(code=exit_code) + return wrapper + return decorator