From 5def2220019bef54d3eff08ac0dfeecb938d3b0d Mon Sep 17 00:00:00 2001 From: Shanto Mathew Date: Sat, 15 Nov 2025 13:39:17 -0600 Subject: [PATCH] feat: Add comprehensive database and encoding error handling exceptions Added four new exception classes to improve error handling: - DatabaseConnectionError: Enhanced database connection errors with recovery hints - DatabaseTimeoutError: Timeout errors with operation context - EncodingError: UTF-8 and character encoding issues - DataValidationError: Data validation failures in RAG processing These exceptions provide better error messages, contextual information, and recovery suggestions for common database and encoding issues reported in the issues tracker. --- lightrag/exceptions.py | 57 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/lightrag/exceptions.py b/lightrag/exceptions.py index 303391c2..efaf5069 100644 --- a/lightrag/exceptions.py +++ b/lightrag/exceptions.py @@ -112,3 +112,60 @@ class QdrantMigrationError(Exception): def __init__(self, message: str): super().__init__(message) self.message = message + + +# Database-related exceptions for improved error handling + +class DatabaseConnectionError(APIConnectionError): + """Raised when database connection fails with recovery suggestions.""" + + def __init__(self, message: str, database_type: str = "", recovery_hint: str = ""): + self.database_type = database_type + self.recovery_hint = recovery_hint + detailed_msg = f"Database connection error + if database_type: + detailed_msg += f" [{database_type}]" + detailed_msg += f": {message}" + if recovery_hint: + detailed_msg += f"\nRecovery: {recovery_hint}" + super().__init__(detailed_msg) + + + class DatabaseTimeoutError(APITimeoutError): + """Raised when database operations timeout.""" + + def __init__(self, message: str, operation: str = "", timeout_seconds: float = 0): + self.operation = operation + self.timeout_seconds = timeout_seconds + detailed_msg = f"Database timeout: {message}" + if operation: + detailed_msg += f" during '{operation}'" + if timeout_seconds > 0: + detailed_msg += f" (timeout: {timeout_seconds}s)" + super().__init__(detailed_msg) + + + class EncodingError(Exception): + """Raised when character encoding issues occur (UTF-8, etc.).""" + + def __init__(self, message: str, encoding: str = "UTF-8", field_name: str = ""): + self.encoding = encoding + self.field_name = field_name + detailed_msg = f"Encoding error [{encoding}]: {message}" + if field_name: + detailed_msg += f" in field '{field_name}'" + super().__init__(detailed_msg) + + + class DataValidationError(Exception): + """Raised when data validation fails in RAG processing.""" + + def __init__(self, message: str, data_type: str = "", expected_format: str = ""): + self.data_type = data_type + self.expected_format = expected_format + detailed_msg = f"Data validation error: {message}" + if data_type: + detailed_msg += f" for data type '{data_type}'" + if expected_format: + detailed_msg += f". Expected format: {expected_format}" + super().__init__(detailed_msg) \ No newline at end of file