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:
Cal Corum 2026-01-24 18:19:15 -06:00
parent 31139c5d4d
commit 701098881a
2 changed files with 42 additions and 0 deletions

View File

@ -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, {})

View File

@ -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: