feat: add find_or_fail method to Model and QueryBuilder - #59
Merged
Conversation
Raises ModelNotFoundException (404) when no record matches the given primary key, mirroring the Laravel/Masonite find_or_fail convention. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Covers: returns model when found, raises ModelNotFoundException when not found, exception has 404 status, and exception message contains the key. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
tmgbedu
force-pushed
the
feat/find-or-fail
branch
from
May 21, 2026 22:36
f90abd4 to
1f40e4a
Compare
These belong in their own dedicated PR. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Add fastapi_startkit/masoniteorm/exceptions.py as the canonical home - Export ModelNotFoundException from fastapi_startkit.masoniteorm - Keep a re-export in fastapi_startkit.exceptions for backwards compatibility Co-Authored-By: Claude Sonnet 4.6 <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.
Summary
find_or_fail(primary_key, columns=None)toQueryBuilder— raisesModelNotFoundException(HTTP 404) when no record matches the given primary keyfind_or_failclassmethod toModelthat delegates to the builderModelNotFoundExceptionto accept an optional message in__init__, so the error message includes the model name and key valueUsage
Test plan
await SomeModel.find_or_fail(existing_id)— should return the model instanceawait SomeModel.find_or_fail(nonexistent_id)— should raiseModelNotFoundExceptionwith a 404 status🤖 Generated with Claude Code