File size: 677 Bytes
8413ad5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from app.core.logger import logger
import traceback

def add_global_exception_handlers(app: FastAPI):
    @app.exception_handler(Exception)
    async def global_exception_handler(request: Request, exc: Exception):
        logger.error(f"CRITICAL SYSTEM CRASH: {request.method} {request.url.path}")
        logger.error(traceback.format_exc())

        return JSONResponse(
            status_code=500,
            content={
                "error": "An unexpected internal server error occurred. Our team has been notified.",
                "code": "INTERNAL_SERVER_ERROR"
            }
        )