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 <noreply@anthropic.com>
This commit is contained in:
Cal Corum 2026-03-03 19:05:16 -06:00
parent 8227b57875
commit 0c042165b7
19 changed files with 95 additions and 93 deletions

View File

@ -4,7 +4,7 @@ import logging
import pydantic import pydantic
from pandas import DataFrame 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 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): async def get_one_award(award_id, csv: Optional[bool] = None):
try: try:
this_award = Award.get_by_id(award_id) 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}') raise HTTPException(status_code=404, detail=f'No award found with id {award_id}')
if csv: if csv:
@ -130,7 +130,7 @@ async def delete_award(award_id, token: str = Depends(oauth2_scheme)):
) )
try: try:
this_award = Award.get_by_id(award_id) 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}') raise HTTPException(status_code=404, detail=f'No award found with id {award_id}')
count = this_award.delete_instance() count = this_award.delete_instance()

View File

@ -6,7 +6,7 @@ import logging
import pydantic import pydantic
from pandas import DataFrame 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 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: try:
this_stat = BattingStat.get_by_id(stat_id) 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}') raise HTTPException(status_code=404, detail=f'No stat found with id {stat_id}')
count = this_stat.delete_instance() count = this_stat.delete_instance()

View File

@ -4,7 +4,7 @@ import logging
import pydantic import pydantic
from pandas import DataFrame 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 from ..dependencies import oauth2_scheme, valid_token
@ -41,19 +41,19 @@ async def get_cards(
if team_id is not None: if team_id is not None:
try: try:
this_team = Team.get_by_id(team_id) 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}') raise HTTPException(status_code=404, detail=f'No team found with id {team_id}')
all_cards = all_cards.where(Card.team == this_team) all_cards = all_cards.where(Card.team == this_team)
if player_id is not None: if player_id is not None:
try: try:
this_player = Player.get_by_id(player_id) 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}') raise HTTPException(status_code=404, detail=f'No player found with id {player_id}')
all_cards = all_cards.where(Card.player == this_player) all_cards = all_cards.where(Card.player == this_player)
if pack_id is not None: if pack_id is not None:
try: try:
this_pack = Pack.get_by_id(pack_id) 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}') raise HTTPException(status_code=404, detail=f'No pack found with id {pack_id}')
all_cards = all_cards.where(Card.pack == this_pack) all_cards = all_cards.where(Card.pack == this_pack)
if value is not None: 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): async def v1_cards_get_one(card_id, csv: Optional[bool] = False):
try: try:
this_card = Card.get_by_id(card_id) 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}') raise HTTPException(status_code=404, detail=f'No card found with id {card_id}')
if csv: if csv:
@ -268,7 +268,7 @@ async def v1_cards_wipe_team(team_id: int, token: str = Depends(oauth2_scheme)):
try: try:
this_team = Team.get_by_id(team_id) 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') logging.error(f'/cards/wipe-team/{team_id} - could not find team')
raise HTTPException(status_code=404, detail=f'Team {team_id} not found') raise HTTPException(status_code=404, detail=f'Team {team_id} not found')
@ -289,7 +289,7 @@ async def v1_cards_patch(
) )
try: try:
this_card = Card.get_by_id(card_id) 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}') raise HTTPException(status_code=404, detail=f'No card found with id {card_id}')
if player_id is not None: if player_id is not None:
@ -332,7 +332,7 @@ async def v1_cards_delete(card_id, token: str = Depends(oauth2_scheme)):
) )
try: try:
this_card = Card.get_by_id(card_id) 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}') raise HTTPException(status_code=404, detail=f'No cards found with id {card_id}')
count = this_card.delete_instance() count = this_card.delete_instance()

View File

