Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[HttpKernel] Handle throwable errors in HttpKernel#26514
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
203804d to0a8215dComparenicolas-grekas commentedMar 14, 2018
I'm mostly 👎 on this change: the debug component is already a dependency of http-kernel, so you just need to use it. And using the debug component handles real fatal errors, which do still exist. This change could give the illusion that nothing else is needed. That's not true, and that's why the debug component exists. |
derrabus commentedMar 14, 2018
I'm fine with the debug component being a dependency of the kernel. In fact, this PR makes use of the component as well. Also, I don't say that a proper error handler is not needed. I just don't see why I'm required to use the error handler from the debug component. Take the script from the PR description: Why am I required to register a specific global error handler to make this script work? |
nicolas-grekas commentedMar 14, 2018 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
because you want to catch fatal errors, and this is the only way to do it? |
derrabus commentedMar 14, 2018
I could use any other error handler for fatal errors. That's not the point. The script does not raise a fatal error. It raises a simple |
derrabus commentedMar 14, 2018
Also, if I changed the controller above to raise an exception instead, that exception would be nicely handled. But an error falls through. That feels inconsistent. |
nicolas-grekas commentedMar 14, 2018 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
|
derrabus commentedMar 14, 2018
The main difference is that exceptions are usually thrown by userland code and errors are raised by php core and extensions.
You're absolutely right. Same goes for many exceptions btw, like What I expect an application framework to do in this case is to fail nicely, provide me with helpful information and (in case it happens on production) give me the chance to log that error somehow. That is to my understanding the purpose of the global try/catch block in HttpKernel. And that block is – imho – incomplete if it ignores errors. |
nicolas-grekas commentedMar 14, 2018
Yep, same expectations, including for fatal errors on my side. This is already provided by Debug, and thinking catching solves the problem is an illusion :) |
derrabus commentedMar 14, 2018
As you said, that's what we need an error handler for. It was not my intention to solve fatals.
So with that argument, we could remove the entire try/catch block from HttpKernel. In fact, the debug component really handles it nicely, if I actually do that. 😮 Yet, that block is there and it serves a purpose. I'm only updating it for the php 7 era. |
nicolas-grekas commentedMar 14, 2018
From Slack: derrabus [3:55 PM] nicolasgrekas [3:57 PM] derrabus [3:59 PM] nicolasgrekas [3:59 PM] derrabus [4:01 PM] nicolasgrekas [4:02 PM] derrabus [4:03 PM] nicolasgrekas [4:03 PM] derrabus [4:03 PM] nicolasgrekas [4:03 PM] derrabus [4:06 PM] nicolasgrekas [4:06 PM] derrabus [4:08 PM] nicolasgrekas [4:08 PM] derrabus [4:17 PM] nicolasgrekas [4:18 PM] derrabus [4:20 PM] nicolasgrekas [4:20 PM] derrabus [4:23 PM] nicolasgrekas [4:24 PM] derrabus [4:25 PM] nicolasgrekas [4:26 PM] derrabus [4:28 PM] nicolasgrekas [4:29 PM] derrabus [4:32 PM] nicolasgrekas [4:32 PM] derrabus [4:34 PM] nicolasgrekas [4:35 PM] derrabus [4:39 PM] nicolasgrekas [4:40 PM] derrabus [4:43 PM] nicolasgrekas [4:43 PM] derrabus [4:43 PM] nicolasgrekas [4:44 PM] derrabus [4:47 PM] |
…to show `HttpKernel::handle()` will catch throwables (Nyholm)This PR was merged into the 6.2 branch.Discussion----------[FrameworkBundle][HttpKernel] Add deprecation warning to show `HttpKernel::handle()` will catch throwables| Q | A| ------------- | ---| Branch? | 6.2| Bug fix? | no| New feature? | no| Deprecations? | yes| Tickets |Fix#16205| License | MIT| Doc PR | symfony/symfony-docs#... <!-- required for new features -->I suggest that starting from Symfony 7.0 the `HttpKernel` will start to catch `\Throwable` and convert them to a `Response`.This was first asked in#16205, I face a similar issue with Runtime component and Bref..----------The reason I push for this change is to embrace the request/response workflow of the Kernel without trusting the custom error handler. In an environment where you serve multiple requests with the same PHP process (read: RoadRunner, Swoole, Bref) you would write something like:```php$kernel = new Kernel('prod', false);while (true) { $request = /* create sf request from custom environment */ try { $response = $kernel->handle(request); return ResponseConverter::convert($response); } catch (\Throwable $e) { exit(1); }}```(pseudo code of course. Here is a [real example](https://github.com/php-runtime/bref/blob/0.3.1/src/BrefRunner.php#L30-L43))The `exit(1)` means a hard crash. For Bref Runtime it would result in a 500 error from API-gateway. Since the `\Throwable` is caught, the Symfony error handler is not used. If we would not to catch the `\Throwable`, then the Symfony error handler would be used, but it would print a Response instead of returning it. (Printing a response will just add HTML on the CLI...)-----------Other PRs and issues related to this:-#45301-#26514-#22128-#36885-#25467I'm happy to let the `HttpKernel` to catch the `\Throwable` exception right now, but I thought this very conservative PR would have a higher change to get merged.Also note that we do not specify any behaviour on our [HttpKernelInterface](https://github.com/symfony/symfony/blob/v6.0.7/src/Symfony/Component/HttpKernel/HttpKernelInterface.php#L43)-------To remove the deprecation message you need to add this to your config:```yamlframework: catch_all_throwables: true```Commits-------7977a15 Add deprecation warning to show HttpKernel::handle() will catch throwables
Uh oh!
There was an error while loading.Please reload this page.
Currently,
HttpKernelonly handles exceptions. If the request processing raises an error, that error falls through. In Symfony full-stack applications, this is not a problem, because the framework bundle registers an error handler from the debug component that converts errors to exceptions.This PR enables
HttpKernelto handle errors that have been raised during the request processing, so it can be used standalone in environments where said error handler is not in use.Test script:
Note that this PR does not change how errors are being processed. They still need to be converted before dispatching the
kernel.exceptionevent. The only difference is thatHttpKerneldoes not depend on a globally registered error handler anymore to perform that conversion.