-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy patherrors.py
More file actions
70 lines (56 loc) · 1.86 KB
/
errors.py
File metadata and controls
70 lines (56 loc) · 1.86 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
70
import asyncio
from enum import Enum
from typing import Any, Dict, Optional
from pydantic import ValidationError
from redis.exceptions import RedisError
from redisvl.exceptions import RedisSearchError
class MCPErrorCode(str, Enum):
"""Stable internal error codes exposed by the MCP framework."""
INVALID_REQUEST = "invalid_request"
INVALID_FILTER = "invalid_filter"
DEPENDENCY_MISSING = "dependency_missing"
BACKEND_UNAVAILABLE = "backend_unavailable"
INTERNAL_ERROR = "internal_error"
class RedisVLMCPError(Exception):
"""Framework-facing exception carrying a stable MCP error contract."""
def __init__(
self,
message: str,
*,
code: MCPErrorCode,
retryable: bool,
metadata: Optional[Dict[str, Any]] = None,
) -> None:
super().__init__(message)
self.code = code
self.retryable = retryable
self.metadata = metadata or {}
def map_exception(exc: Exception) -> RedisVLMCPError:
"""Map framework exceptions into deterministic MCP-facing exceptions."""
if isinstance(exc, RedisVLMCPError):
return exc
if isinstance(exc, (ValidationError, ValueError, FileNotFoundError)):
return RedisVLMCPError(
str(exc),
code=MCPErrorCode.INVALID_REQUEST,
retryable=False,
)
if isinstance(exc, ImportError):
return RedisVLMCPError(
str(exc),
code=MCPErrorCode.DEPENDENCY_MISSING,
retryable=False,
)
if isinstance(
exc, (TimeoutError, asyncio.TimeoutError, RedisSearchError, RedisError)
):
return RedisVLMCPError(
str(exc),
code=MCPErrorCode.BACKEND_UNAVAILABLE,
retryable=True,
)
return RedisVLMCPError(
str(exc),
code=MCPErrorCode.INTERNAL_ERROR,
retryable=False,
)