From b31c405b7178f61f0839e1e7a5b5df761050aa67 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 | 13 +++++++------ app/routers_v2/rarity.py | 8 ++++---- app/routers_v2/results.py | 18 +++++++++--------- app/routers_v2/rewards.py | 8 ++++---- app/routers_v2/teams.py | 24 ++++++++++++------------ 19 files changed, 93 insertions(+), 92 deletions(-) diff --git a/app/routers_v2/awards.py b/app/routers_v2/awards.py index 6914bfe..d749d8d 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, LOG_DATA, PRIVATE_IN_SCHEMA logging.basicConfig( @@ -78,7 +78,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: @@ -135,7 +135,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 909c9bf..44ea00a 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, LOG_DATA, PRIVATE_IN_SCHEMA logging.basicConfig( @@ -241,7 +241,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 ca294ff..6dbbc79 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, LOG_DATA logging.basicConfig( @@ -46,19 +46,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: @@ -130,7 +130,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: @@ -273,7 +273,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') @@ -294,7 +294,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: @@ -337,7 +337,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 8052264..67951eb 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, LOG_DATA logging.basicConfig( @@ -46,7 +46,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: @@ -109,7 +109,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') @@ -155,7 +155,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: @@ -210,7 +210,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: @@ -246,7 +246,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 9a68029..581d115 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, LOG_DATA, PRIVATE_IN_SCHEMA logging.basicConfig( @@ -50,7 +50,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: @@ -107,7 +107,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: @@ -141,7 +141,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 6908170..9a114eb 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, LOG_DATA logging.basicConfig( @@ -68,7 +68,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: @@ -132,7 +132,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: @@ -168,7 +168,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 c99377c..84c5d9e 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, LOG_DATA logging.basicConfig( @@ -68,7 +68,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: @@ -125,7 +125,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: @@ -166,7 +166,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 0540c87..f40f384 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, LOG_DATA @@ -62,7 +62,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 8ceac15..e6c3d16 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, LOG_DATA logging.basicConfig( @@ -89,7 +89,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 7357b99..3b0c7ea 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, LOG_DATA logging.basicConfig( @@ -79,7 +79,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: @@ -140,7 +140,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: @@ -178,7 +178,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 ba4ada0..630079d 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, LOG_DATA logging.basicConfig( @@ -46,20 +46,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: @@ -68,7 +68,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: @@ -112,7 +112,7 @@ async def get_packs( async def get_one_pack(pack_id, 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: @@ -196,7 +196,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: @@ -239,7 +239,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 fb08c3a..cbc9c16 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, LOG_DATA logging.basicConfig( @@ -73,7 +73,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: @@ -134,7 +134,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: @@ -168,7 +168,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 08264e9..93a76f9 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, LOG_DATA logging.basicConfig( @@ -80,7 +80,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: @@ -140,7 +140,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: @@ -170,7 +170,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 5219ca0..624bda4 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, LOG_DATA logging.basicConfig( @@ -175,7 +175,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 4c38e73..a6e9295 100644 --- a/app/routers_v2/players.py +++ b/app/routers_v2/players.py @@ -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, LOG_DATA @@ -590,7 +591,7 @@ async def search_players( async def get_one_player(player_id, 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}" ) @@ -673,7 +674,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}" ) @@ -873,7 +874,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}" ) @@ -896,7 +897,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}" ) @@ -904,7 +905,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}" ) @@ -1129,7 +1130,7 @@ async def delete_player(player_id, 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 e37156e..b03eadc 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, LOG_DATA logging.basicConfig( @@ -69,7 +69,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: @@ -130,7 +130,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: @@ -160,7 +160,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 c706a41..b884ce6 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, LOG_DATA logging.basicConfig( @@ -55,28 +55,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: @@ -163,7 +163,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: @@ -190,7 +190,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') @@ -337,7 +337,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: @@ -396,7 +396,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 86f46ff..c45df3c 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, LOG_DATA logging.basicConfig( @@ -80,7 +80,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: @@ -135,7 +135,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: @@ -166,7 +166,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 c6b27ca..7429f1d 100644 --- a/app/routers_v2/teams.py +++ b/app/routers_v2/teams.py @@ -31,6 +31,7 @@ from ..db_engine import ( PitchingCardRatings, StratGame, LIVE_PROMO_CARDSET_ID, + DoesNotExist, ) from ..dependencies import ( oauth2_scheme, @@ -179,7 +180,7 @@ async def get_teams( async def get_one_team(team_id, 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( @@ -282,7 +283,6 @@ def get_scouting_dfs(allowed_players, position: str): ) ) - def get_total_ops(df_data): ops_vl = df_data["obp_vl"] + df_data["slg_vl"] ops_vr = df_data["obp_vr"] + df_data["slg_vr"] @@ -1017,7 +1017,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 +1037,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 +1105,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 +1168,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 +1186,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 +1254,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 +1394,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 +1438,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 +1500,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()