@ -4,7 +4,7 @@ import logging
import pydantic import pydantic
from pandas import DataFrame 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 from ..dependencies import oauth2_scheme, valid_token
@ -41,7 +41,7 @@ async def get_cardsets(
try: try:
this_event = Event.get_by_id(event_id) this_event = Event.get_by_id(event_id)
all_cardsets = all_cardsets.where(Cardset.event == this_event) 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}') logging.error(f'Failed to find event {event_id}: {e}')
raise HTTPException(status_code=404, detail=f'Event id {event_id} not found') raise HTTPException(status_code=404, detail=f'Event id {event_id} not found')
if in_packs is not None: if in_packs is not None:
@ -104,7 +104,7 @@ async def search_cardsets(
try: try:
this_event = Event.get_by_id(event_id) this_event = Event.get_by_id(event_id)
all_cardsets = all_cardsets.where(Cardset.event == this_event) 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}') logging.error(f'Failed to find event {event_id}: {e}')
raise HTTPException(status_code=404, detail=f'Event id {event_id} not found') 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): async def get_one_cardset(cardset_id, csv: Optional[bool] = False):
try: try:
this_cardset = Cardset.get_by_id(cardset_id) 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}') raise HTTPException(status_code=404, detail=f'No cardset found with id {cardset_id}')
if csv: if csv:
@ -205,7 +205,7 @@ async def patch_cardsets(
) )
try: try:
this_cardset = Cardset.get_by_id(cardset_id) 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}') raise HTTPException(status_code=404, detail=f'No cardset found with id {cardset_id}')
if name is not None: if name is not None:
@ -241,7 +241,7 @@ async def delete_cardsets(cardset_id, token: str = Depends(oauth2_scheme)):
) )
try: try:
this_cardset = Cardset.get_by_id(cardset_id) 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}') raise HTTPException(status_code=404, detail=f'No cardset found with id {cardset_id}')
count = this_cardset.delete_instance() count = this_cardset.delete_instance()

View File

@ -4,7 +4,7 @@ from typing import Optional
import logging import logging
import pydantic 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 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): async def get_one_current(current_id, csv: Optional[bool] = False):
try: try:
current = Current.get_by_id(current_id) 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}') raise HTTPException(status_code=404, detail=f'No current found with id {current_id}')
if csv: if csv:
@ -102,7 +102,7 @@ async def patch_current(
) )
try: try:
current = Current.get_by_id(current_id) 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}') raise HTTPException(status_code=404, detail=f'No current found with id {current_id}')
if season is not None: if season is not None:
@ -136,7 +136,7 @@ async def delete_current(current_id, token: str = Depends(oauth2_scheme)):
) )
try: try:
this_curr = Current.get_by_id(current_id) 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}') raise HTTPException(status_code=404, detail=f'No current found with id {current_id}')
count = this_curr.delete_instance() count = this_curr.delete_instance()

View File

@ -4,7 +4,7 @@ import logging
import pydantic import pydantic
from pandas import DataFrame 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 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): async def v1_events_get_one(event_id, csv: Optional[bool] = False):
try: try:
this_event = Event.get_by_id(event_id) 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}') raise HTTPException(status_code=404, detail=f'No event found with id {event_id}')
if csv: if csv:
@ -127,7 +127,7 @@ async def v1_events_patch(
) )
try: try:
this_event = Event.get_by_id(event_id) 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}') raise HTTPException(status_code=404, detail=f'No event found with id {event_id}')
if name is not None: if name is not None:
@ -163,7 +163,7 @@ async def v1_events_delete(event_id, token: str = Depends(oauth2_scheme)):
) )
try: try:
this_event = Event.get_by_id(event_id) 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}') raise HTTPException(status_code=404, detail=f'No event found with id {event_id}')
count = this_event.delete_instance() count = this_event.delete_instance()

View File

