Spaces:
Runtime error
Runtime error
File size: 1,115 Bytes
6aa6081 | 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 | import traceback
from fastapi import Request
from fastapi.responses import JSONResponse
from app.core.database import SessionLocal
from app.models.system_log import SystemLog
async def system_error_handler(request: Request, exc: Exception) -> JSONResponse:
tb = "".join(traceback.format_exception(type(exc), exc, exc.__traceback__))
account_id = None
user_id = None
if hasattr(request.state, "user") and request.state.user:
account_id = request.state.user.account_id
user_id = request.state.user.id
try:
db = SessionLocal()
log = SystemLog(
level="error",
message=str(exc)[:500],
traceback=tb,
endpoint=str(request.url.path),
method=request.method,
account_id=account_id,
user_id=user_id,
ip_address=request.client.host if request.client else None,
)
db.add(log)
db.commit()
db.close()
except Exception:
pass
return JSONResponse(
status_code=500,
content={"detail": "Internal server error"},
)
|