diff --git a/backend/app/api/routes/games.py b/backend/app/api/routes/games.py index eb6000d..4129e5d 100644 --- a/backend/app/api/routes/games.py +++ b/backend/app/api/routes/games.py @@ -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, {}) diff --git a/backend/app/services/sba_api_client.py b/backend/app/services/sba_api_client.py index 56eb925..5d3599c 100644 --- a/backend/app/services/sba_api_client.py +++ b/backend/app/services/sba_api_client.py @@ -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: