Skip to content

Commit e71aa49

Browse files
committed
Catch an exact exception
1 parent 60ee25a commit e71aa49

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

futuramaapi/routers/services/favorites/create_favorite_character.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from asyncpg import UniqueViolationError
12
from fastapi import HTTPException, status
23
from sqlalchemy import Insert, Result, insert, select
34
from sqlalchemy.exc import IntegrityError
@@ -21,11 +22,15 @@ def _statement(self) -> Insert[tuple[FavoriteCharacterModel]]:
2122
async def process(self) -> None:
2223
try:
2324
result: Result[tuple[FavoriteCharacterModel]] = await self.session.execute(self._statement)
24-
except IntegrityError:
25-
raise HTTPException(
26-
status_code=status.HTTP_409_CONFLICT,
27-
detail="Character is already in favorites",
28-
) from None
25+
except IntegrityError as err:
26+
await self.session.rollback()
27+
28+
if err.orig.sqlstate == UniqueViolationError.sqlstate:
29+
raise HTTPException(
30+
status_code=status.HTTP_409_CONFLICT,
31+
detail="Character is already in favorites",
32+
) from None
33+
raise
2934

3035
if result.rowcount == 0:
3136
raise HTTPException(

0 commit comments

Comments
 (0)