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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
# DEPENDENCIES
import traceback
from enum import Enum
from typing import Any
from typing import Dict
from typing import Optional
from config.models import ErrorResponse
from config.logging_config import get_logger


# Setup Logger
logger = get_logger(__name__)


class ErrorCode(str, Enum):
    """
    Standardized error codes
    """
    # File errors (1xxx)
    FILE_NOT_FOUND            = "FILE_1001"
    FILE_TOO_LARGE            = "FILE_1002"
    INVALID_FILE_TYPE         = "FILE_1003"
    FILE_CORRUPTED            = "FILE_1004"
    FILE_UPLOAD_FAILED        = "FILE_1005"
    
    # Parsing errors (2xxx)
    PARSE_ERROR               = "PARSE_2001"
    PDF_PARSE_ERROR           = "PARSE_2002"
    DOCX_PARSE_ERROR          = "PARSE_2003"
    TEXT_ENCODING_ERROR       = "PARSE_2004"
    
    # Processing errors (3xxx)
    CHUNKING_ERROR            = "PROC_3001"
    EMBEDDING_ERROR           = "PROC_3002"
    INDEXING_ERROR            = "PROC_3003"
    
    
    # LLM errors (5xxx)
    LLM_ERROR                 = "LLM_4001"
    OLLAMA_NOT_AVAILABLE      = "LLM_4002"
    GENERATION_TIMEOUT        = "LLM_4003"
    CONTEXT_TOO_LONG          = "LLM_4004"
    
    # Validation errors (6xxx)
    VALIDATION_ERROR          = "VAL_5001"
    INVALID_INPUT             = "VAL_5002"
    MISSING_REQUIRED_FIELD    = "VAL_5003"
    
    # System errors (7xxx)
    SYSTEM_ERROR              = "SYS_6001"
    DATABASE_ERROR            = "SYS_6002"
    CACHE_ERROR               = "SYS_6003"
    CONFIGURATION_ERROR       = "SYS_6004"
    
    # Vector Search
    VECTOR_SEARCH_ERROR       = "RETR_7001"
    KEYWORD_SEARCH_ERROR      = "RETR_7002"
    RERANKING_ERROR           = "RETR_7003"
    CITATION_ERROR            = "RETR_7004"
    CONTEXT_ASSEMBLY_ERROR    = "RETR_7005"
    HYBRID_RETRIEVAL_ERROR    = "RETR_7006"

    # LLM Generation
    TOKEN_MANAGEMENT_ERROR    = "GEN_8001"
    TEMPERATURE_CONTROL_ERROR = "GEN_8002"
    CITATION_FORMATTING_ERROR = "GEN_8003"
    PROMPT_BUILDING_ERROR     = "GEN_8004"
    LLM_CLIENT_ERROR          = "GEN_8005"
    RESPONSE_GENERATION_ERROR = "GEN_8006"
    
    # Generic
    UNKNOWN_ERROR             = "ERR_9999"


class RAGException(Exception):
    """
    Base exception for RAG system
    """
    def __init__(self, message: str, error_code: ErrorCode = ErrorCode.UNKNOWN_ERROR, details: Optional[Dict[str, Any]] = None, original_error: Optional[Exception] = None):
        self.message        = message
        self.error_code     = error_code
        self.details        = details or {}
        self.original_error = original_error
        
        super().__init__(self.message)

    
    def to_dict(self) -> dict:
        """
        Convert exception to dictionary
        """
        error_dict = {"error"   : self.error_code.value,
                      "message" : self.message,
                      "details" : self.details,
                     }
        
        if self.original_error:
            error_dict["original_error"] = str(self.original_error)
        
        return error_dict
    

    def to_error_response(self) -> ErrorResponse:
        """
        Convert to ErrorResponse model
        """
        return ErrorResponse(error   = self.error_code.value,
                             message = self.message,
                             detail  = self.details if self.details else None,
                            )


# Specific Exceptions
class FileException(RAGException):
    """
    File-related errors
    """
    pass


class FileNotFoundError(FileException):
    """
    File not found
    """
    def __init__(self, file_path: str, **kwargs):
        super().__init__(message    = f"File not found: {file_path}",
                         error_code = ErrorCode.FILE_NOT_FOUND,
                         details    = {"file_path": file_path},
                         **kwargs
                        )


class FileTooLargeError(FileException):
    """
    File exceeds size limit
    """
    def __init__(self, file_size: int, max_size: int, **kwargs):
        super().__init__(message    = f"File size {file_size} bytes exceeds maximum {max_size} bytes",
                         error_code = ErrorCode.FILE_TOO_LARGE,
                         details    = {"file_size": file_size, "max_size": max_size},
                         **kwargs
                        )


