Skip to content

Commit 82bfeeb

Browse files
committed
remove try except and add EmailNotVerifiedError
1 parent 4a43841 commit 82bfeeb

2 files changed

Lines changed: 25 additions & 13 deletions

File tree

server/app/api/user/repository.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,13 @@
1212
async def find_user_by_username_email(
1313
db: AsyncDatabase, username: str, email: str
1414
) -> Optional[UserAuthOut]:
15-
try:
16-
user_auth = await db.user_auth.find_one(
17-
{"$or": [{"username": username}, {"email": email}]}
18-
)
19-
if not user_auth:
20-
return None
21-
22-
return UserAuthOut(**user_auth)
15+
user_auth = await db.user_auth.find_one(
16+
{"$or": [{"username": username}, {"email": email}]}
17+
)
18+
if not user_auth:
19+
return None
2320

24-
except PyMongoError as e:
25-
logger.critical(f"Failed to query db for user returning error: {e}")
26-
raise InternalServerError()
21+
return UserAuthOut(**user_auth)
2722

2823

2924
async def create_user(db: AsyncDatabase, user: UserRegistration) -> ObjectId:

server/app/core/exceptions.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,35 @@
11
from fastapi import status
22

3+
34
class AppException(Exception):
45
"""Base application exception."""
6+
57
def __init__(self, status_code: int, detail: str):
68
self.status_code = status_code
79
self.detail = detail
810
super().__init__(detail)
911

12+
1013
class UserAlreadyExistsError(AppException):
1114
"""Exception raised when a user tries to register with an email or username that already exists."""
12-
def __init__(self, detail: str = "User with this email or username already exists."):
15+
16+
def __init__(
17+
self, detail: str = "User with this email or username already exists."
18+
):
1319
super().__init__(status_code=status.HTTP_409_CONFLICT, detail=detail)
1420

21+
1522
class InternalServerError(AppException):
1623
"""Exception raised for internal server errors."""
24+
1725
def __init__(self, detail: str = "An internal server error occurred."):
18-
super().__init__(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=detail)
26+
super().__init__(
27+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=detail
28+
)
29+
30+
31+
class EmailNotVerifiedError(AppException):
32+
""" " Exception raised when a user's email is not verified"""
33+
34+
def __init__(self, detail: str = "User email is not verified"):
35+
super().__init__(status_code=status.HTTP_403_FORBIDDEN, detail=detail)

0 commit comments

Comments
 (0)