-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
Renderable exceptions inside views #35493
Comments
Hmm, I don't totally recall why that wrapping is done. It doesn't seem like it would always be entirely necessary but maybe it is in certain situations? |
Maybe keeping the wrapping but forwarding the method calls? |
@driesvints Do you need more info from my side or is it ok? |
@ThibaudDauce, do you have any use cases when thrown an exception inside views is useful? it's very bad idea, views should not throws exceptions, they are needed exclusively for displaying data, that's all. |
Yes I may call a method from a model that throws an exception. The |
@ThibaudDauce, you should not call model's method inside the view, there shouldn't be any logic, only the data output. |
I've discovered that this was implemented by @taylorotwell in 2013 here: bccc3fb If I look at the commit correctly is seems to be done to include the view path in the exception message. @taylorotwell could that be correct? |
Maybe we can create a if (method_exists($e, 'render') && $response = $e->render($request)) {
return Router::toResponse($request, $response);
} elseif ($e instanceof Responsable) {
return $e->toResponse($request);
} If the exception inside has a |
Something like: public function render($request)
{
if (!$this->getPrevious() || !method_exists($this->getPrevious(), 'render')) {
return null;
}
return $this->getPrevious()->render($request);
} |
Seems like a possible solution. This probably hasn't come up because it may be kinda rare to trigger renderable exception in views? What is your use case where this is happening? |
Accounts on my website are linked with customers (relation HasMany), an account without an attached customer throws an exception. I want to redirect to the home page with a notification explaining why the account is not valid. There are other solutions in my case:
My solution with an exception has the advantage to be lazy with no SQL request until necessary. It also do not require adding more code since I'll throw the exception anyway in case of a "problem" (missing middleware or just to please the IDE and avoiding returning I think the middleware could be a solution for the The |
@ThibaudDauce Maybe by eager loading your relation you may come arround that issue by throwing the exception earlier. |
@ThibaudDauce I managed to reproduce this in another app where I was having the exact same issue. But for me I saw the original stacktrack with the exact point where the error was happening when I removed |
Hi, my repository https://github.com/ThibaudDauce/ignition_bug reproduces the bug without Ignition (the first commit is with Ignition but I tried without in ThibaudDauce/ignition_bug@2611d75 when they told me it was a Laravel problem in my issue https://github.com/facade/ignition/issues/339) |
The problem is not not sawing the original stacktrace, the problem is not calling the |
I've finally found time to deep dive into this and managed to send in a PR that should fix this: #36032 I'll report the other issue I discovered from above (that indeed didn't had anything to do with this one) to the Ignition issue tracker. @ThibaudDauce thanks for reporting this one. |
Description:
An exception with a
render
method thrown inside a view is wrapped inside aErrorException
so therender
method is not executed by the Laravel's exception handler.A simple repro repository https://github.com/ThibaudDauce/ignition_bug
See
routes/web.php
:My
TestRenderableException
:I think there is the same problem with the
report
function.I first though it was an issue with Ignition https://github.com/facade/ignition/issues/339 but it doesn't works even after removing Ignition with
composer remove facade/ignition
. (the exception is wrapped inside anErrorException
by Laravel instead of aViewException
for Ignition, but it's the same problem)The text was updated successfully, but these errors were encountered: