refactor: Improve exception handling and logging in authentication and playlist services; enhance code readability and structure
This commit is contained in:
@@ -467,11 +467,13 @@ class AuthService:
|
||||
# If user has existing password, verify it
|
||||
if had_existing_password:
|
||||
if not current_password:
|
||||
raise ValueError("Current password is required when changing existing password")
|
||||
msg = "Current password is required when changing existing password"
|
||||
raise ValueError(msg)
|
||||
if not PasswordUtils.verify_password(current_password, user.password_hash):
|
||||
raise ValueError("Current password is incorrect")
|
||||
msg = "Current password is incorrect"
|
||||
raise ValueError(msg)
|
||||
else:
|
||||
# User doesn't have a password (OAuth-only user), so we're setting their first password
|
||||
# User doesn't have a password (OAuth-only user), setting first password
|
||||
logger.info("Setting first password for OAuth user: %s", user_email)
|
||||
|
||||
# Hash new password
|
||||
@@ -509,6 +511,6 @@ class AuthService:
|
||||
updated_at=user.updated_at,
|
||||
)
|
||||
|
||||
async def get_user_oauth_providers(self, user: User):
|
||||
async def get_user_oauth_providers(self, user: User) -> list:
|
||||
"""Get OAuth providers connected to the user."""
|
||||
return await self.oauth_repo.get_by_user_id(user.id)
|
||||
|
||||
@@ -223,11 +223,11 @@ class CreditService:
|
||||
user_id,
|
||||
)
|
||||
|
||||
return transaction
|
||||
|
||||
except Exception:
|
||||
await session.rollback()
|
||||
raise
|
||||
else:
|
||||
return transaction
|
||||
finally:
|
||||
await session.close()
|
||||
|
||||
@@ -316,11 +316,11 @@ class CreditService:
|
||||
user_id,
|
||||
)
|
||||
|
||||
return transaction
|
||||
|
||||
except Exception:
|
||||
await session.rollback()
|
||||
raise
|
||||
else:
|
||||
return transaction
|
||||
finally:
|
||||
await session.close()
|
||||
|
||||
@@ -503,11 +503,11 @@ class CreditService:
|
||||
user_id,
|
||||
)
|
||||
|
||||
return transaction
|
||||
|
||||
except Exception:
|
||||
await session.rollback()
|
||||
raise
|
||||
else:
|
||||
return transaction
|
||||
finally:
|
||||
await session.close()
|
||||
|
||||
|
||||
@@ -87,7 +87,7 @@ class DashboardService:
|
||||
)
|
||||
raise
|
||||
|
||||
def _get_date_filter(self, period: str) -> datetime | None:
|
||||
def _get_date_filter(self, period: str) -> datetime | None: # noqa: PLR0911
|
||||
"""Calculate the date filter based on the period."""
|
||||
now = datetime.now(UTC)
|
||||
|
||||
|
||||
@@ -212,12 +212,13 @@ class PlaylistService:
|
||||
"""Search all playlists by name."""
|
||||
return await self.playlist_repo.search_by_name(query)
|
||||
|
||||
async def search_and_sort_playlists(
|
||||
async def search_and_sort_playlists( # noqa: PLR0913
|
||||
self,
|
||||
search_query: str | None = None,
|
||||
sort_by: PlaylistSortField | None = None,
|
||||
sort_order: SortOrder = SortOrder.ASC,
|
||||
user_id: int | None = None,
|
||||
*,
|
||||
include_stats: bool = False,
|
||||
limit: int | None = None,
|
||||
offset: int = 0,
|
||||
@@ -269,7 +270,7 @@ class PlaylistService:
|
||||
current_sounds = await self.playlist_repo.get_playlist_sounds(playlist_id)
|
||||
position = len(current_sounds)
|
||||
else:
|
||||
# Ensure position doesn't create gaps - if position is too high, place at end
|
||||
# Ensure position doesn't create gaps - if too high, place at end
|
||||
current_sounds = await self.playlist_repo.get_playlist_sounds(playlist_id)
|
||||
max_position = len(current_sounds)
|
||||
position = min(position, max_position)
|
||||
@@ -329,7 +330,11 @@ class PlaylistService:
|
||||
# Create sequential positions: 0, 1, 2, 3...
|
||||
sound_positions = [(sound.id, index) for index, sound in enumerate(sounds)]
|
||||
await self.playlist_repo.reorder_playlist_sounds(playlist_id, sound_positions)
|
||||
logger.debug("Reordered %s sounds in playlist %s to eliminate gaps", len(sounds), playlist_id)
|
||||
logger.debug(
|
||||
"Reordered %s sounds in playlist %s to eliminate gaps",
|
||||
len(sounds),
|
||||
playlist_id,
|
||||
)
|
||||
|
||||
async def reorder_playlist_sounds(
|
||||
self,
|
||||
|
||||
Reference in New Issue
Block a user