File size: 734 Bytes
64d7fdf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from fastapi import Request, status
from fastapi.responses import JSONResponse
from app.utils.errors import RagChatbotError
from app.utils.logger import logger


async def error_handler_middleware(request: Request, call_next):
    try:
        response = await call_next(request)
        return response
    except RagChatbotError as e:
        logger.error(f"RAG error: {str(e)}")
        return JSONResponse(
            status_code=e.status_code,
            content={"detail": str(e)}
        )
    except Exception as e:
        logger.error(f"Unhandled error: {str(e)}")
        return JSONResponse(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            content={"detail": "Internal server error"}
        )