@ -4,7 +4,7 @@ import logging
import pydantic import pydantic
from pandas import DataFrame 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 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): async def v1_gamerewards_get_one(gamereward_id, csv: Optional[bool] = None):
try: try:
this_game_reward = GameRewards.get_by_id(gamereward_id) 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}') raise HTTPException(status_code=404, detail=f'No game reward found with id {gamereward_id}')
if csv: if csv:
@ -120,7 +120,7 @@ async def v1_gamerewards_patch(
) )
try: try:
this_game_reward = GameRewards.get_by_id(game_reward_id) 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}') raise HTTPException(status_code=404, detail=f'No game reward found with id {game_reward_id}')
if name is not None: if name is not None:
@ -161,7 +161,7 @@ async def v1_gamerewards_delete(gamereward_id, token: str = Depends(oauth2_schem
) )
try: try:
this_award = GameRewards.get_by_id(gamereward_id) 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}') raise HTTPException(status_code=404, detail=f'No award found with id {gamereward_id}')
count = this_award.delete_instance() count = this_award.delete_instance()

View File

@ -3,7 +3,7 @@ from typing import Optional, List
import logging import logging
import pydantic 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 ..db_helpers import upsert_gauntlet_rewards
from ..dependencies import oauth2_scheme, valid_token from ..dependencies import oauth2_scheme, valid_token
@ -57,7 +57,7 @@ async def v1_gauntletreward_get(
async def v1_gauntletreward_get_one(gauntletreward_id): async def v1_gauntletreward_get_one(gauntletreward_id):
try: try:
this_reward = GauntletReward.get_by_id(gauntletreward_id) this_reward = GauntletReward.get_by_id(gauntletreward_id)
except Exception: except DoesNotExist:
raise HTTPException( raise HTTPException(
status_code=404, status_code=404,
detail=f"No gauntlet reward found with id {gauntletreward_id}", detail=f"No gauntlet reward found with id {gauntletreward_id}",

View File

@ -4,7 +4,7 @@ from typing import Optional
import logging import logging
import pydantic 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 from ..dependencies import oauth2_scheme, valid_token
@ -84,7 +84,7 @@ async def get_gauntletruns(
async def get_one_gauntletrun(gauntletrun_id): async def get_one_gauntletrun(gauntletrun_id):
try: try:
this_gauntlet = GauntletRun.get_by_id(gauntletrun_id) 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}') raise HTTPException(status_code=404, detail=f'No gauntlet found with id {gauntletrun_id}')
return_val = model_to_dict(this_gauntlet) return_val = model_to_dict(this_gauntlet)

View File

@ -5,7 +5,7 @@ import logging
import pydantic import pydantic
from pandas import DataFrame 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 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): async def get_one_notif(notif_id, csv: Optional[bool] = None):
try: try:
this_notif = Notification.get_by_id(notif_id) 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}') raise HTTPException(status_code=404, detail=f'No notification found with id {notif_id}')
if csv: if csv:
@ -135,7 +135,7 @@ async def patch_notif(
) )
try: try:
this_notif = Notification.get_by_id(notif_id) 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}') raise HTTPException(status_code=404, detail=f'No notification found with id {notif_id}')
if title is not None: if title is not None:
@ -173,7 +173,7 @@ async def delete_notif(notif_id, token: str = Depends(oauth2_scheme)):
) )
try: try:
this_notif = Notification.get_by_id(notif_id) 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}') raise HTTPException(status_code=404, detail=f'No notification found with id {notif_id}')
count = this_notif.delete_instance() count = this_notif.delete_instance()

View File

