forked from contextforge-org/contextforge-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.py
More file actions
69 lines (52 loc) · 1.91 KB
/
Copy patherrors.py
File metadata and controls
69 lines (52 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# -*- coding: utf-8 -*-
"""
SPDX-License-Identifier: Apache-2.0
Error domain for CLI execution.
This module defines CLI-facing exception types and helpers that normalize
exception payloads into user-friendly terminal output. It is the shared
boundary between internal failures and surfaced command errors.
"""
# Standard
from enum import Enum
import json
from typing import Any, Optional, Tuple
# Third-Party
import typer
# First-Party
from cforge.common.console import get_console
class CLIError(Exception):
"""Base class for CLI-related errors."""
class AuthenticationError(CLIError):
"""Raised when authentication fails."""
class CaseInsensitiveEnum(str, Enum):
"""Enum that supports case-insensitive parsing for CLI options."""
@classmethod
def _missing_(cls, value: object) -> Optional["CaseInsensitiveEnum"]:
"""Resolve unknown values by matching enum values case-insensitively."""
if not isinstance(value, str):
return None
value_folded = value.casefold()
for member in cls:
if member.value.casefold() == value_folded:
return member
return None
def split_exception_details(exception: Exception) -> Tuple[str, Any]:
"""Try to parse JSON details from an exception string."""
exc_str = str(exception)
splits = exc_str.split(":", 1)
if len(splits) == 2:
try:
parsed_details = json.loads(splits[1])
return splits[0], parsed_details
except json.JSONDecodeError:
pass
return exc_str, None
def handle_exception(exception: Exception) -> None:
"""Handle an exception and print a friendly error message."""
# First-Party
from cforge.common.render import print_json
e_str, e_detail = split_exception_details(exception)
get_console().print(f"[red]Error: {e_str}[/red]")
if e_detail:
print_json(e_detail, "Error details")
raise typer.Exit(1)