class InvalidFileTypeError(FileException):
    """
    Invalid file type
    """
    def __init__(self, file_type: str, allowed_types: list, **kwargs):
        super().__init__(message    = f"Invalid file type '{file_type}'. Allowed: {', '.join(allowed_types)}",
                         error_code = ErrorCode.INVALID_FILE_TYPE,
                         details    = {"file_type": file_type, "allowed_types": allowed_types},
                         **kwargs
                        )


class ParsingException(RAGException):
    """
    Document parsing errors
    """
    pass


class PDFParseError(ParsingException):
    """
    PDF parsing failed
    """
    def __init__(self, file_path: str, **kwargs):
        super().__init__(message    = f"Failed to parse PDF: {file_path}",
                         error_code = ErrorCode.PDF_PARSE_ERROR,
                         details    = {"file_path": file_path},
                         **kwargs
                        )


class DOCXParseError(ParsingException):
    """
    DOCX parsing failed
    """
    def __init__(self, file_path: str, **kwargs):
        super().__init__(message    = f"Failed to parse DOCX: {file_path}",
                         error_code = ErrorCode.DOCX_PARSE_ERROR,
                         details    = {"file_path": file_path},
                         **kwargs
                        )


class TextEncodingError(ParsingException):
    """
    Text encoding error
    """
    def __init__(self, file_path: str, encoding: str, **kwargs):
        super().__init__(message    = f"Failed to decode file {file_path} with encoding {encoding}",
                         error_code = ErrorCode.TEXT_ENCODING_ERROR,
                         details    = {"file_path": file_path, "encoding": encoding},
                         **kwargs
                        )


class OCRException(RAGException):
    """
    OCR-specific exceptions
    """
    pass


class ArchiveException(RAGException):
    """
    Archive-specific exceptions
    """
    pass


class ProcessingException(RAGException):
    """
    Processing errors
    """
    pass


class ChunkingError(ProcessingException):
    """
    Chunking failed
    """
    def __init__(self, document_id: str, **kwargs):
        super().__init__(message    = f"Failed to chunk document: {document_id}",
                         error_code = ErrorCode.CHUNKING_ERROR,
                         details    = {"document_id": document_id},
                         **kwargs
                        )


class EmbeddingError(ProcessingException):
    """
    Embedding generation failed
    """
    def __init__(self, text_length: int, **kwargs):
        super().__init__(message    = f"Failed to generate embeddings for text of length {text_length}",
                         error_code = ErrorCode.EMBEDDING_ERROR,
                         details    = {"text_length": text_length},
                         **kwargs
                        )


class IndexingError(ProcessingException):
    """
    Indexing failed
    """
    def __init__(self, index_type: str, **kwargs):
        super().__init__(message    = f"Failed to index into {index_type}",
                         error_code = ErrorCode.INDEXING_ERROR,
                         details    = {"index_type": index_type},
                         **kwargs
                        )


class VectorSearchError(RAGException):
    """
    Vector search errors
    """
    pass


class KeywordSearchError(RAGException):
    """
    Keyword search errors
    """
    pass


class RerankingError(RAGException):
    """
    Reranking errors
    """
    pass


class CitationError(RAGException):
    """
    Citation tracking errors
    """
    pass


class ContextAssemblyError(RAGException):
    """
    Context assembly errors
    """
    pass


class HybridRetrievalError(RAGException):
    """
    Hybrid retrieval errors
    """
    pass


class TokenManagementError(RAGException):
    """
    Token management errors
    """
    pass


class TemperatureControlError(RAGException):
    """
    Temperature control errors
    """
    pass


class CitationFormattingError(RAGException):
    """
    Citation formatting errors
    """
    pass


class PromptBuildingError(RAGException):
    """
    Prompt building errors
    """
    pass


class LLMClientError(RAGException):
    """
    LLM client errors
    """
    pass


class ResponseGenerationError(RAGException):
    """
    Response generation errors
    """
    pass


class NoResultsFoundError(RAGException):
    """
    No results found
    """
    def __init__(self, query: str, **kwargs):
        super().__init__(message    = f"No results found for query: {query}",
                         error_code = ErrorCode.NO_RESULTS_FOUND,
                         details    = {"query": query},
                         **kwargs
                        )


class LLMException(RAGException):
    """
    LLM errors
    """
    pass


