Skip to content

Commit 64dfda4

Browse files
committed
Extract conflict exception
1 parent 5260792 commit 64dfda4

File tree

4 files changed

+13
-6
lines changed

4 files changed

+13
-6
lines changed

futuramaapi/apps/fastapi.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,17 @@ def _setup_static(self) -> None:
100100

101101
def _exception_handler(self, _: Request, exc) -> Response:
102102
from futuramaapi.routers.services import ( # noqa: PLC0415
103+
ConflictError,
103104
NotFoundError,
104105
ServiceError,
105106
UnauthorizedError,
106107
)
107108

108109
exception_to_value: dict[type[ServiceError], _ExceptionValue] = {
110+
ConflictError: _ExceptionValue(
111+
status_code=status.HTTP_409_CONFLICT,
112+
default_message="Conflict",
113+
),
109114
UnauthorizedError: _ExceptionValue(
110115
status_code=status.HTTP_401_UNAUTHORIZED,
111116
default_message="Unauthorized",

futuramaapi/routers/services/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
BaseService,
33
BaseSessionService,
44
BaseUserAuthenticatedService,
5+
ConflictError,
56
NotFoundError,
67
ServiceError,
78
UnauthorizedError,
@@ -13,6 +14,7 @@
1314
"BaseSessionService",
1415
"BaseTemplateService",
1516
"BaseUserAuthenticatedService",
17+
"ConflictError",
1618
"NotFoundError",
1719
"ServiceError",
1820
"UnauthorizedError",

futuramaapi/routers/services/_base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ class NotFoundError(ServiceError):
3232
"""Not Found Error."""
3333

3434

35+
class ConflictError(ServiceError):
36+
"""Conflict Error."""
37+
38+
3539
class BaseService[TResponse](BaseModel, ABC):
3640
context: dict[str, Any] | None = None
3741

futuramaapi/routers/services/favorites/create_favorite_character.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
from asyncpg import UniqueViolationError
2-
from fastapi import HTTPException, status
32
from sqlalchemy import Insert, Result, insert, select
43
from sqlalchemy.exc import IntegrityError
54

65
from futuramaapi.repositories.models import CharacterModel, FavoriteCharacterModel
7-
from futuramaapi.routers.services import BaseUserAuthenticatedService, NotFoundError
6+
from futuramaapi.routers.services import BaseUserAuthenticatedService, ConflictError, NotFoundError
87

98

109
class CreateFavoriteCharacterService(BaseUserAuthenticatedService[None]):
@@ -26,10 +25,7 @@ async def process(self) -> None:
2625
await self.session.rollback()
2726

2827
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
28+
raise ConflictError("Character is already in favorites") from None
3329
raise
3430

3531
if result.rowcount == 0:

0 commit comments

Comments
 (0)