File size: 3,185 Bytes
b62e029
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# core/exceptions.py

from fastapi import Request, status
from fastapi.responses import JSONResponse

from core.logger import setup_logger

logger = setup_logger("exception_handler")

# ---------------------------------------------------
# Base Exception (Parent class of all custom errors)
# ---------------------------------------------------
class KnowledgeEngineException(Exception):
    """Base custom exception class for the Knowledge Engine application"""
    def __init__(self, message: str, status_code: int = status.HTTP_500_INTERNAL_SERVER_ERROR):
        self.message = message
        self.status_code = status_code
        super().__init__(self.message)

# ---------------------------------------------------
# Domain Specific Exceptions (Hierarchical error)
# ---------------------------------------------------
class ModelLoadError(KnowledgeEngineException):
    """models/ layer where model (Embedder/Reranker) loading fails"""
    def __init__(self, message: str):
        super().__init__(message, status_code=status.HTTP_503_SERVICE_UNAVAILABLE)

class DatabaseError(KnowledgeEngineException):
    """storage/ layer where Qdrant or SQLite integration fails"""
    def __init__(self, message: str):
        super().__init__(message, status_code=status.HTTP_503_SERVICE_UNAVAILABLE)

class SearchExecutionError(KnowledgeEngineException):
    """services/ layer where the search pipeline (Hybrid Search) encounters a logical error"""
    def __init__(self, message: str):
        super().__init__(message, status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)

class InvalidQueryError(KnowledgeEngineException):
    """api/ layer where user input is invalid (e.g., empty query, unsupported parameters)"""
    def __init__(self, message: str):
        super().__init__(message, status_code=status.HTTP_400_BAD_REQUEST)

# -----------------------------------
# FastAPI Exception Handler 
# -----------------------------------
async def custom_exception_handler(request: Request, exc: KnowledgeEngineException):
    """
    When a custom exception occurs in a FastAPI app,  
    catch it and convert it into a consistent JSON error response.
    """
    logger.error(f"[{exc.status_code}] {request.method} {request.url} - {exc.message}")
    return JSONResponse(
        status_code=exc.status_code,
        content={
            "error": exc.__class__.__name__,
            "message": exc.message,
            "path": str(request.url.path)
        }
    )
async def global_exception_handler(request: Request, exc: Exception):
    """Catch any unhandled exceptions that are not instances of KnowledgeEngineException,  
    log them, and return a generic error response."""
    logger.critical(f"Unhandled Exception: {str(exc)}", exc_info=True) # Log stack trace for debugging
    return JSONResponse(
        status_code=500,
        content={"error": "InternalServerError", "message": "An unexpected error occurred."}
    )

def setup_exception_handlers(app):
    """Register custom exception handlers to the FastAPI app."""
    app.add_exception_handler(KnowledgeEngineException, custom_exception_handler)
    app.add_exception_handler(Exception, global_exception_handler)