Skip to content
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

Merged
merged 14 commits into from
Feb 20, 2024

Conversation

benfurfie
Copy link
Contributor

@benfurfie benfurfie commented Feb 11, 2024

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)

use Statamic\Facades\Entry;

...

$id = 'home';
Entry::findOrFail($id);

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:

use Statamic\Facades\Entry;

...

$id = 'not-home';
Entry::findOrFail($id);

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. the id not found to the developer assuming they have debug mode enabled).

Testing

I have written tests for the following cases:

  • Expect an exception to be thrown when a developer attempts to call an entry that does not exist when the id is a string.
  • Expect an exception to be thrown when a developer attempts to call an entry that does not exist when the id is a int.
  • That the entry is returned and no errors are thrown when an entry exists and the id is a string.
  • That the entry is returned and no errors are thrown when an entry exists and the 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.

- 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.
@benfurfie benfurfie changed the title Feature/find or fail Implement find or fail on EntryRepository Feb 11, 2024
@benfurfie benfurfie changed the title Implement find or fail on EntryRepository [4.x] Implement find or fail on EntryRepository Feb 11, 2024
src/Contracts/Entries/EntryRepository.php Outdated Show resolved Hide resolved
src/Stache/Repositories/EntryRepository.php Outdated Show resolved Hide resolved
src/Stache/Repositories/EntryRepository.php Outdated Show resolved Hide resolved
src/Exceptions/EntryNotFoundException.php Outdated Show resolved Hide resolved
benfurfie and others added 6 commits February 20, 2024 08:01
* Moves the `findOrFail` tests next to the tests for the other `find` method.
* Simplifies the test to just test against a single ID.
@duncanmcclean duncanmcclean changed the base branch from 4.x to master February 20, 2024 17:15
@duncanmcclean duncanmcclean changed the title [4.x] Implement find or fail on EntryRepository [5.x] Implement find or fail on EntryRepository Feb 20, 2024
Copy link
Member

@duncanmcclean duncanmcclean left a 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.

@jasonvarga jasonvarga changed the base branch from master to 4.x February 20, 2024 21:42
@jasonvarga jasonvarga changed the title [5.x] Implement find or fail on EntryRepository [4.x] Implement find or fail on EntryRepository Feb 20, 2024
@jasonvarga
Copy link
Member

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.

@jasonvarga jasonvarga merged commit 1f31074 into statamic:4.x Feb 20, 2024
18 checks passed
duncanmcclean added a commit to statamic/docs that referenced this pull request Feb 27, 2024
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.

Add findOrFail to repositories
5 participants