from __future__ import annotations from typing import Any class DocEngineException(Exception): """Base exception for DocEngine application.""" def __init__(self, detail: str, status_code: int = 500, extra: dict[str, Any] | None = None) -> None: self.detail = detail self.status_code = status_code self.extra = extra or {} super().__init__(self.detail) class NotFoundError(DocEngineException): """Resource not found.""" def __init__(self, resource: str, identifier: str) -> None: super().__init__( detail=f"{resource} with identifier '{identifier}' not found", status_code=404, ) self.resource = resource self.identifier = identifier class DuplicateError(DocEngineException): """Resource already exists.""" def __init__(self, resource: str, field: str, value: str) -> None: super().__init__( detail=f"{resource} with {field} '{value}' already exists", status_code=409, ) class ValidationError(DocEngineException): """Input validation error.""" def __init__(self, detail: str, errors: list[dict[str, Any]] | None = None) -> None: super().__init__(detail=detail, status_code=422) self.errors = errors or [] class AuthenticationError(DocEngineException): """Authentication failed.""" def __init__(self, detail: str = "Authentication failed") -> None: super().__init__(detail=detail, status_code=401) class AuthorizationError(DocEngineException): """Authorization failed.""" def __init__(self, detail: str = "Insufficient permissions") -> None: super().__init__(detail=detail, status_code=403) class StorageError(DocEngineException): """Storage operation failed.""" def __init__(self, detail: str) -> None: super().__init__(detail=detail, status_code=500) class ProcessingError(DocEngineException): """Document processing failed.""" def __init__(self, detail: str, document_id: str | None = None) -> None: super().__init__(detail=detail, status_code=500) self.document_id = document_id class TemplateError(DocEngineException): """Template operation failed.""" def __init__(self, detail: str) -> None: super().__init__(detail=detail, status_code=500) class RateLimitError(DocEngineException): """Rate limit exceeded.""" def __init__(self, detail: str = "Rate limit exceeded. Please try again later.") -> None: super().__init__(detail=detail, status_code=429) class FileSizeError(DocEngineException): """File exceeds maximum allowed size.""" def __init__(self, max_size_mb: int) -> None: super().__init__( detail=f"File size exceeds maximum allowed size of {max_size_mb}MB", status_code=413, ) class UnsupportedFileTypeError(DocEngineException): """File type not supported.""" def __init__(self, file_type: str) -> None: super().__init__( detail=f"File type '{file_type}' is not supported. Supported types: jpg, jpeg, png, tiff, pdf", status_code=415, )