исправление кодовых ошибок
This commit is contained in:
+13
-3
@@ -42,10 +42,20 @@ async def register_user(user_in: CreateUser, db: AsyncSession = Depends(get_db))
|
||||
)
|
||||
except Exception as e:
|
||||
await db.rollback()
|
||||
if isinstance(e.__cause__, asyncpg.exceptions.UniqueViolationError) or "unique constraint" in str(e).lower():
|
||||
raise HTTPException(status_code=409, detail={"code": "conflict", "message": "Username already exists"})
|
||||
|
||||
# Проверяем и по __cause__, и по тексту ошибки (надёжнее для SQLAlchemy)
|
||||
error_str = str(e).lower()
|
||||
if "unique constraint" in error_str or "duplicate key" in error_str:
|
||||
raise HTTPException(
|
||||
status_code=409,
|
||||
detail={"code": "conflict", "message": "Username already exists"}
|
||||
)
|
||||
|
||||
logger.error(f"Database error during registration: {e}")
|
||||
raise HTTPException(status_code=500, detail={"code": "internal_error", "message": "Database error"})
|
||||
raise HTTPException(
|
||||
status_code=500,
|
||||
detail={"code": "internal_error", "message": "Database error"}
|
||||
)
|
||||
|
||||
@router.post("/api/v1/auth/sessions", response_model=Session, status_code=status.HTTP_201_CREATED, responses={401: {"model": ErrorResponse}})
|
||||
async def create_session(credentials = Depends(get_basic_auth), db: AsyncSession = Depends(get_db), redis_client = Depends(get_redis)):
|
||||
|
||||
Reference in New Issue
Block a user