-
-
Notifications
You must be signed in to change notification settings - Fork 530
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[4.x] Implement find or fail on EntryRepository #9506
Conversation
- Ensure that the EntryNotFoundException is thrown when no entry is found using findOrFail. - Ensure that an Entry is returned if one is found when findOrFail is called.
– Returns a string with the ID not found for debugging.
- When null is returned, throw EntryNotFoundException and pass ID.
* Moves the `findOrFail` tests next to the tests for the other `find` method. * Simplifies the test to just test against a single ID.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great - thanks!
I've made a few tweaks to the exception message and simplified one of the tests. I've also rebased this PR on master
since adding a method to the EntryRepository
is a breaking change for those implementing their own repositories.
f0976e9
to
e900e33
Compare
This is so small it might as well be in 4.x. I've just avoided adding it to the interface. We can do that for v5. |
Closes #1249. Related: statamic/cms#9506
Key deets
Targets: [4.x]
Closes statamic/ideas#1126
What?
This PR introduces a new method to the
EntryRepository
-findOrFail
. This method can be used to throw an error when an entry is not found, so that error monitoring tools such as Sentry, Flare, Rollbar etc can pick up on it and proactively alert developers.It also brings Statamic more into line with Laravel's conventions, making it easier for developers to onboard, and to integrate existing Laravel packages that require findOrFail methods.
How
Assuming an entry exists, (say 'home' as this exists with all new Statamic installs)
This will behave the same way as the
find
method does currently.However, let's say we attempt to call an entry that does not exist:
This will throw the new
EntryNotFoundException
. This exception handles the error and returns the id that was not found. This exception enables it to be picked up by error trackers, and also returns useful information to the developer (i.e. theid
not found to the developer assuming they have debug mode enabled).Testing
I have written tests for the following cases:
id
is a string.id
is a int.id
is a string.id
is an int.Anything else?
This PR also adds typehinting and return types to the method to assist with updating Statamic to run with Laravel 11 and PHP8.3.