refactor: Improve exception handling and logging in authentication and playlist services; enhance code readability and structure
All checks were successful
Backend CI / lint (push) Successful in 9m21s
Backend CI / test (push) Successful in 4m18s

This commit is contained in:
JSC
2025-08-13 00:04:55 +02:00
parent f094fbf140
commit bee1076239
14 changed files with 144 additions and 66 deletions

View File

@@ -181,7 +181,9 @@ async def logout(
user_id = int(user_id_str)
user = await auth_service.get_current_user(user_id)
logger.info("Found user from access token: %s", user.email)
except (HTTPException, Exception) as e:
except HTTPException as e:
logger.info("Access token validation failed: %s", str(e))
except Exception as e: # noqa: BLE001
logger.info("Access token validation failed: %s", str(e))
# If no user found, try refresh token
@@ -193,7 +195,9 @@ async def logout(
user_id = int(user_id_str)
user = await auth_service.get_current_user(user_id)
logger.info("Found user from refresh token: %s", user.email)
except (HTTPException, Exception) as e:
except HTTPException as e:
logger.info("Refresh token validation failed: %s", str(e))
except Exception as e: # noqa: BLE001
logger.info("Refresh token validation failed: %s", str(e))
# If we found a user, revoke their refresh token
@@ -484,7 +488,6 @@ async def change_password(
await auth_service.change_user_password(
current_user, request.current_password, request.new_password,
)
return {"message": "Password changed successfully"}
except ValueError as e:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
@@ -496,6 +499,8 @@ async def change_password(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to change password",
) from e
else:
return {"message": "Password changed successfully"}
@router.get("/user-providers")

View File

@@ -33,9 +33,18 @@ async def get_track_statistics(
async def get_top_sounds(
_current_user: Annotated[User, Depends(get_current_user)],
dashboard_service: Annotated[DashboardService, Depends(get_dashboard_service)],
sound_type: Annotated[str, Query(description="Sound type filter (SDB, TTS, EXT, or 'all')")],
period: Annotated[str, Query(description="Time period (today, 1_day, 1_week, 1_month, 1_year, all_time)")] = "all_time",
limit: Annotated[int, Query(description="Number of top sounds to return", ge=1, le=100)] = 10,
sound_type: Annotated[
str, Query(description="Sound type filter (SDB, TTS, EXT, or 'all')"),
],
period: Annotated[
str,
Query(
description="Time period (today, 1_day, 1_week, 1_month, 1_year, all_time)",
),
] = "all_time",
limit: Annotated[
int, Query(description="Number of top sounds to return", ge=1, le=100),
] = 10,
) -> list[dict[str, Any]]:
"""Get top sounds by play count for a specific period."""
return await dashboard_service.get_top_sounds(

View File

@@ -69,7 +69,10 @@ async def elements_docs() -> HTMLResponse:
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
>
<title>API Documentation - elements</title>
<script src="https://unpkg.com/@stoplight/elements/web-components.min.js"></script>
<link rel="stylesheet" href="https://unpkg.com/@stoplight/elements/styles.min.css">

View File

@@ -32,7 +32,7 @@ async def get_playlist_service(
@router.get("/")
async def get_all_playlists(
async def get_all_playlists( # noqa: PLR0913
current_user: Annotated[User, Depends(get_current_active_user_flexible)], # noqa: ARG001
playlist_service: Annotated[PlaylistService, Depends(get_playlist_service)],
search: Annotated[
@@ -57,7 +57,7 @@ async def get_all_playlists(
] = 0,
) -> list[dict]:
"""Get all playlists from all users with search and sorting."""
playlists = await playlist_service.search_and_sort_playlists(
return await playlist_service.search_and_sort_playlists(
search_query=search,
sort_by=sort_by,
sort_order=sort_order,
@@ -66,7 +66,6 @@ async def get_all_playlists(
limit=limit,
offset=offset,
)
return playlists
@router.get("/user")

View File

@@ -35,7 +35,7 @@ async def get_sound_repository(
@router.get("/")
async def get_sounds(
async def get_sounds( # noqa: PLR0913
current_user: Annotated[User, Depends(get_current_active_user_flexible)], # noqa: ARG001
sound_repo: Annotated[SoundRepository, Depends(get_sound_repository)],
types: Annotated[