refactor(ai_client): Move ProviderError to module level
This commit is contained in:
+19
-25
@@ -51,6 +51,25 @@ events: EventEmitter = EventEmitter()
|
|||||||
|
|
||||||
_ai_proxy = None
|
_ai_proxy = None
|
||||||
|
|
||||||
|
class ProviderError(Exception):
|
||||||
|
def __init__(self, kind: str, provider: str, original: Exception) -> None:
|
||||||
|
self.kind = kind
|
||||||
|
self.provider = provider
|
||||||
|
self.original = original
|
||||||
|
super().__init__(str(original))
|
||||||
|
|
||||||
|
def ui_message(self) -> str:
|
||||||
|
labels = {
|
||||||
|
"quota": "QUOTA EXHAUSTED",
|
||||||
|
"rate_limit": "RATE LIMITED",
|
||||||
|
"auth": "AUTH / API KEY ERROR",
|
||||||
|
"balance": "BALANCE / BILLING ERROR",
|
||||||
|
"network": "NETWORK / CONNECTION ERROR",
|
||||||
|
"unknown": "API ERROR",
|
||||||
|
}
|
||||||
|
label = labels.get(self.kind, "API ERROR")
|
||||||
|
return f"[{self.provider.upper()} {label}]\n\n{self.original}"
|
||||||
|
|
||||||
def _get_proxy():
|
def _get_proxy():
|
||||||
global _ai_proxy
|
global _ai_proxy
|
||||||
if _ai_proxy is None and os.environ.get("AI_SERVER_ENABLED"):
|
if _ai_proxy is None and os.environ.get("AI_SERVER_ENABLED"):
|
||||||
@@ -298,31 +317,6 @@ def _load_credentials() -> dict[str, Any]:
|
|||||||
f"Or set SLOP_CREDENTIALS env var to a custom path."
|
f"Or set SLOP_CREDENTIALS env var to a custom path."
|
||||||
)
|
)
|
||||||
|
|
||||||
class ProviderError(Exception):
|
|
||||||
def __init__(self, kind: str, provider: str, original: Exception) -> None:
|
|
||||||
"""
|
|
||||||
[C: src/api_hooks.py:HookServerInstance.__init__, src/mcp_client.py:_DDGParser.__init__, src/mcp_client.py:_TextExtractor.__init__]
|
|
||||||
"""
|
|
||||||
self.kind = kind
|
|
||||||
self.provider = provider
|
|
||||||
self.original = original
|
|
||||||
super().__init__(str(original))
|
|
||||||
|
|
||||||
def ui_message(self) -> str:
|
|
||||||
"""
|
|
||||||
[C: src/app_controller.py:AppController._handle_request_event, src/app_controller.py:AppController.generate]
|
|
||||||
"""
|
|
||||||
labels = {
|
|
||||||
"quota": "QUOTA EXHAUSTED",
|
|
||||||
"rate_limit": "RATE LIMITED",
|
|
||||||
"auth": "AUTH / API KEY ERROR",
|
|
||||||
"balance": "BALANCE / BILLING ERROR",
|
|
||||||
"network": "NETWORK / CONNECTION ERROR",
|
|
||||||
"unknown": "API ERROR",
|
|
||||||
}
|
|
||||||
label = labels.get(self.kind, "API ERROR")
|
|
||||||
return f"[{self.provider.upper()} {label}]\n\n{self.original}"
|
|
||||||
|
|
||||||
def _classify_anthropic_error(exc: Exception) -> ProviderError:
|
def _classify_anthropic_error(exc: Exception) -> ProviderError:
|
||||||
try:
|
try:
|
||||||
if isinstance(exc, anthropic.RateLimitError):
|
if isinstance(exc, anthropic.RateLimitError):
|
||||||
|
|||||||
Reference in New Issue
Block a user