Skip to content

[4.x] Implement find or fail on EntryRepository - #9506

Merged
jasonvarga merged 14 commits into
statamic:4.xfrom
benfurfie:feature/findOrFail
Feb 20, 2024
Merged

[4.x] Implement find or fail on EntryRepository#9506
jasonvarga merged 14 commits into
statamic:4.xfrom
benfurfie:feature/findOrFail

Conversation

@benfurfie

@benfurfiebenfurfie commented Feb 11, 2024

Copy link
Copy Markdown
Contributor

Key deets

Targets: [4.x]
Closesstatamic/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)

useStatamic\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:

useStatamic\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.
@benfurfiebenfurfie changed the title Feature/find or failImplement find or fail on EntryRepositoryFeb 11, 2024
@benfurfiebenfurfie changed the title Implement find or fail on EntryRepository[4.x] Implement find or fail on EntryRepositoryFeb 11, 2024
Comment threadsrc/Contracts/Entries/EntryRepository.php Outdated
Comment threadsrc/Stache/Repositories/EntryRepository.php Outdated
Comment threadsrc/Stache/Repositories/EntryRepository.php Outdated
Comment threadsrc/Exceptions/EntryNotFoundException.php Outdated
benfurfieand others added 6 commits February 20, 2024 08:01
@duncanmcclean
duncanmcclean changed the base branch from 4.x to masterFebruary 20, 2024 17:15
@duncanmccleanduncanmcclean changed the title [4.x] Implement find or fail on EntryRepository[5.x] Implement find or fail on EntryRepositoryFeb 20, 2024

@duncanmccleanduncanmcclean left a comment

Copy link
Copy Markdown
Member

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.

Comment threadsrc/Stache/Repositories/EntryRepository.php Outdated
@jasonvarga
jasonvarga changed the base branch from master to 4.xFebruary 20, 2024 21:42
@jasonvargajasonvarga changed the title [5.x] Implement find or fail on EntryRepository[4.x] Implement find or fail on EntryRepositoryFeb 20, 2024
@jasonvarga

Copy link
Copy Markdown
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.

duncanmcclean added a commit to statamic/docs that referenced this pull request Feb 27, 2024
Sign up for freeto 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

@benfurfie@jasonvarga@ryanmitchell@edalzell@duncanmcclean