Spaces:
Running
Running
Added exception handler for global exceptions
Browse files- backend/app/core/exceptions.py +18 -0
- backend/main.py +3 -0
backend/app/core/exceptions.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
from app.core.logger import logger
|
| 4 |
+
import traceback
|
| 5 |
+
|
| 6 |
+
def add_global_exception_handlers(app: FastAPI):
|
| 7 |
+
@app.exception_handler(Exception)
|
| 8 |
+
async def global_exception_handler(request: Request, exc: Exception):
|
| 9 |
+
logger.error(f"CRITICAL SYSTEM CRASH: {request.method} {request.url.path}")
|
| 10 |
+
logger.error(traceback.format_exc())
|
| 11 |
+
|
| 12 |
+
return JSONResponse(
|
| 13 |
+
status_code=500,
|
| 14 |
+
content={
|
| 15 |
+
"error": "An unexpected internal server error occurred. Our team has been notified.",
|
| 16 |
+
"code": "INTERNAL_SERVER_ERROR"
|
| 17 |
+
}
|
| 18 |
+
)
|
backend/main.py
CHANGED
|
@@ -15,6 +15,7 @@ from app.core.logging_middleware import APILoggingMiddleware
|
|
| 15 |
from app.api.user_routes import router as user_router
|
| 16 |
from app.core.limiter import limiter
|
| 17 |
from starlette.middleware.sessions import SessionMiddleware
|
|
|
|
| 18 |
|
| 19 |
@asynccontextmanager
|
| 20 |
async def lifespan(app: FastAPI):
|
|
@@ -26,6 +27,8 @@ app = FastAPI(
|
|
| 26 |
lifespan=lifespan
|
| 27 |
)
|
| 28 |
|
|
|
|
|
|
|
| 29 |
app.state.limiter = limiter
|
| 30 |
|
| 31 |
api_v1_router = APIRouter(prefix="/api/v1")
|
|
|
|
| 15 |
from app.api.user_routes import router as user_router
|
| 16 |
from app.core.limiter import limiter
|
| 17 |
from starlette.middleware.sessions import SessionMiddleware
|
| 18 |
+
from app.core.exceptions import add_global_exception_handlers
|
| 19 |
|
| 20 |
@asynccontextmanager
|
| 21 |
async def lifespan(app: FastAPI):
|
|
|
|
| 27 |
lifespan=lifespan
|
| 28 |
)
|
| 29 |
|
| 30 |
+
add_global_exception_handlers(app)
|
| 31 |
+
|
| 32 |
app.state.limiter = limiter
|
| 33 |
|
| 34 |
api_v1_router = APIRouter(prefix="/api/v1")
|