@ -6,7 +6,7 @@ import logging
import pydantic import pydantic
from pandas import DataFrame 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 from ..dependencies import oauth2_scheme, valid_token
@ -41,20 +41,20 @@ async def get_packs(
if team_id is not None: if team_id is not None:
try: try:
this_team = Team.get_by_id(team_id) 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}') raise HTTPException(status_code=404, detail=f'No team found with id {team_id}')
all_packs = all_packs.where(Pack.team == this_team) all_packs = all_packs.where(Pack.team == this_team)
if pack_type_id is not None: if pack_type_id is not None:
try: try:
this_pack_type = PackType.get_by_id(pack_type_id) 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}') 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) all_packs = all_packs.where(Pack.pack_type == this_pack_type)
if pack_team_id is not None: if pack_team_id is not None:
try: try:
this_pack_team = Team.get_by_id(pack_team_id) 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}') 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) all_packs = all_packs.where(Pack.pack_team == this_pack_team)
elif exact_match: elif exact_match:
@ -63,7 +63,7 @@ async def get_packs(
if pack_cardset_id is not None: if pack_cardset_id is not None:
try: try:
this_pack_cardset = Cardset.get_by_id(pack_cardset_id) 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}') 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) all_packs = all_packs.where(Pack.pack_cardset == this_pack_cardset)
elif exact_match: elif exact_match:
@ -107,7 +107,7 @@ async def get_packs(
async def get_one_pack(pack_id: int, csv: Optional[bool] = False): async def get_one_pack(pack_id: int, csv: Optional[bool] = False):
try: try:
this_pack = Pack.get_by_id(pack_id) 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}') raise HTTPException(status_code=404, detail=f'No pack found with id {pack_id}')
if csv: if csv:
@ -191,7 +191,7 @@ async def patch_pack(
) )
try: try:
this_pack = Pack.get_by_id(pack_id) 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}') raise HTTPException(status_code=404, detail=f'No pack found with id {pack_id}')
if team_id is not None: if team_id is not None:
@ -234,7 +234,7 @@ async def delete_pack(pack_id, token: str = Depends(oauth2_scheme)):
) )
try: try:
this_pack = Pack.get_by_id(pack_id) 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}') raise HTTPException(status_code=404, detail=f'No packs found with id {pack_id}')
count = this_pack.delete_instance() count = this_pack.delete_instance()

View File

@ -4,7 +4,7 @@ import logging
import pydantic import pydantic
from pandas import DataFrame 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 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): async def get_one_packtype(packtype_id, csv: Optional[bool] = False):
try: try:
this_packtype = PackType.get_by_id(packtype_id) 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}') raise HTTPException(status_code=404, detail=f'No packtype found with id {packtype_id}')
if csv: if csv:
@ -129,7 +129,7 @@ async def patch_packtype(
) )
try: try:
this_packtype = PackType.get_by_id(packtype_id) 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}') raise HTTPException(status_code=404, detail=f'No packtype found with id {packtype_id}')
if name is not None: if name is not None:
@ -163,7 +163,7 @@ async def delete_packtype(packtype_id, token: str = Depends(oauth2_scheme)):
) )
try: try:
this_packtype = PackType.get_by_id(packtype_id) 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}') raise HTTPException(status_code=404, detail=f'No packtype found with id {packtype_id}')
count = this_packtype.delete_instance() count = this_packtype.delete_instance()

View File

@ -5,7 +5,7 @@ import logging
import pydantic import pydantic
from pandas import DataFrame 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 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): async def get_one_paperdex(paperdex_id, csv: Optional[bool] = False):
try: try:
this_dex = Paperdex.get_by_id(paperdex_id) 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}') raise HTTPException(status_code=404, detail=f'No paperdex found with id {paperdex_id}')
if csv: if csv:
@ -135,7 +135,7 @@ async def patch_paperdex(
) )
try: try:
this_dex = Paperdex.get_by_id(paperdex_id) 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}') raise HTTPException(status_code=404, detail=f'No paperdex found with id {paperdex_id}')
if team_id is not None: if team_id is not None:
@ -165,7 +165,7 @@ async def delete_paperdex(paperdex_id, token: str = Depends(oauth2_scheme)):
) )
try: try:
this_dex = Paperdex.get_by_id(paperdex_id) 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}') raise HTTPException(status_code=404, detail=f'No paperdex found with id {paperdex_id}')
count = this_dex.delete_instance() count = this_dex.delete_instance()

View File

