errors.py (2003B)
1 """Error types for Agent API.""" 2 3 from typing import Any 4 5 6 class AgentAPIError(Exception): 7 """Base exception for all Agent API errors.""" 8 9 10 class AgentCLIConnectionError(AgentAPIError): 11 """Raised when unable to connect to Agent Code.""" 12 13 14 class AgentCLINotFoundError(AgentCLIConnectionError): 15 """Raised when Agent Code is not found or not installed.""" 16 17 def __init__(self, message: str, cli_path: str | None = None) -> None: 18 if cli_path: 19 message = f"{message}: {cli_path}" 20 super().__init__(message) 21 22 23 class AgentProcessError(AgentAPIError): 24 """Raised when the CLI process fails.""" 25 26 def __init__(self, message: str, exit_code: int | None = None, stderr: str | None = None) -> None: 27 self.exit_code = exit_code 28 self.stderr = stderr 29 30 if exit_code is not None: 31 message = f"{message} (exit code: {exit_code})" 32 if stderr: 33 message = f"{message}\nError output: {stderr}" 34 35 super().__init__(message) 36 37 38 class AgentCLIJSONDecodeError(AgentAPIError): 39 """Raised when unable to decode JSON from CLI output.""" 40 41 def __init__(self, line: str, original_error: Exception) -> None: 42 self.line = line 43 self.original_error = original_error 44 super().__init__(f"Failed to decode JSON: {line[:100]}...") 45 46 47 class AgentUnknownMessageTypeError(AgentAPIError): 48 """Raised when an unknown message type is encountered.""" 49 50 def __init__(self, message_type: str, data: dict[str, Any]) -> None: 51 self.message_type = message_type 52 self.data = data 53 super().__init__(f"Unknown message type: {message_type}\nData: {data}") 54 55 56 class AgentUnknownContentBlockTypeError(AgentAPIError): 57 """Raised when an unknown content block type is encountered.""" 58 59 def __init__(self, block_type: str, data: dict[str, Any]) -> None: 60 self.block_type = block_type 61 self.data = data 62 super().__init__(f"Unknown content block type: {block_type}\nData: {data}")