CLAUDE: Add validation to prevent null team metadata in game creation
- Add validation in create_game() and quick_create_game() to ensure both teams are successfully fetched from SBA API before creating game - Raise HTTP 400 with clear error message if team data cannot be fetched - Add warning logs in get_teams_by_ids() when teams are missing from result - Prevents games from being created with null team display info (names, abbreviations, colors, thumbnails) Root cause: get_teams_by_ids() silently returned empty dict on API failures, and game creation endpoints didn't validate the result. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
31139c5d4d
commit
701098881a
@ -325,6 +325,22 @@ async def create_game(request: CreateGameRequest):
|
||||
teams_data = await sba_api_client.get_teams_by_ids(
|
||||
[request.home_team_id, request.away_team_id], season=request.season
|
||||
)
|
||||
|
||||
# Validate that we successfully fetched both teams
|
||||
missing_teams = [
|
||||
tid for tid in [request.home_team_id, request.away_team_id]
|
||||
if tid not in teams_data
|
||||
]
|
||||
if missing_teams:
|
||||
logger.error(
|
||||
f"Failed to fetch team data for IDs {missing_teams} in season {request.season}"
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=f"Could not fetch team data for team IDs: {missing_teams}. "
|
||||
f"Verify teams exist in season {request.season}."
|
||||
)
|
||||
|
||||
home_team_data = teams_data.get(request.home_team_id, {})
|
||||
away_team_data = teams_data.get(request.away_team_id, {})
|
||||
|
||||
@ -439,6 +455,22 @@ async def quick_create_game(
|
||||
teams_data = await sba_api_client.get_teams_by_ids(
|
||||
[home_team_id, away_team_id], season=13
|
||||
)
|
||||
|
||||
# Validate that we successfully fetched both teams
|
||||
missing_teams = [
|
||||
tid for tid in [home_team_id, away_team_id]
|
||||
if tid not in teams_data
|
||||
]
|
||||
if missing_teams:
|
||||
logger.error(
|
||||
f"Quick-create: Failed to fetch team data for IDs {missing_teams}"
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=f"Could not fetch team data for team IDs: {missing_teams}. "
|
||||
f"Verify teams exist in season 13."
|
||||
)
|
||||
|
||||
home_team_data = teams_data.get(home_team_id, {})
|
||||
away_team_data = teams_data.get(away_team_id, {})
|
||||
|
||||
|
||||
@ -184,6 +184,16 @@ class SbaApiClient:
|
||||
team = await self.get_team_by_id(team_id, season)
|
||||
if team:
|
||||
result[team_id] = team
|
||||
else:
|
||||
logger.warning(f"Team {team_id} not found in cache for season {season}")
|
||||
|
||||
# Log if we couldn't find all requested teams
|
||||
missing = [tid for tid in team_ids if tid not in result]
|
||||
if missing:
|
||||
logger.warning(
|
||||
f"get_teams_by_ids: {len(missing)}/{len(team_ids)} teams not found: {missing}"
|
||||
)
|
||||
|
||||
return result
|
||||
|
||||
async def get_player(self, player_id: int) -> SbaPlayer:
|
||||
|
||||
Loading…
Reference in New Issue
Block a user