Replies: 1 comment 6 replies
-
@dfidler I will be blamed for this, but laravel is there to generate as many billable hours as possible. It offers 10000000 ways of doing things just for that purpose. It is the developer's job to learn it, love it at first and then hate it. I use try catch with modelNotFoundException, but I always take care to query something there and not to search in php in collections. It makes sense to throw ItemNotFoundException in the collections situation (thanks, I did not knew it behaves like this),I used instanceof comparison or null comparison coupled with find(). A solution would be Side note. I always expect exceptions from db queries, cache queries etc. My bad I don't expect exceptions from Log::error('text'); but that is because I log to stdout. If the logger can't write into a file for example it will throw something. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
PROBLEM STATEMENT
Eloquent Builder and [Illuminate\Support] Collection both have rich filtering functionality and there is a huge overlap between the two subsystems.
Consider the following two statements:
Functionally, these achieve the same outcome and, in fact, if "tenant" isn't loaded in ex1, laravel does the +1 query to load it (per the relationship) and returns the appropriate tenant.
Aside: And, in my opinion, ex1 is almost always the better syntax (because we all make extensive use of with/load/loadMissing during our first query, right 😄).
However, if tenant "bob" doesn't exist, the two pieces of code produce different results...
The family tree for these two exceptions is as follows:
ex1: ItemNotFoundException (henceforth INFE) <-- RuntimeException
ex2: ModeNotFoundException (henceforth MNFE) <-- RecordsNotFoundException (henceforth RNFE) <-- RuntimeException
I've recently had a production failure because of this exact thing; the dev used syntax from ex1 and wrapped it with a
try/catch (ModelNotFoundException)
.To me, this seems like a pretty reasonable (and harmless) mistake and that two features, with so much overlap, shouldn't produce a production outage when it happens.
Note: As a result of this, I've done some regexp'ing on our code base and have two more examples where this will, eventually, cause a failure (because they are catching MNFE insteand of INFE).
PROPOSED SOLUITON
It seems reasonable to insert INFE as the ancestor to RecordsNotFoundException (although it does mean that Eloquent would then have a dependency on Support\Collections - which doesn't bother me, but it might bother some).
I also don't foresee any potential impact in the laravel code base as RNFE is only used in four places (none of which make use of Support Collections), though I can't speak for any other laravel plugins.
It seems like a pretty innocuous change and I'm wondering how many other code bases have (and will have in the future) a customer/production UX issue as a result of this small syntactical difference.
Thoughts?
Beta Was this translation helpful? Give feedback.
All reactions