@ -5,7 +5,7 @@ import logging
import pydantic import pydantic
from pandas import DataFrame 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 from ..dependencies import oauth2_scheme, valid_token
@ -170,7 +170,7 @@ async def delete_pitstat(stat_id, token: str = Depends(oauth2_scheme)):
) )
try: try:
this_stat = PitchingStat.get_by_id(stat_id) 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}') raise HTTPException(status_code=404, detail=f'No stat found with id {stat_id}')
count = this_stat.delete_instance() count = this_stat.delete_instance()

View File

@ -12,7 +12,7 @@ from pandas import DataFrame
from playwright.async_api import async_playwright from playwright.async_api import async_playwright
from ..card_creation import get_batter_card_data, get_pitcher_card_data from ..card_creation import get_batter_card_data, get_pitcher_card_data
from ..db_engine import ( from ..db_engine import (, DoesNotExist
db, db,
Player, Player,
model_to_dict, model_to_dict,
@ -26,6 +26,7 @@ from ..db_engine import (
PitchingCardRatings, PitchingCardRatings,
CardPosition, CardPosition,
MlbPlayer, MlbPlayer,
DoesNotExist,
) )
from ..db_helpers import upsert_players from ..db_helpers import upsert_players
from ..dependencies import oauth2_scheme, valid_token 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): async def get_one_player(player_id: int, csv: Optional[bool] = False):
try: try:
this_player = Player.get_by_id(player_id) this_player = Player.get_by_id(player_id)
except Exception: except DoesNotExist:
raise HTTPException( raise HTTPException(
status_code=404, detail=f"No player found with id {player_id}" status_code=404, detail=f"No player found with id {player_id}"
) )
@ -668,7 +669,7 @@ async def get_batter_card(
): ):
try: try:
this_player = Player.get_by_id(player_id) this_player = Player.get_by_id(player_id)
except Exception: except DoesNotExist:
raise HTTPException( raise HTTPException(
status_code=404, detail=f"No player found with id {player_id}" status_code=404, detail=f"No player found with id {player_id}"
) )
@ -868,7 +869,7 @@ async def v1_players_patch(
try: try:
this_player = Player.get_by_id(player_id) this_player = Player.get_by_id(player_id)
except Exception: except DoesNotExist:
raise HTTPException( raise HTTPException(
status_code=404, detail=f"No player found with id {player_id}" 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: if cardset_id is not None:
try: try:
this_cardset = Cardset.get_by_id(cardset_id) this_cardset = Cardset.get_by_id(cardset_id)
except Exception: except DoesNotExist:
raise HTTPException( raise HTTPException(
status_code=404, detail=f"No cardset found with id {cardset_id}" 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: if rarity_id is not None:
try: try:
this_rarity = Rarity.get_by_id(rarity_id) this_rarity = Rarity.get_by_id(rarity_id)
except Exception: except DoesNotExist:
raise HTTPException( raise HTTPException(
status_code=404, detail=f"No rarity found with id {rarity_id}" 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: try:
this_player = Player.get_by_id(player_id) this_player = Player.get_by_id(player_id)
except Exception: except DoesNotExist:
raise HTTPException( raise HTTPException(
status_code=404, detail=f"No player found with id {player_id}" status_code=404, detail=f"No player found with id {player_id}"
) )

View File

@ -4,7 +4,7 @@ import logging
import pydantic import pydantic
from pandas import DataFrame 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 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): async def get_one_rarity(rarity_id, csv: Optional[bool] = False):
try: try:
this_rarity = Rarity.get_by_id(rarity_id) 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}') raise HTTPException(status_code=404, detail=f'No rarity found with id {rarity_id}')
if csv: if csv:
@ -125,7 +125,7 @@ async def patch_rarity(
) )
try: try:
this_rarity = Rarity.get_by_id(rarity_id) 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}') raise HTTPException(status_code=404, detail=f'No rarity found with id {rarity_id}')
if value is not None: if value is not None:
@ -155,7 +155,7 @@ async def v1_rarities_delete(rarity_id, token: str = Depends(oauth2_scheme)):
) )
try: try:
this_rarity = Rarity.get_by_id(rarity_id) 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}') raise HTTPException(status_code=404, detail=f'No rarity found with id {rarity_id}')
count = this_rarity.delete_instance() count = this_rarity.delete_instance()

