fix: replace broad except Exception blocks with DoesNotExist (#15) #48

Merged
cal merged 1 commits from ai/paper-dynasty-database#15 into next-release 2026-03-07 03:18:57 +00:00
Owner

Summary

  • Replaced 71 broad except Exception blocks across 19 router files with the specific peewee.DoesNotExist exception
  • Added DoesNotExist to the from ..db_engine import line in each affected file (available via from peewee import * in db_engine.py)
  • GET endpoints calling Model.get_by_id() now only catch the expected not-found case; real database failures propagate as 500s instead of being swallowed as 404s

Files Changed

app/routers_v2/: awards, batstats, cards, cardsets, current, events, gamerewards, gauntletrewards, gauntletruns, notifications, packs, packtypes, paperdex, pitstats, players, rarity, results, rewards, teams

Test Results

No test suite. All 19 files pass python3 -m py_compile syntax check. No remaining except Exception blocks in the routers_v2 directory.

Closes #15

## Summary - Replaced 71 broad `except Exception` blocks across 19 router files with the specific `peewee.DoesNotExist` exception - Added `DoesNotExist` to the `from ..db_engine import` line in each affected file (available via `from peewee import *` in `db_engine.py`) - GET endpoints calling `Model.get_by_id()` now only catch the expected not-found case; real database failures propagate as 500s instead of being swallowed as 404s ## Files Changed `app/routers_v2/`: awards, batstats, cards, cardsets, current, events, gamerewards, gauntletrewards, gauntletruns, notifications, packs, packtypes, paperdex, pitstats, players, rarity, results, rewards, teams ## Test Results No test suite. All 19 files pass `python3 -m py_compile` syntax check. No remaining `except Exception` blocks in the routers_v2 directory. Closes #15
cal added 1 commit 2026-03-04 01:05:37 +00:00
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>
cal added the
ai-reviewing
label 2026-03-04 01:16:59 +00:00
cal removed the
ai-reviewing
label 2026-03-05 03:41:42 +00:00
cal added the
ai-reviewing
label 2026-03-05 04:17:18 +00:00
cal reviewed 2026-03-05 04:18:35 +00:00
cal left a comment
Author
Owner

AI Code Review

Files Reviewed

  • app/routers_v2/awards.py (modified)
  • app/routers_v2/batstats.py (modified)
  • app/routers_v2/cards.py (modified)
  • app/routers_v2/cardsets.py (modified)
  • app/routers_v2/current.py (modified)
  • app/routers_v2/events.py (modified)
  • app/routers_v2/gamerewards.py (modified)
  • app/routers_v2/gauntletrewards.py (modified)
  • app/routers_v2/gauntletruns.py (modified)
  • app/routers_v2/notifications.py (modified)
  • app/routers_v2/packs.py (modified)
  • app/routers_v2/packtypes.py (modified)
  • app/routers_v2/paperdex.py (modified)
  • app/routers_v2/pitstats.py (modified)
  • app/routers_v2/players.py (modified)
  • app/routers_v2/rarity.py (modified)
  • app/routers_v2/results.py (modified)
  • app/routers_v2/rewards.py (modified)
  • app/routers_v2/teams.py (modified)

Findings

Correctness

  • peewee.DoesNotExist is the correct exception raised by Model.get_by_id() when no row matches. It is also the base class for all model-specific DoesNotExist exceptions (e.g. Team.DoesNotExist), so except DoesNotExist correctly catches lookup failures across all models.
  • db_engine.py uses from peewee import *, making DoesNotExist available in that module's namespace. Importing it via from ..db_engine import ..., DoesNotExist is valid and consistent with existing import patterns.
  • Every modified try block contains only a single Model.get_by_id() call — no other operations are inside the try, so no legitimate exceptions are inadvertently excluded by the narrowing.
  • Real database errors (connection failures, query errors) will now correctly propagate as 500s rather than being silently swallowed as 404s. This is the intended improvement.

Security

  • No issues. Exception narrowing has no security implications here.

Style & Conventions

  • The change pattern is applied consistently across all 19 files.
  • players.py and teams.py use multi-line import blocks; DoesNotExist is appended correctly in both cases.
  • One incidental cosmetic change in teams.py (removed a blank line before get_total_ops) — benign.

Suggestions

  • (Pre-existing, out of scope) Several catch blocks capture the exception as e (e.g. except DoesNotExist as e:) but don't include e in the log message. Consider logging.error(f'...: {e}') for richer diagnostics in a future pass.

Verdict: APPROVED

Clean, focused fix applied consistently across 19 files. The narrowing from Exception to DoesNotExist is technically correct, improves error transparency, and follows existing project import conventions. No issues found.


Automated review by Claude PR Reviewer

## AI Code Review ### Files Reviewed - `app/routers_v2/awards.py` (modified) - `app/routers_v2/batstats.py` (modified) - `app/routers_v2/cards.py` (modified) - `app/routers_v2/cardsets.py` (modified) - `app/routers_v2/current.py` (modified) - `app/routers_v2/events.py` (modified) - `app/routers_v2/gamerewards.py` (modified) - `app/routers_v2/gauntletrewards.py` (modified) - `app/routers_v2/gauntletruns.py` (modified) - `app/routers_v2/notifications.py` (modified) - `app/routers_v2/packs.py` (modified) - `app/routers_v2/packtypes.py` (modified) - `app/routers_v2/paperdex.py` (modified) - `app/routers_v2/pitstats.py` (modified) - `app/routers_v2/players.py` (modified) - `app/routers_v2/rarity.py` (modified) - `app/routers_v2/results.py` (modified) - `app/routers_v2/rewards.py` (modified) - `app/routers_v2/teams.py` (modified) ### Findings #### Correctness - `peewee.DoesNotExist` is the correct exception raised by `Model.get_by_id()` when no row matches. It is also the base class for all model-specific `DoesNotExist` exceptions (e.g. `Team.DoesNotExist`), so `except DoesNotExist` correctly catches lookup failures across all models. - `db_engine.py` uses `from peewee import *`, making `DoesNotExist` available in that module's namespace. Importing it via `from ..db_engine import ..., DoesNotExist` is valid and consistent with existing import patterns. - Every modified try block contains only a single `Model.get_by_id()` call — no other operations are inside the try, so no legitimate exceptions are inadvertently excluded by the narrowing. - Real database errors (connection failures, query errors) will now correctly propagate as 500s rather than being silently swallowed as 404s. This is the intended improvement. #### Security - No issues. Exception narrowing has no security implications here. #### Style & Conventions - The change pattern is applied consistently across all 19 files. - `players.py` and `teams.py` use multi-line import blocks; `DoesNotExist` is appended correctly in both cases. - One incidental cosmetic change in `teams.py` (removed a blank line before `get_total_ops`) — benign. #### Suggestions - (Pre-existing, out of scope) Several catch blocks capture the exception as `e` (e.g. `except DoesNotExist as e:`) but don't include `e` in the log message. Consider `logging.error(f'...: {e}')` for richer diagnostics in a future pass. ### Verdict: APPROVED Clean, focused fix applied consistently across 19 files. The narrowing from `Exception` to `DoesNotExist` is technically correct, improves error transparency, and follows existing project import conventions. No issues found. --- *Automated review by Claude PR Reviewer*
cal added the
ai-reviewed
label 2026-03-05 04:18:51 +00:00
cal force-pushed ai/paper-dynasty-database#15 from b31c405b71 to 0c042165b7 2026-03-05 21:33:01 +00:00 Compare
cal merged commit 3ddb7028f3 into next-release 2026-03-07 03:18:57 +00:00
cal deleted branch ai/paper-dynasty-database#15 2026-03-07 03:18:57 +00:00
Sign in to join this conversation.
No description provided.