[13.x] feat: convert QueryException to ModelNotFoundException in implicit route binding#58943
[13.x] feat: convert QueryException to ModelNotFoundException in implicit route binding#58943calebdw wants to merge 1 commit intolaravel:13.xfrom
QueryException to ModelNotFoundException in implicit route binding#58943Conversation
QueryException to ModelNotFoundException in implicit route binding
ad0a9b6 to
e484dc0
Compare
|
Is this intentional? 🤔 |
Is what intentional? 🧐 I intentionally submitted this PR if that's what you're asking about 🙃 |
|
No, sorry, should have included the quote, I was looking at 😬 😅
|
|
Lol, I'm not sure. I was trying to keep this PR simple, but if folks want the exception then I can add a way to configure it |
|
I guess, that would be great—thanks 👍🏻 |
|
I wonder if this would be hard to debug what is going on, especially if you expect to be getting a model back, you get some query error (maybe a global scope), but now it's masked as a 404. It feels like the current behavior is pretty helpful in local development, but maybe not ideal in prod (though you would still want the original error). |
|
Yeah, I could see maybe having some configuration options, maybe something like: Model::throwRouteModelBindingExceptions();
// and / or maybe just this
Model::reportRouteModelBindingExceptions();I think from the end user perspective they should always receive a 404 instead of a 500 in production |
|
Nice! 👍🏻 |
e484dc0 to
6540965
Compare
|
@taylorotwell, I've updated to report the |
…ute binding When implicit route model binding encounters a QueryException (e.g., from integer overflow or type mismatch), convert it to a ModelNotFoundException instead of letting the 500 propagate. This ensures invalid route parameters like '/users/99999999999999999999' return a 404 rather than a 500.
6540965 to
d6e5406
Compare
|
Hmm, let me think about this one - I'm not confident we're totally landing on the right solution. |
Hello!
When implicit route model binding encounters a
QueryException(e.g., from integer overflow or type mismatch), convert it to aModelNotFoundExceptioninstead of letting the 500 propagate. This ensures invalid route parameters like/users/99999999999999999999return a 404 rather than a 500.This has been the biggest pain, without this you have to add
->whereNumber()on every route OR you litter theAppServiceProviderwith 50+ calls likeRoute::pattern('user', '[0-9]+');. Even with this, we've still had exceptions where the parameter was all digits but it exceeded the column int size so we had to add even more checks to prevent the exception.Ultimately, a model was not found so end user should always receive a 404 instead of a 500.
The
QueryExceptionis still reported, but developers now have the ability to disable reporting, for example:Thanks!