View File

@ -4,7 +4,7 @@ import logging
import pydantic import pydantic
from pandas import DataFrame 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 from ..dependencies import oauth2_scheme, valid_token
@ -50,28 +50,28 @@ async def get_results(
try: try:
this_team = Team.get_by_id(away_team_id) this_team = Team.get_by_id(away_team_id)
all_results = all_results.where(Result.away_team == this_team) 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}') raise HTTPException(status_code=404, detail=f'No team found with id {away_team_id}')
if home_team_id is not None: if home_team_id is not None:
try: try:
this_team = Team.get_by_id(home_team_id) this_team = Team.get_by_id(home_team_id)
all_results = all_results.where(Result.home_team == this_team) 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}') raise HTTPException(status_code=404, detail=f'No team found with id {home_team_id}')
if team_one_id is not None: if team_one_id is not None:
try: try:
this_team = Team.get_by_id(team_one_id) this_team = Team.get_by_id(team_one_id)
all_results = all_results.where((Result.home_team == this_team) | (Result.away_team == this_team)) 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}') raise HTTPException(status_code=404, detail=f'No team found with id {team_one_id}')
if team_two_id is not None: if team_two_id is not None:
try: try:
this_team = Team.get_by_id(team_two_id) this_team = Team.get_by_id(team_two_id)
all_results = all_results.where((Result.home_team == this_team) | (Result.away_team == this_team)) 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}') raise HTTPException(status_code=404, detail=f'No team found with id {team_two_id}')
if away_score_min is not None: 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): async def get_one_results(result_id, csv: Optional[bool] = None):
try: try:
this_result = Result.get_by_id(result_id) 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}') raise HTTPException(status_code=404, detail=f'No result found with id {result_id}')
if csv: 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) all_results = Result.select().where((Result.away_team_id == team_id) | (Result.home_team_id == team_id)).order_by(Result.id)
try: try:
this_team = Team.get_by_id(team_id) 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') logging.error(f'Unknown team id {team_id} trying to pull team results')
raise HTTPException(404, f'Team id {team_id} not found') raise HTTPException(404, f'Team id {team_id} not found')
@ -332,7 +332,7 @@ async def patch_result(
) )
try: try:
this_result = Result.get_by_id(result_id) 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}') raise HTTPException(status_code=404, detail=f'No result found with id {result_id}')
if away_team_id is not None: if away_team_id is not None:
@ -391,7 +391,7 @@ async def delete_result(result_id, token: str = Depends(oauth2_scheme)):
) )
try: try:
this_result = Result.get_by_id(result_id) 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}') raise HTTPException(status_code=404, detail=f'No result found with id {result_id}')
count = this_result.delete_instance() count = this_result.delete_instance()

View File

@ -5,7 +5,7 @@ import logging
import pydantic import pydantic
from pandas import DataFrame 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 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): async def get_one_reward(reward_id, csv: Optional[bool] = False):
try: try:
this_reward = Reward.get_by_id(reward_id) 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}') raise HTTPException(status_code=404, detail=f'No reward found with id {reward_id}')
if csv: if csv:
@ -130,7 +130,7 @@ async def patch_reward(
) )
try: try:
this_reward = Reward.get_by_id(reward_id) 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}') raise HTTPException(status_code=404, detail=f'No reward found with id {reward_id}')
if name is not None: if name is not None:
@ -161,7 +161,7 @@ async def delete_reward(reward_id, token: str = Depends(oauth2_scheme)):
) )
try: try:
this_reward = Reward.get_by_id(reward_id) 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}') raise HTTPException(status_code=404, detail=f'No reward found with id {reward_id}')
count = this_reward.delete_instance() count = this_reward.delete_instance()

