Skip to content

Commit

Permalink
Better failure when task name already exists (closes #64)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcompa committed Sep 22, 2022
1 parent 7b0afb6 commit 6f2fe78
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions fractal_server/app/api/v1/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from fastapi import APIRouter
from fastapi import Depends
from fastapi import HTTPException
from fastapi import status
from sqlalchemy.exc import IntegrityError
from sqlmodel import select
Expand Down Expand Up @@ -96,9 +97,16 @@ async def create_task(
db: AsyncSession = Depends(get_db),
):
db_task = Task.from_orm(task)
db.add(db_task)
await db.commit()
await db.refresh(db_task)
try:
db.add(db_task)
await db.commit()
await db.refresh(db_task)
except IntegrityError as e:
await db.rollback()
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail=str(e),
)
return db_task


Expand Down

0 comments on commit 6f2fe78

Please sign in to comment.