-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrors.py
More file actions
120 lines (90 loc) · 2.74 KB
/
errors.py
File metadata and controls
120 lines (90 loc) · 2.74 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""
Custom exception hierarchy for code-memory.
All exceptions inherit from CodeMemoryError for easy catching.
Each exception type maps to a specific error category for
structured error responses to MCP clients.
"""
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
import api_types
class CodeMemoryError(Exception):
"""Base exception for all code-memory errors.
All custom exceptions should inherit from this class.
Provides a consistent interface for error handling.
"""
def __init__(self, message: str, details: dict | None = None):
self.message = message
self.details = details or {}
super().__init__(message)
def to_dict(self) -> api_types.ErrorResponse:
"""Convert exception to structured error response dict."""
return {
"error": True,
"error_type": self.__class__.__name__,
"message": self.message,
"details": self.details if self.details else None,
}
class DatabaseError(CodeMemoryError):
"""Database operation failed.
Raised when:
- Database file not found or corrupted
- SQL execution fails
- sqlite-vec operations fail
- Schema migration errors
"""
pass
class IndexingError(CodeMemoryError):
"""Code or documentation indexing failed.
Raised when:
- File parsing fails
- Embedding generation fails
- File read errors
- Unsupported file type errors
"""
pass
class GitError(CodeMemoryError):
"""Git operation failed.
Raised when:
- Not a git repository
- Git command fails
- Invalid commit hash
- File not in git history
"""
pass
class ValidationError(CodeMemoryError):
"""Input validation failed.
Raised when:
- Empty or invalid query
- Invalid search_type
- Path traversal attempt
- Invalid line numbers
- File/directory not found
"""
pass
class EmbeddingError(CodeMemoryError):
"""Embedding model operation failed.
Raised when:
- Model fails to load
- Embedding generation fails
- Vector dimension mismatch
"""
pass
def format_error(error: Exception) -> api_types.ErrorResponse:
"""Format any exception as a structured error response.
Args:
error: Any exception (CodeMemoryError or built-in)
Returns:
Structured error dict suitable for MCP response
"""
if isinstance(error, CodeMemoryError):
return error.to_dict()
# Handle common built-in exceptions
error_type = error.__class__.__name__
message = str(error) or f"An error of type {error_type} occurred"
return {
"error": True,
"error_type": error_type,
"message": message,
"details": None,
}