View File

@ -8,7 +8,7 @@ import logging
import pydantic import pydantic
from pandas import DataFrame from pandas import DataFrame
from ..db_engine import ( from ..db_engine import (, DoesNotExist
db, db,
Team, Team,
model_to_dict, model_to_dict,
@ -31,6 +31,7 @@ from ..db_engine import (
PitchingCardRatings, PitchingCardRatings,
StratGame, StratGame,
LIVE_PROMO_CARDSET_ID, LIVE_PROMO_CARDSET_ID,
DoesNotExist,
) )
from ..dependencies import ( from ..dependencies import (
oauth2_scheme, 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): async def get_one_team(team_id: int, inc_packs: bool = True, csv: Optional[bool] = False):
try: try:
this_team = Team.get_by_id(team_id) 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}") raise HTTPException(status_code=404, detail=f"No team found with id {team_id}")
p_query = Pack.select().where( 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): async def team_buy_players(team_id: int, ids: str, ts: str):
try: try:
this_team = Team.get_by_id(team_id) 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}") raise HTTPException(status_code=404, detail=f"No team found with id {team_id}")
if ts != this_team.team_hash(): 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 != "": if player_id != "":
try: try:
this_player = Player.get_by_id(player_id) this_player = Player.get_by_id(player_id)
except Exception: except DoesNotExist:
raise HTTPException( raise HTTPException(
status_code=404, status_code=404,
detail=f"No player found with id {player_id} /// " detail=f"No player found with id {player_id} /// "
@ -1105,14 +1106,14 @@ async def team_buy_packs(
): ):
try: try:
this_packtype = PackType.get_by_id(packtype_id) this_packtype = PackType.get_by_id(packtype_id)
except Exception: except DoesNotExist:
raise HTTPException( raise HTTPException(
status_code=404, detail=f"No pack type found with id {packtype_id}" status_code=404, detail=f"No pack type found with id {packtype_id}"
) )
try: try:
this_team = Team.get_by_id(team_id) 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}") raise HTTPException(status_code=404, detail=f"No team found with id {team_id}")
if ts != this_team.team_hash(): 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): async def team_sell_cards(team_id: int, ids: str, ts: str):
try: try:
this_team = Team.get_by_id(team_id) 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}") raise HTTPException(status_code=404, detail=f"No team found with id {team_id}")
if ts != this_team.team_hash(): 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 != "": if card_id != "":
try: try:
this_card = Card.get_by_id(card_id) this_card = Card.get_by_id(card_id)
except Exception: except DoesNotExist:
raise HTTPException( raise HTTPException(
status_code=404, detail=f"No card found with id {card_id}" 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: try:
this_team = Team.get_by_id(team_id) 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}") raise HTTPException(status_code=404, detail=f"No team found with id {team_id}")
if not csv: if not csv:
@ -1394,7 +1395,7 @@ async def team_update_money(
try: try:
this_team = Team.get_by_id(team_id) 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}") raise HTTPException(status_code=404, detail=f"No team found with id {team_id}")
this_team.wallet += delta this_team.wallet += delta
@ -1438,7 +1439,7 @@ async def patch_team(
) )
try: try:
this_team = Team.get_by_id(team_id) 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}") raise HTTPException(status_code=404, detail=f"No team found with id {team_id}")
if abbrev is not None: if abbrev is not None:
@ -1500,7 +1501,7 @@ async def delete_team(team_id, token: str = Depends(oauth2_scheme)):
) )
try: try:
this_team = Team.get_by_id(team_id) 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}") raise HTTPException(status_code=404, detail=f"No team found with id {team_id}")
count = this_team.delete_instance() count = this_team.delete_instance()