Skip to content

Commit

Permalink
Fix unmapped codes
Browse files Browse the repository at this point in the history
  • Loading branch information
cmyui committed Aug 6, 2024
1 parent 36df17d commit 51bcbc1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
12 changes: 11 additions & 1 deletion app/api/public/users.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from fastapi import APIRouter, Cookie
from fastapi import Response
from pydantic import BaseModel
Expand All @@ -12,14 +13,23 @@


def map_error_code_to_http_status_code(error_code: ErrorCode) -> int:
return _error_code_to_http_status_code_map[error_code]
status_code = _error_code_to_http_status_code_map.get(error_code)
if status_code is None:
logging.warning(
"No HTTP status code mapping found for error code: %s",
error_code,
extra={"error_code": error_code},
)
return 500
return status_code


_error_code_to_http_status_code_map: dict[ErrorCode, int] = {
ErrorCode.INCORRECT_CREDENTIALS: 401,
ErrorCode.INSUFFICIENT_PRIVILEGES: 401,
ErrorCode.PENDING_VERIFICATION: 401,
ErrorCode.NOT_FOUND: 404,
ErrorCode.CONFLICT: 409,
ErrorCode.INTERNAL_SERVER_ERROR: 500,
}

Expand Down
3 changes: 1 addition & 2 deletions app/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ class ErrorCode(StrEnum):
INSUFFICIENT_PRIVILEGES = "INSUFFICIENT_PRIVILEGES"
PENDING_VERIFICATION = "PENDING_VERIFICATION"
NOT_FOUND = "NOT_FOUND"

USERNAME_ALREADY_TAKEN = "USERNAME_ALREADY_TAKEN"
CONFLICT = "CONFLICT"

INTERNAL_SERVER_ERROR = "INTERNAL_SERVER_ERROR"

Expand Down
2 changes: 1 addition & 1 deletion app/usecases/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ async def update_username(user_id: int, new_username: str) -> None | Error:
exists = await users.username_is_taken(new_username)
if exists:
return Error(
error_code=ErrorCode.USERNAME_ALREADY_TAKEN,
error_code=ErrorCode.CONFLICT,
user_feedback="Username is already taken.",
)

Expand Down

0 comments on commit 51bcbc1

Please sign in to comment.