refactor: collapse not-found handling into one seam#27
Merged
Conversation
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>
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.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 (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)
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.Verification
just test→ 19 passed, 100% coverageruff format/ruff check/ty checkclean🤖 Generated with Claude Code