class OllamaNotAvailableError(LLMException):
    """
    Ollama service not available
    """
    def __init__(self, base_url: str, **kwargs):
        super().__init__(message    = f"Ollama service not available at {base_url}",
                         error_code = ErrorCode.OLLAMA_NOT_AVAILABLE,
                         details    = {"base_url": base_url},
                         **kwargs
                        )


class GenerationTimeoutError(LLMException):
    """
    Generation timeout
    """
    def __init__(self, timeout: int, **kwargs):
        super().__init__(message    = f"LLM generation timed out after {timeout} seconds",
                         error_code = ErrorCode.GENERATION_TIMEOUT,
                         details    = {"timeout": timeout},
                         **kwargs
                        )


class ContextTooLongError(LLMException):
    """
    Context exceeds window
    """
    def __init__(self, context_length: int, max_length: int, **kwargs):
        super().__init__(message    = f"Context length {context_length} exceeds maximum {max_length}",
                         error_code = ErrorCode.CONTEXT_TOO_LONG,
                         details    = {"context_length": context_length, "max_length": max_length},
                         **kwargs
                        )


class ValidationException(RAGException):
    """
    Validation errors
    """
    pass


# Error Handler Decorators
def handle_errors(error_type: type = RAGException, log_error: bool = True, reraise: bool = True):
    """
    Decorator to handle errors in functions
    
    Arguments:
    ----------
        error_type { RAGException } : Exception type to catch (default: RAGException)

        log_error      { bool }     : Whether to log the error
        
        reraise        { bool }     : Whether to reraise after handling
    """
    def decorator(func):
        def wrapper(*args, **kwargs):
            try:
                return func(*args, **kwargs)
            
            except error_type as e:
                if log_error:
                    logger.error(f"Error in {func.__name__}: {str(e)}", exc_info=True)
                
                if reraise:
                    raise
                
                else:
                    return None
        
        return wrapper
    return decorator


def safe_execute(func, *args, default = None, log_errors: bool = True, **kwargs):
    """
    Safely execute a function with error handling
    
    Arguments:
    ----------
        func       : Function to execute

        *args      : Function arguments
        
        default    : Default value on error
        
        log_errors : Whether to log errors
        
        **kwargs   : Function keyword arguments
    
    Returns:
    --------
        Function result or default on error
    """
    try:
        return func(*args, **kwargs)

    except Exception as e:
        if log_errors:
            logger.error(f"Error in {func.__name__}: {str(e)}", exc_info=True)
        
        return default


class ErrorContext:
    """
    Context manager for error handling
    """
    
    def __init__(self, operation: str, raise_on_error: bool = True, log_on_error: bool = True, **context_data):
        self.operation                  = operation
        self.raise_on_error             = raise_on_error
        self.log_on_error               = log_on_error
        self.context_data               = context_data
        self.error: Optional[Exception] = None

    
    def __enter__(self):
        logger.debug(f"Starting: {self.operation}")
        
        return self
    

    def __exit__(self, exc_type, exc_val, exc_tb):
        if exc_type is not None:
            self.error = exc_val
            
            if self.log_on_error:
                logger.error(f"Error in {self.operation}: {exc_val}",
                             extra    = self.context_data,
                             exc_info = True,
                            )
            
            if not self.raise_on_error:
                # Suppress exception
                return True
        
        return False


def log_and_raise(error: Exception, message: str,**context):
    """
    Log error and raise
    
    Arguments:
    ----------
        error     : Exception to raise
        
        message   : Log message
        
        **context : Additional context to log
    """
    logger.error(message, extra=context, exc_info=True)
    
    raise error


def format_error_message(error: Exception, include_traceback: bool = False) -> str:
    """
    Format error message for display.
    
    Arguments:
    ----------
        error             : Exception

        include_traceback : Include full traceback
    
    Returns:
    --------
            { str }       : Formatted error message
    """
    if isinstance(error, RAGException):
        message = f"[{error.error_code.value}] {error.message}"
        
        if error.details:
            message += f"\nDetails: {error.details}"
    
    else:
        message = f"{type(error).__name__}: {str(error)}"
    
    if include_traceback:
        message += f"\n\nTraceback:\n{''.join(traceback.format_tb(error.__traceback__))}"
    
    return message


class EvaluationError(RAGException):
    """
    Custom exception for evaluation-related errors
    """
    def __init__(self, message: str, original_error: Optional[Exception] = None):
        super().__init__(error_type     = "EvaluationError",
                         message        = message,
                         original_error = original_error,
                         status_code    = 500,
                        )

        self.evaluation_context = None
    

    def with_context(self, context: Dict[str, Any]) -> 'EvaluationError':
        """
        Add evaluation context to the error
        """
        self.evaluation_context = context

        return self