# Copyright (c) 2024 Rajesh
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""Custom exceptions for docx-mcp server."""
class DocxMcpError(Exception):
"""Base exception for all docx-mcp errors."""
def __init__(self, message: str, error_code: str = "INTERNAL_ERROR", status_code: int = 500):
"""Initialize exception."""
self.message = message
self.error_code = error_code
self.status_code = status_code
super().__init__(message)
class FileNotFoundError(DocxMcpError):
"""File does not exist."""
def __init__(self, filepath: str):
super().__init__(
f"File not found: {filepath}",
error_code="FILE_NOT_FOUND",
status_code=404,
)
class PermissionDeniedError(DocxMcpError):
"""Access to file or directory is denied."""
def __init__(self, filepath: str, reason: str = "Access denied"):
super().__init__(
f"Permission denied for {filepath}: {reason}",
error_code="PERMISSION_DENIED",
status_code=403,
)
class InvalidPathError(DocxMcpError):
"""Path is invalid or contains suspicious patterns."""
def __init__(self, filepath: str, reason: str = "Invalid path"):
super().__init__(
f"Invalid path: {filepath} - {reason}",
error_code="INVALID_PATH",
status_code=400,
)
class FileSizeExceededError(DocxMcpError):
"""File size exceeds maximum allowed."""
def __init__(self, filepath: str, size: int, max_size: int):
super().__init__(
f"File size {size} bytes exceeds maximum of {max_size} bytes: {filepath}",
error_code="FILE_SIZE_EXCEEDED",
status_code=413,
)
class InvalidFormatError(DocxMcpError):
"""File format is invalid or not supported."""
def __init__(self, filepath: str, format: str = "Unknown"):
super().__init__(
f"Invalid or unsupported format: {format} for {filepath}",
error_code="INVALID_FORMAT",
status_code=400,
)
class DocumentError(DocxMcpError):
"""Error reading or processing document."""
def __init__(self, message: str, filepath: str = ""):
full_msg = f"Document error: {message}"
if filepath:
full_msg += f" ({filepath})"
super().__init__(
full_msg,
error_code="DOCUMENT_ERROR",
status_code=422,
)
class InvalidParameterError(DocxMcpError):
"""Invalid parameter provided to tool."""
def __init__(self, param_name: str, reason: str):
super().__init__(
f"Invalid parameter '{param_name}': {reason}",
error_code="INVALID_PARAMETER",
status_code=400,
)
class NotImplementedError(DocxMcpError):
"""Feature is not yet implemented."""
def __init__(self, feature: str):
super().__init__(
f"Feature not yet implemented: {feature}",
error_code="NOT_IMPLEMENTED",
status_code=501,
)