Spaces:
Running
Running
| """ | |
| MediGuard AI β Domain Exception Hierarchy | |
| Production-grade exception classes for the medical RAG system. | |
| Each service layer raises its own exception type so callers can handle | |
| failures precisely without leaking implementation details. | |
| """ | |
| from typing import Any | |
| # ββ Base ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class MediGuardError(Exception): | |
| """Root exception for the entire MediGuard AI application.""" | |
| def __init__(self, message: str = "", *, details: dict[str, Any] | None = None): | |
| self.details = details or {} | |
| super().__init__(message) | |
| # ββ Configuration / startup ββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class ConfigurationError(MediGuardError): | |
| """Raised when a required setting is missing or invalid.""" | |
| class ServiceInitError(MediGuardError): | |
| """Raised when a service fails to initialise during app startup.""" | |
| # ββ Database βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class DatabaseError(MediGuardError): | |
| """Base class for all database-related errors.""" | |
| class ConnectionError(DatabaseError): | |
| """Could not connect to PostgreSQL.""" | |
| class RecordNotFoundError(DatabaseError): | |
| """Expected record does not exist.""" | |
| # ββ Search engine ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class SearchError(MediGuardError): | |
| """Base class for search-engine (OpenSearch) errors.""" | |
| class IndexNotFoundError(SearchError): | |
| """The requested OpenSearch index does not exist.""" | |
| class SearchQueryError(SearchError): | |
| """The search query was malformed or returned an error.""" | |
| # ββ Embeddings βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class EmbeddingError(MediGuardError): | |
| """Failed to generate embeddings.""" | |
| class EmbeddingProviderError(EmbeddingError): | |
| """The upstream embedding provider returned an error.""" | |
| # ββ PDF / document parsing βββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class PDFParsingError(MediGuardError): | |
| """Base class for PDF-processing errors.""" | |
| class PDFExtractionError(PDFParsingError): | |
| """Could not extract text from a PDF document.""" | |
| class PDFValidationError(PDFParsingError): | |
| """Uploaded PDF failed validation (size, format, etc.).""" | |
| # ββ LLM / Ollama βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class LLMError(MediGuardError): | |
| """Base class for LLM-related errors.""" | |
| class OllamaConnectionError(LLMError): | |
| """Could not reach the Ollama server.""" | |
| class OllamaModelNotFoundError(LLMError): | |
| """The requested Ollama model is not pulled/available.""" | |
| class LLMResponseError(LLMError): | |
| """The LLM returned an unparseable or empty response.""" | |
| # ββ Biomarker domain βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class BiomarkerError(MediGuardError): | |
| """Base class for biomarker-related errors.""" | |
| class BiomarkerValidationError(BiomarkerError): | |
| """A biomarker value is physiologically implausible.""" | |
| class BiomarkerNotFoundError(BiomarkerError): | |
| """The biomarker name is unknown to the system.""" | |
| # ββ Medical analysis / workflow ββββββββββββββββββββββββββββββββββββββββββββββ | |
| class AnalysisError(MediGuardError): | |
| """The clinical-analysis workflow encountered an error.""" | |
| class GuardrailError(MediGuardError): | |
| """A safety guardrail was triggered (input or output).""" | |
| class OutOfScopeError(GuardrailError): | |
| """The user query falls outside the medical domain.""" | |
| # ββ Cache ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class CacheError(MediGuardError): | |
| """Base class for cache (Redis) errors.""" | |
| class CacheConnectionError(CacheError): | |
| """Could not connect to Redis.""" | |
| # ββ Observability ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class ObservabilityError(MediGuardError): | |
| """Langfuse or metrics reporting failed (non-fatal).""" | |
| # ββ Telegram bot βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class TelegramError(MediGuardError): | |
| """Error from the Telegram bot integration.""" | |