|
| 1 | +"""Custom error types for MCP Text Editor.""" |
| 2 | + |
| 3 | +from enum import Enum, auto |
| 4 | + |
| 5 | + |
| 6 | +class ErrorCode(Enum): |
| 7 | + """Error codes for MCP Text Editor.""" |
| 8 | + |
| 9 | + # Validation errors |
| 10 | + INVALID_REQUEST = auto() |
| 11 | + INVALID_RANGE = auto() |
| 12 | + |
| 13 | + # File operation errors |
| 14 | + FILE_NOT_FOUND = auto() |
| 15 | + FILE_ACCESS_DENIED = auto() |
| 16 | + FILE_CREATE_FAILED = auto() |
| 17 | + FILE_WRITE_FAILED = auto() |
| 18 | + |
| 19 | + # Content operation errors |
| 20 | + CONTENT_HASH_MISMATCH = auto() |
| 21 | + CONTENT_ENCODING_ERROR = auto() |
| 22 | + CONTENT_VALIDATION_ERROR = auto() |
| 23 | + |
| 24 | + |
| 25 | +class MTextEditorError(Exception): |
| 26 | + """Base error class for MCP Text Editor.""" |
| 27 | + |
| 28 | + def __init__( |
| 29 | + self, message: str, code: ErrorCode, details: dict | None = None |
| 30 | + ) -> None: |
| 31 | + """Initialize the error. |
| 32 | +
|
| 33 | + Args: |
| 34 | + message (str): Error message |
| 35 | + code (ErrorCode): Error code |
| 36 | + details (dict | None, optional): Additional error details. Defaults to None. |
| 37 | + """ |
| 38 | + super().__init__(message) |
| 39 | + self.code = code |
| 40 | + self.details = details or {} |
| 41 | + |
| 42 | + |
| 43 | +class ValidationError(MTextEditorError): |
| 44 | + """Raised when input validation fails.""" |
| 45 | + |
| 46 | + |
| 47 | +class FileOperationError(MTextEditorError): |
| 48 | + """Raised when file operations fail.""" |
| 49 | + |
| 50 | + |
| 51 | +class ContentOperationError(MTextEditorError): |
| 52 | + """Raised when content operations fail.""" |
0 commit comments