statamic / ideas

đŸ’¡Discussions on ideas and feature requests for Statamic
https://statamic.dev
30 stars 1 forks source link

Add findOrFail to repositories #1126

Closed benfurfie closed 4 months ago

benfurfie commented 4 months ago

What?

Currently, all of the repositories in Statamic have the find method. For example, in the EntryRepository, if an entry isn't found, it returns null. However, there are plenty of cases where when making heavy use of the Entry Facade to create and render entries programatically, we need to be aware of an entry that should exist not being found.

Business case

One common way of handling this in the Laravel world is to use the findOrFail method, where an exception will be thrown if a record is not found, and can be picked up by error monitoring tools like Sentry, Flare, Rollbar etc. This allows us to proactively fix issues before users become aware of them.

There is also an argument that introducing the functionality will bring Statamic more into line with Laravel's conventions, and make it easier to integrate existing Laravel packages with Statamic without needing to resort to overriding packages' Service class methods (such as in @edalzell's closed PR).

This request is similar to #817, but is focused on throwing exceptions, rather than returning a 404, as in keeping with Laravel's convention.

Next steps

I've created a PR to add the findOrFail method to the EntryRepository. I've limited it to one repository initially, as it makes sense to keep the initial scope small until we have agreed on the apporoach.

Assuming this PR is accepted and merged, the plan would then be to implement the method (where it makes sense) to the other repositories in Statamic.