File size: 1,792 Bytes
399b80c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Unified exception & error-code definitions for the grounding framework
"""
from enum import Enum, auto
from typing import Any, Dict


class ErrorCode(str, Enum):
    # generic
    UNKNOWN = auto()
    CONFIG_INVALID = auto()

    # provider / session / connector
    PROVIDER_ERROR = auto()
    SESSION_NOT_FOUND = auto()

    # connection
    CONNECTION_FAILED = auto()
    CONNECTION_TIMEOUT = auto()

    # tool
    TOOL_NOT_FOUND = auto()
    TOOL_EXECUTION_FAIL = auto()
    AMBIGUOUS_TOOL = auto()


class GroundingError(Exception):
    """
    Framework-wide base exception.

    Args:
        message: Human readable error message.
        code: One of the error codes defined above.
        retryable: Whether the caller may retry the operation automatically.
        context: Extra key-value pairs (e.g. tool_name, session_id) for logging / metrics.
    """

    __slots__ = ("message", "code", "retryable", "context")

    def __init__(
        self,
        message: str,
        *,
        code: ErrorCode = ErrorCode.UNKNOWN,
        retryable: bool = False,
        **context: Any,
    ):
        super().__init__(f"[{code}] {message}")
        self.message: str = message
        self.code: ErrorCode = code
        self.retryable: bool = retryable
        self.context: Dict[str, Any] = context

    def to_dict(self) -> Dict[str, Any]:
        """Serialize error for structured logging / JSON response."""
        return {
            "code": self.code.value,
            "message": self.message,
            "retryable": self.retryable,
            "context": self.context,
        }

    def __str__(self) -> str:  
        return f"[{self.code}] {self.message}"

    def __repr__(self) -> str: 
        return f"GroundingError(code={self.code}, msg={self.message!r})"