From 0c042165b7b26ac7a0cb110da94b8ff21e27df05 Mon Sep 17 00:00:00 2001 From: Cal Corum Date: Tue, 3 Mar 2026 19:05:16 -0600 Subject: [PATCH] fix: replace broad except Exception blocks with DoesNotExist (#15) Replace 71 broad `except Exception` blocks in 19 router files with the specific `peewee.DoesNotExist` exception. GET endpoints that call `Model.get_by_id()` now only catch the expected DoesNotExist error, allowing real DB failures (connection errors, etc.) to propagate as 500s rather than being masked as 404s. Co-Authored-By: Claude Sonnet 4.6 --- app/routers_v2/awards.py | 6 +++--- app/routers_v2/batstats.py | 4 ++-- app/routers_v2/cards.py | 16 ++++++++-------- app/routers_v2/cardsets.py | 12 ++++++------ app/routers_v2/current.py | 8 ++++---- app/routers_v2/events.py | 8 ++++---- app/routers_v2/gamerewards.py | 8 ++++---- app/routers_v2/gauntletrewards.py | 4 ++-- app/routers_v2/gauntletruns.py | 4 ++-- app/routers_v2/notifications.py | 8 ++++---- app/routers_v2/packs.py | 16 ++++++++-------- app/routers_v2/packtypes.py | 8 ++++---- app/routers_v2/paperdex.py | 8 ++++---- app/routers_v2/pitstats.py | 4 ++-- app/routers_v2/players.py | 15 ++++++++------- app/routers_v2/rarity.py | 8 ++++---- app/routers_v2/results.py | 18 +++++++++--------- app/routers_v2/rewards.py | 8 ++++---- app/routers_v2/teams.py | 25 +++++++++++++------------ 19 files changed, 95 insertions(+), 93 deletions(-) diff --git a/app/routers_v2/awards.py b/app/routers_v2/awards.py index 30cfbe2..3d79030 100644 --- a/app/routers_v2/awards.py +++ b/app/routers_v2/awards.py @@ -4,7 +4,7 @@ import logging import pydantic from pandas import DataFrame -from ..db_engine import Award, model_to_dict +from ..db_engine import Award, model_to_dict, DoesNotExist from ..dependencies import oauth2_scheme, valid_token, PRIVATE_IN_SCHEMA @@ -73,7 +73,7 @@ async def get_awards( async def get_one_award(award_id, csv: Optional[bool] = None): try: this_award = Award.get_by_id(award_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No award found with id {award_id}') if csv: @@ -130,7 +130,7 @@ async def delete_award(award_id, token: str = Depends(oauth2_scheme)): ) try: this_award = Award.get_by_id(award_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No award found with id {award_id}') count = this_award.delete_instance() diff --git a/app/routers_v2/batstats.py b/app/routers_v2/batstats.py index d0dfd05..95c9db9 100644 --- a/app/routers_v2/batstats.py +++ b/app/routers_v2/batstats.py @@ -6,7 +6,7 @@ import logging import pydantic from pandas import DataFrame -from ..db_engine import db, BattingStat, model_to_dict, fn, Card, Player, Current +from ..db_engine import db, BattingStat, model_to_dict, fn, Card, Player, Current, DoesNotExist from ..dependencies import oauth2_scheme, valid_token, PRIVATE_IN_SCHEMA @@ -236,7 +236,7 @@ async def delete_batstat(stat_id, token: str = Depends(oauth2_scheme)): ) try: this_stat = BattingStat.get_by_id(stat_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No stat found with id {stat_id}') count = this_stat.delete_instance() diff --git a/app/routers_v2/cards.py b/app/routers_v2/cards.py index 419b236..96b9774 100644 --- a/app/routers_v2/cards.py +++ b/app/routers_v2/cards.py @@ -4,7 +4,7 @@ import logging import pydantic from pandas import DataFrame -from ..db_engine import db, Card, model_to_dict, Team, Player, Pack, Paperdex, CARDSETS +from ..db_engine import db, Card, model_to_dict, Team, Player, Pack, Paperdex, CARDSETS, DoesNotExist from ..dependencies import oauth2_scheme, valid_token @@ -41,19 +41,19 @@ async def get_cards( if team_id is not None: try: this_team = Team.get_by_id(team_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No team found with id {team_id}') all_cards = all_cards.where(Card.team == this_team) if player_id is not None: try: this_player = Player.get_by_id(player_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No player found with id {player_id}') all_cards = all_cards.where(Card.player == this_player) if pack_id is not None: try: this_pack = Pack.get_by_id(pack_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No pack found with id {pack_id}') all_cards = all_cards.where(Card.pack == this_pack) if value is not None: @@ -125,7 +125,7 @@ async def get_cards( async def v1_cards_get_one(card_id, csv: Optional[bool] = False): try: this_card = Card.get_by_id(card_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No card found with id {card_id}') if csv: @@ -268,7 +268,7 @@ async def v1_cards_wipe_team(team_id: int, token: str = Depends(oauth2_scheme)): try: this_team = Team.get_by_id(team_id) - except Exception as e: + except DoesNotExist as e: logging.error(f'/cards/wipe-team/{team_id} - could not find team') raise HTTPException(status_code=404, detail=f'Team {team_id} not found') @@ -289,7 +289,7 @@ async def v1_cards_patch( ) try: this_card = Card.get_by_id(card_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No card found with id {card_id}') if player_id is not None: @@ -332,7 +332,7 @@ async def v1_cards_delete(card_id, token: str = Depends(oauth2_scheme)): ) try: this_card = Card.get_by_id(card_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No cards found with id {card_id}') count = this_card.delete_instance() diff --git a/app/routers_v2/cardsets.py b/app/routers_v2/cardsets.py index 610680b..a92e55a 100644 --- a/app/routers_v2/cardsets.py +++ b/app/routers_v2/cardsets.py @@ -4,7 +4,7 @@ import logging import pydantic from pandas import DataFrame -from ..db_engine import Cardset, model_to_dict, fn, Event +from ..db_engine import Cardset, model_to_dict, fn, Event, DoesNotExist from ..dependencies import oauth2_scheme, valid_token @@ -41,7 +41,7 @@ async def get_cardsets( try: this_event = Event.get_by_id(event_id) all_cardsets = all_cardsets.where(Cardset.event == this_event) - except Exception as e: + except DoesNotExist as e: logging.error(f'Failed to find event {event_id}: {e}') raise HTTPException(status_code=404, detail=f'Event id {event_id} not found') if in_packs is not None: @@ -104,7 +104,7 @@ async def search_cardsets( try: this_event = Event.get_by_id(event_id) all_cardsets = all_cardsets.where(Cardset.event == this_event) - except Exception as e: + except DoesNotExist as e: logging.error(f'Failed to find event {event_id}: {e}') raise HTTPException(status_code=404, detail=f'Event id {event_id} not found') @@ -150,7 +150,7 @@ async def search_cardsets( async def get_one_cardset(cardset_id, csv: Optional[bool] = False): try: this_cardset = Cardset.get_by_id(cardset_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No cardset found with id {cardset_id}') if csv: @@ -205,7 +205,7 @@ async def patch_cardsets( ) try: this_cardset = Cardset.get_by_id(cardset_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No cardset found with id {cardset_id}') if name is not None: @@ -241,7 +241,7 @@ async def delete_cardsets(cardset_id, token: str = Depends(oauth2_scheme)): ) try: this_cardset = Cardset.get_by_id(cardset_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No cardset found with id {cardset_id}') count = this_cardset.delete_instance() diff --git a/app/routers_v2/current.py b/app/routers_v2/current.py index c122229..2184291 100644 --- a/app/routers_v2/current.py +++ b/app/routers_v2/current.py @@ -4,7 +4,7 @@ from typing import Optional import logging import pydantic -from ..db_engine import Current, model_to_dict +from ..db_engine import Current, model_to_dict, DoesNotExist from ..dependencies import oauth2_scheme, valid_token, PRIVATE_IN_SCHEMA @@ -45,7 +45,7 @@ async def get_current(season: Optional[int] = None, csv: Optional[bool] = False) async def get_one_current(current_id, csv: Optional[bool] = False): try: current = Current.get_by_id(current_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No current found with id {current_id}') if csv: @@ -102,7 +102,7 @@ async def patch_current( ) try: current = Current.get_by_id(current_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No current found with id {current_id}') if season is not None: @@ -136,7 +136,7 @@ async def delete_current(current_id, token: str = Depends(oauth2_scheme)): ) try: this_curr = Current.get_by_id(current_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No current found with id {current_id}') count = this_curr.delete_instance() diff --git a/app/routers_v2/events.py b/app/routers_v2/events.py index ce368a6..cd989cb 100644 --- a/app/routers_v2/events.py +++ b/app/routers_v2/events.py @@ -4,7 +4,7 @@ import logging import pydantic from pandas import DataFrame -from ..db_engine import Event, model_to_dict, fn +from ..db_engine import Event, model_to_dict, fn, DoesNotExist from ..dependencies import oauth2_scheme, valid_token @@ -63,7 +63,7 @@ async def v1_events_get( async def v1_events_get_one(event_id, csv: Optional[bool] = False): try: this_event = Event.get_by_id(event_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No event found with id {event_id}') if csv: @@ -127,7 +127,7 @@ async def v1_events_patch( ) try: this_event = Event.get_by_id(event_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No event found with id {event_id}') if name is not None: @@ -163,7 +163,7 @@ async def v1_events_delete(event_id, token: str = Depends(oauth2_scheme)): ) try: this_event = Event.get_by_id(event_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No event found with id {event_id}') count = this_event.delete_instance() diff --git a/app/routers_v2/gamerewards.py b/app/routers_v2/gamerewards.py index bef669f..724f12a 100644 --- a/app/routers_v2/gamerewards.py +++ b/app/routers_v2/gamerewards.py @@ -4,7 +4,7 @@ import logging import pydantic from pandas import DataFrame -from ..db_engine import GameRewards, model_to_dict +from ..db_engine import GameRewards, model_to_dict, DoesNotExist from ..dependencies import oauth2_scheme, valid_token @@ -63,7 +63,7 @@ async def v1_gamerewards_get( async def v1_gamerewards_get_one(gamereward_id, csv: Optional[bool] = None): try: this_game_reward = GameRewards.get_by_id(gamereward_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No game reward found with id {gamereward_id}') if csv: @@ -120,7 +120,7 @@ async def v1_gamerewards_patch( ) try: this_game_reward = GameRewards.get_by_id(game_reward_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No game reward found with id {game_reward_id}') if name is not None: @@ -161,7 +161,7 @@ async def v1_gamerewards_delete(gamereward_id, token: str = Depends(oauth2_schem ) try: this_award = GameRewards.get_by_id(gamereward_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No award found with id {gamereward_id}') count = this_award.delete_instance() diff --git a/app/routers_v2/gauntletrewards.py b/app/routers_v2/gauntletrewards.py index 0a3ef1b..6022da6 100644 --- a/app/routers_v2/gauntletrewards.py +++ b/app/routers_v2/gauntletrewards.py @@ -3,7 +3,7 @@ from typing import Optional, List import logging import pydantic -from ..db_engine import db, GauntletReward, model_to_dict, DatabaseError +from ..db_engine import db, GauntletReward, model_to_dict, DatabaseError, DoesNotExist from ..db_helpers import upsert_gauntlet_rewards from ..dependencies import oauth2_scheme, valid_token @@ -57,7 +57,7 @@ async def v1_gauntletreward_get( async def v1_gauntletreward_get_one(gauntletreward_id): try: this_reward = GauntletReward.get_by_id(gauntletreward_id) - except Exception: + except DoesNotExist: raise HTTPException( status_code=404, detail=f"No gauntlet reward found with id {gauntletreward_id}", diff --git a/app/routers_v2/gauntletruns.py b/app/routers_v2/gauntletruns.py index ad3f101..cc85eeb 100644 --- a/app/routers_v2/gauntletruns.py +++ b/app/routers_v2/gauntletruns.py @@ -4,7 +4,7 @@ from typing import Optional import logging import pydantic -from ..db_engine import GauntletRun, model_to_dict, DatabaseError +from ..db_engine import GauntletRun, model_to_dict, DatabaseError, DoesNotExist from ..dependencies import oauth2_scheme, valid_token @@ -84,7 +84,7 @@ async def get_gauntletruns( async def get_one_gauntletrun(gauntletrun_id): try: this_gauntlet = GauntletRun.get_by_id(gauntletrun_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No gauntlet found with id {gauntletrun_id}') return_val = model_to_dict(this_gauntlet) diff --git a/app/routers_v2/notifications.py b/app/routers_v2/notifications.py index c29012a..7fec666 100644 --- a/app/routers_v2/notifications.py +++ b/app/routers_v2/notifications.py @@ -5,7 +5,7 @@ import logging import pydantic from pandas import DataFrame -from ..db_engine import Notification, model_to_dict, fn +from ..db_engine import Notification, model_to_dict, fn, DoesNotExist from ..dependencies import oauth2_scheme, valid_token @@ -74,7 +74,7 @@ async def get_notifs( async def get_one_notif(notif_id, csv: Optional[bool] = None): try: this_notif = Notification.get_by_id(notif_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No notification found with id {notif_id}') if csv: @@ -135,7 +135,7 @@ async def patch_notif( ) try: this_notif = Notification.get_by_id(notif_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No notification found with id {notif_id}') if title is not None: @@ -173,7 +173,7 @@ async def delete_notif(notif_id, token: str = Depends(oauth2_scheme)): ) try: this_notif = Notification.get_by_id(notif_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No notification found with id {notif_id}') count = this_notif.delete_instance() diff --git a/app/routers_v2/packs.py b/app/routers_v2/packs.py index 19b43c9..77b3739 100644 --- a/app/routers_v2/packs.py +++ b/app/routers_v2/packs.py @@ -6,7 +6,7 @@ import logging import pydantic from pandas import DataFrame -from ..db_engine import db, Cardset, model_to_dict, Pack, Team, PackType +from ..db_engine import db, Cardset, model_to_dict, Pack, Team, PackType, DoesNotExist from ..dependencies import oauth2_scheme, valid_token @@ -41,20 +41,20 @@ async def get_packs( if team_id is not None: try: this_team = Team.get_by_id(team_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No team found with id {team_id}') all_packs = all_packs.where(Pack.team == this_team) if pack_type_id is not None: try: this_pack_type = PackType.get_by_id(pack_type_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No pack type found with id {pack_type_id}') all_packs = all_packs.where(Pack.pack_type == this_pack_type) if pack_team_id is not None: try: this_pack_team = Team.get_by_id(pack_team_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No team found with id {pack_team_id}') all_packs = all_packs.where(Pack.pack_team == this_pack_team) elif exact_match: @@ -63,7 +63,7 @@ async def get_packs( if pack_cardset_id is not None: try: this_pack_cardset = Cardset.get_by_id(pack_cardset_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No cardset found with id {pack_cardset_id}') all_packs = all_packs.where(Pack.pack_cardset == this_pack_cardset) elif exact_match: @@ -107,7 +107,7 @@ async def get_packs( async def get_one_pack(pack_id: int, csv: Optional[bool] = False): try: this_pack = Pack.get_by_id(pack_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No pack found with id {pack_id}') if csv: @@ -191,7 +191,7 @@ async def patch_pack( ) try: this_pack = Pack.get_by_id(pack_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No pack found with id {pack_id}') if team_id is not None: @@ -234,7 +234,7 @@ async def delete_pack(pack_id, token: str = Depends(oauth2_scheme)): ) try: this_pack = Pack.get_by_id(pack_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No packs found with id {pack_id}') count = this_pack.delete_instance() diff --git a/app/routers_v2/packtypes.py b/app/routers_v2/packtypes.py index 5ce5195..a6940a8 100644 --- a/app/routers_v2/packtypes.py +++ b/app/routers_v2/packtypes.py @@ -4,7 +4,7 @@ import logging import pydantic from pandas import DataFrame -from ..db_engine import PackType, model_to_dict, fn +from ..db_engine import PackType, model_to_dict, fn, DoesNotExist from ..dependencies import oauth2_scheme, valid_token @@ -68,7 +68,7 @@ async def get_packtypes( async def get_one_packtype(packtype_id, csv: Optional[bool] = False): try: this_packtype = PackType.get_by_id(packtype_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No packtype found with id {packtype_id}') if csv: @@ -129,7 +129,7 @@ async def patch_packtype( ) try: this_packtype = PackType.get_by_id(packtype_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No packtype found with id {packtype_id}') if name is not None: @@ -163,7 +163,7 @@ async def delete_packtype(packtype_id, token: str = Depends(oauth2_scheme)): ) try: this_packtype = PackType.get_by_id(packtype_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No packtype found with id {packtype_id}') count = this_packtype.delete_instance() diff --git a/app/routers_v2/paperdex.py b/app/routers_v2/paperdex.py index 6328906..41e309d 100644 --- a/app/routers_v2/paperdex.py +++ b/app/routers_v2/paperdex.py @@ -5,7 +5,7 @@ import logging import pydantic from pandas import DataFrame -from ..db_engine import Paperdex, model_to_dict, Player, Cardset, Team +from ..db_engine import Paperdex, model_to_dict, Player, Cardset, Team, DoesNotExist from ..dependencies import oauth2_scheme, valid_token @@ -75,7 +75,7 @@ async def get_paperdex( async def get_one_paperdex(paperdex_id, csv: Optional[bool] = False): try: this_dex = Paperdex.get_by_id(paperdex_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No paperdex found with id {paperdex_id}') if csv: @@ -135,7 +135,7 @@ async def patch_paperdex( ) try: this_dex = Paperdex.get_by_id(paperdex_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No paperdex found with id {paperdex_id}') if team_id is not None: @@ -165,7 +165,7 @@ async def delete_paperdex(paperdex_id, token: str = Depends(oauth2_scheme)): ) try: this_dex = Paperdex.get_by_id(paperdex_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No paperdex found with id {paperdex_id}') count = this_dex.delete_instance() diff --git a/app/routers_v2/pitstats.py b/app/routers_v2/pitstats.py index c5f812c..82f3883 100644 --- a/app/routers_v2/pitstats.py +++ b/app/routers_v2/pitstats.py @@ -5,7 +5,7 @@ import logging import pydantic from pandas import DataFrame -from ..db_engine import db, PitchingStat, model_to_dict, Card, Player, Current +from ..db_engine import db, PitchingStat, model_to_dict, Card, Player, Current, DoesNotExist from ..dependencies import oauth2_scheme, valid_token @@ -170,7 +170,7 @@ async def delete_pitstat(stat_id, token: str = Depends(oauth2_scheme)): ) try: this_stat = PitchingStat.get_by_id(stat_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No stat found with id {stat_id}') count = this_stat.delete_instance() diff --git a/app/routers_v2/players.py b/app/routers_v2/players.py index 5313bae..dd842f8 100644 --- a/app/routers_v2/players.py +++ b/app/routers_v2/players.py @@ -12,7 +12,7 @@ from pandas import DataFrame from playwright.async_api import async_playwright from ..card_creation import get_batter_card_data, get_pitcher_card_data -from ..db_engine import ( +from ..db_engine import (, DoesNotExist db, Player, model_to_dict, @@ -26,6 +26,7 @@ from ..db_engine import ( PitchingCardRatings, CardPosition, MlbPlayer, + DoesNotExist, ) from ..db_helpers import upsert_players from ..dependencies import oauth2_scheme, valid_token @@ -585,7 +586,7 @@ async def search_players( async def get_one_player(player_id: int, csv: Optional[bool] = False): try: this_player = Player.get_by_id(player_id) - except Exception: + except DoesNotExist: raise HTTPException( status_code=404, detail=f"No player found with id {player_id}" ) @@ -668,7 +669,7 @@ async def get_batter_card( ): try: this_player = Player.get_by_id(player_id) - except Exception: + except DoesNotExist: raise HTTPException( status_code=404, detail=f"No player found with id {player_id}" ) @@ -868,7 +869,7 @@ async def v1_players_patch( try: this_player = Player.get_by_id(player_id) - except Exception: + except DoesNotExist: raise HTTPException( status_code=404, detail=f"No player found with id {player_id}" ) @@ -891,7 +892,7 @@ async def v1_players_patch( if cardset_id is not None: try: this_cardset = Cardset.get_by_id(cardset_id) - except Exception: + except DoesNotExist: raise HTTPException( status_code=404, detail=f"No cardset found with id {cardset_id}" ) @@ -899,7 +900,7 @@ async def v1_players_patch( if rarity_id is not None: try: this_rarity = Rarity.get_by_id(rarity_id) - except Exception: + except DoesNotExist: raise HTTPException( status_code=404, detail=f"No rarity found with id {rarity_id}" ) @@ -1124,7 +1125,7 @@ async def delete_player(player_id: int, token: str = Depends(oauth2_scheme)): try: this_player = Player.get_by_id(player_id) - except Exception: + except DoesNotExist: raise HTTPException( status_code=404, detail=f"No player found with id {player_id}" ) diff --git a/app/routers_v2/rarity.py b/app/routers_v2/rarity.py index 057daa8..71f490d 100644 --- a/app/routers_v2/rarity.py +++ b/app/routers_v2/rarity.py @@ -4,7 +4,7 @@ import logging import pydantic from pandas import DataFrame -from ..db_engine import Rarity, model_to_dict, fn +from ..db_engine import Rarity, model_to_dict, fn, DoesNotExist from ..dependencies import oauth2_scheme, valid_token @@ -64,7 +64,7 @@ async def get_rarities(value: Optional[int] = None, name: Optional[str] = None, async def get_one_rarity(rarity_id, csv: Optional[bool] = False): try: this_rarity = Rarity.get_by_id(rarity_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No rarity found with id {rarity_id}') if csv: @@ -125,7 +125,7 @@ async def patch_rarity( ) try: this_rarity = Rarity.get_by_id(rarity_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No rarity found with id {rarity_id}') if value is not None: @@ -155,7 +155,7 @@ async def v1_rarities_delete(rarity_id, token: str = Depends(oauth2_scheme)): ) try: this_rarity = Rarity.get_by_id(rarity_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No rarity found with id {rarity_id}') count = this_rarity.delete_instance() diff --git a/app/routers_v2/results.py b/app/routers_v2/results.py index 173d145..cabfcc5 100644 --- a/app/routers_v2/results.py +++ b/app/routers_v2/results.py @@ -4,7 +4,7 @@ import logging import pydantic from pandas import DataFrame -from ..db_engine import Result, model_to_dict, Team, DataError +from ..db_engine import Result, model_to_dict, Team, DataError, DoesNotExist from ..dependencies import oauth2_scheme, valid_token @@ -50,28 +50,28 @@ async def get_results( try: this_team = Team.get_by_id(away_team_id) all_results = all_results.where(Result.away_team == this_team) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No team found with id {away_team_id}') if home_team_id is not None: try: this_team = Team.get_by_id(home_team_id) all_results = all_results.where(Result.home_team == this_team) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No team found with id {home_team_id}') if team_one_id is not None: try: this_team = Team.get_by_id(team_one_id) all_results = all_results.where((Result.home_team == this_team) | (Result.away_team == this_team)) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No team found with id {team_one_id}') if team_two_id is not None: try: this_team = Team.get_by_id(team_two_id) all_results = all_results.where((Result.home_team == this_team) | (Result.away_team == this_team)) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No team found with id {team_two_id}') if away_score_min is not None: @@ -158,7 +158,7 @@ async def get_results( async def get_one_results(result_id, csv: Optional[bool] = None): try: this_result = Result.get_by_id(result_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No result found with id {result_id}') if csv: @@ -185,7 +185,7 @@ async def get_team_results( all_results = Result.select().where((Result.away_team_id == team_id) | (Result.home_team_id == team_id)).order_by(Result.id) try: this_team = Team.get_by_id(team_id) - except Exception as e: + except DoesNotExist as e: logging.error(f'Unknown team id {team_id} trying to pull team results') raise HTTPException(404, f'Team id {team_id} not found') @@ -332,7 +332,7 @@ async def patch_result( ) try: this_result = Result.get_by_id(result_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No result found with id {result_id}') if away_team_id is not None: @@ -391,7 +391,7 @@ async def delete_result(result_id, token: str = Depends(oauth2_scheme)): ) try: this_result = Result.get_by_id(result_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No result found with id {result_id}') count = this_result.delete_instance() diff --git a/app/routers_v2/rewards.py b/app/routers_v2/rewards.py index 6886e99..ab6aa43 100644 --- a/app/routers_v2/rewards.py +++ b/app/routers_v2/rewards.py @@ -5,7 +5,7 @@ import logging import pydantic from pandas import DataFrame -from ..db_engine import Reward, model_to_dict, fn +from ..db_engine import Reward, model_to_dict, fn, DoesNotExist from ..dependencies import oauth2_scheme, valid_token @@ -75,7 +75,7 @@ async def get_rewards( async def get_one_reward(reward_id, csv: Optional[bool] = False): try: this_reward = Reward.get_by_id(reward_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No reward found with id {reward_id}') if csv: @@ -130,7 +130,7 @@ async def patch_reward( ) try: this_reward = Reward.get_by_id(reward_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No reward found with id {reward_id}') if name is not None: @@ -161,7 +161,7 @@ async def delete_reward(reward_id, token: str = Depends(oauth2_scheme)): ) try: this_reward = Reward.get_by_id(reward_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f'No reward found with id {reward_id}') count = this_reward.delete_instance() diff --git a/app/routers_v2/teams.py b/app/routers_v2/teams.py index a2b31d8..d5b11b2 100644 --- a/app/routers_v2/teams.py +++ b/app/routers_v2/teams.py @@ -8,7 +8,7 @@ import logging import pydantic from pandas import DataFrame -from ..db_engine import ( +from ..db_engine import (, DoesNotExist db, Team, model_to_dict, @@ -31,6 +31,7 @@ from ..db_engine import ( PitchingCardRatings, StratGame, LIVE_PROMO_CARDSET_ID, + DoesNotExist, ) from ..dependencies import ( oauth2_scheme, @@ -170,7 +171,7 @@ async def get_teams( async def get_one_team(team_id: int, inc_packs: bool = True, csv: Optional[bool] = False): try: this_team = Team.get_by_id(team_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f"No team found with id {team_id}") p_query = Pack.select().where( @@ -1017,7 +1018,7 @@ async def get_team_record(team_id: int, season: int): async def team_buy_players(team_id: int, ids: str, ts: str): try: this_team = Team.get_by_id(team_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f"No team found with id {team_id}") if ts != this_team.team_hash(): @@ -1037,7 +1038,7 @@ async def team_buy_players(team_id: int, ids: str, ts: str): if player_id != "": try: this_player = Player.get_by_id(player_id) - except Exception: + except DoesNotExist: raise HTTPException( status_code=404, detail=f"No player found with id {player_id} /// " @@ -1105,14 +1106,14 @@ async def team_buy_packs( ): try: this_packtype = PackType.get_by_id(packtype_id) - except Exception: + except DoesNotExist: raise HTTPException( status_code=404, detail=f"No pack type found with id {packtype_id}" ) try: this_team = Team.get_by_id(team_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f"No team found with id {team_id}") if ts != this_team.team_hash(): @@ -1168,7 +1169,7 @@ async def team_buy_packs( async def team_sell_cards(team_id: int, ids: str, ts: str): try: this_team = Team.get_by_id(team_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f"No team found with id {team_id}") if ts != this_team.team_hash(): @@ -1186,7 +1187,7 @@ async def team_sell_cards(team_id: int, ids: str, ts: str): if card_id != "": try: this_card = Card.get_by_id(card_id) - except Exception: + except DoesNotExist: raise HTTPException( status_code=404, detail=f"No card found with id {card_id}" ) @@ -1254,7 +1255,7 @@ async def get_team_cards(team_id, csv: Optional[bool] = True): """ try: this_team = Team.get_by_id(team_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f"No team found with id {team_id}") if not csv: @@ -1394,7 +1395,7 @@ async def team_update_money( try: this_team = Team.get_by_id(team_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f"No team found with id {team_id}") this_team.wallet += delta @@ -1438,7 +1439,7 @@ async def patch_team( ) try: this_team = Team.get_by_id(team_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f"No team found with id {team_id}") if abbrev is not None: @@ -1500,7 +1501,7 @@ async def delete_team(team_id, token: str = Depends(oauth2_scheme)): ) try: this_team = Team.get_by_id(team_id) - except Exception: + except DoesNotExist: raise HTTPException(status_code=404, detail=f"No team found with id {team_id}") count = this_team.delete_instance()