refactor: collapse not-found handling into one seam#52
Merged
Conversation
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)
Consolidate that policy into one seam: a NotFoundError → 404 handler
registered in build_app, mirroring the existing DuplicateKeyError
handler. The not-found decision now lives where the data access happens
(the repository's raising get_one), mapped to a response in one place.
- app/exceptions.py — new not_found_error_handler returning
404 {"detail": "Not found"}.
- app/application.py — register NotFoundError handler.
- app/repositories.py — fetch_with_cards tightens to -> models.Deck,
using get_one (raises on miss) instead of get_one_or_none.
- app/api/decks.py — get_deck / update_deck / get_card drop all 404
code; get_card uses get_one. Removed now-unused imports
(NotFoundError, starlette status, HTTPException usage).
404 body is generic {"detail": "Not found"} — advanced-alchemy's
NotFoundError.detail is empty and no test asserts the message.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Three handlers each re-derived the not-found → 404 policy, via two different mechanisms:
get_deck/get_card:get_one_or_none(...)thenif not instance: raise HTTPException(404, ...)update_deck:try: ... except NotFoundError: raise HTTPException(404, ...)This consolidates that policy into one seam: a single
NotFoundError → 404handler registered inbuild_app, mirroring the existingDuplicateKeyErrorhandler.Ports modern-python/litestar-sqlalchemy-template#27 to this template, adapted to FastAPI idioms (
JSONResponsehandler,add_exception_handler,typing.castresponses preserved).Changes
app/exceptions.py— newnot_found_error_handlerreturning404 {"detail": "Not found"}.app/application.py— registerNotFoundError: exceptions.not_found_error_handler.app/repositories.py—fetch_with_cardstightens to-> models.Deck, usingget_one(raises on miss) instead ofget_one_or_none.app/api/decks.py—get_deck/update_deck/get_carddrop all 404 code;get_cardusesget_one. Removed now-unused imports (NotFoundError, starlettestatus,HTTPExceptionusage).No handler mentions 404 any more — the not-found decision lives where the data access happens, mapped to a response in one place.
Decisions
NotFoundError(the signal the repository already emits; matches theDuplicateKeyErrorconvention).fetch_with_cards.{"detail": "Not found"}— advanced-alchemy'sNotFoundError.detailis empty and no test asserts the message (the existing 404 tests assert status code only).Verification
just test→ 19 passed, 100% coverageruff format --check/ruff check --no-fix/ty checkclean🤖 Generated with Claude Code