File size: 2,985 Bytes
266d7bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import Request
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from qdrant_client.http.exceptions import UnexpectedResponse

from src.utils.logger_util import setup_logging

logger = setup_logging()


async def validation_exception_handler(request: Request, exc: Exception) -> JSONResponse:
    """Handle FastAPI request validation errors.

    Args:
        request (Request): The incoming request that caused the validation error.
        exc (Exception): The exception instance.

    Returns:
        JSONResponse: A JSON response with status code 422 and error details.

    """
    if isinstance(exc, RequestValidationError):
        logger.warning(f"Validation error on {request.url}: {exc.errors()}")
        return JSONResponse(
            status_code=422,
            content={
                "type": "validation_error",
                "message": "Invalid request",
                "details": exc.errors(),
            },
        )

    logger.exception(f"Unexpected exception on {request.url}: {exc}")
    return JSONResponse(
        status_code=500,
        content={
            "type": "internal_error",
            "message": "Internal server error",
            "details": str(exc),
        },
    )


async def qdrant_exception_handler(request: Request, exc: Exception) -> JSONResponse:
    """Handle unexpected responses from Qdrant.

    Args:
        request (Request): The incoming request that caused the error.
        exc (Exception): The exception instance.

    Returns:
        JSONResponse: A JSON response with status code 500 and error details.

    """
    if isinstance(exc, UnexpectedResponse):
        logger.error(f"Qdrant error on {request.url}: {exc}")
        return JSONResponse(
            status_code=500,
            content={
                "type": "qdrant_error",
                "message": "Vector store error",
                "details": str(exc),
            },
        )

    # Fallback to general internal error if exception is not UnexpectedResponse
    logger.exception(f"Unexpected exception on {request.url}: {exc}")
    return JSONResponse(
        status_code=500,
        content={
            "type": "internal_error",
            "message": "Internal server error",
            "details": str(exc),
        },
    )


async def general_exception_handler(request: Request, exc: Exception) -> JSONResponse:
    """Handle all uncaught exceptions in FastAPI.

    Args:
        request (Request): The incoming request that caused the error.
        exc (Exception): The exception instance.

    Returns:
        JSONResponse: A JSON response with status code 500 and error details.

    """
    logger.exception(f"Unhandled exception on {request.url}: {exc}")
    return JSONResponse(
        status_code=500,
        content={
            "type": "internal_error",
            "message": "Internal server error",
            "details": str(exc),
        },
    )