Skip to content

refactor: collapse not-found handling into one seam#27

Merged
lesnik512 merged 1 commit into
mainfrom
collapse-not-found-seam
Jun 26, 2026
Merged

refactor: collapse not-found handling into one seam#27
lesnik512 merged 1 commit into
mainfrom
collapse-not-found-seam

Conversation

@lesnik512

Copy link
Copy Markdown
Member

What

Three handlers each re-derived the not-found → 404 policy, via two different mechanisms:

  • get_deck / get_card: get_one_or_none(...) then if not instance: raise HTTPException(404, ...)
  • update_deck: try: ... except NotFoundError: raise HTTPException(404, ...)

This consolidates that policy into one seam: a single NotFoundError → 404 handler registered in build_app, mirroring the existing DuplicateKeyError handler.

Changes

  • app/exceptions.py — new not_found_error_handler returning 404 {"detail": "Not found"}.
  • app/application.py — register NotFoundError: exceptions.not_found_error_handler.
  • app/repositories.pyfetch_with_cards tightens to -> models.Deck, using get_one (raises on miss) instead of get_one_or_none.
  • app/api/decks.pyget_deck / update_deck / get_card drop all 404 code; get_card uses get_one. Removed now-unused imports (HTTPException, status_codes, NotFoundError).

No handler mentions 404 any more — the not-found decision lives where the data access happens, mapped to a response in one place.

Decisions (from design grilling)

  • Seam = advanced-alchemy NotFoundError (the signal the repository already emits; matches the DuplicateKeyError convention).
  • Reads use the raising variant; the raise is pushed behind fetch_with_cards.
  • 404 body is generic {"detail": "Not found"} — advanced-alchemy's NotFoundError.detail is empty and no test asserts the message.

Verification

  • just test → 19 passed, 100% coverage
  • ruff format / ruff check / ty check clean
  • Net lines (deletes more than it adds)

🤖 Generated with Claude Code

Three handlers re-derived the not-found policy via two mechanisms: a
None-check after get_one_or_none, and an except NotFoundError around
update. The 404 knowledge was spread across the HTTP handlers.

Register a single NotFoundError -> 404 handler in build_app (mirroring
the existing DuplicateKeyError handler). Reads switch to advanced-alchemy's
raising variant (get_one), fetch_with_cards tightens to a non-optional
return, and update_deck drops its try/except. No handler mentions 404.

The 404 body becomes a generic {"detail": "Not found"}; status behaviour
is unchanged and the existing 404 tests stay green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@lesnik512 lesnik512 merged commit 79e130a into main Jun 26, 2026
2 checks passed
@lesnik512 lesnik512 deleted the collapse-not-found-seam branch June 26, 2026 09:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant