Skip to content

Commit de1a6ac

Browse files
bug symfony#58372 Tweak error/exception handler registration (nicolas-grekas)
This PR was merged into the 5.4 branch. Discussion ---------- Tweak error/exception handler registration | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix symfony#53812 | License | MIT This should allow removing the custom bootstrap file from symfony#58370 /cc `@xabbuh` The change on FrameworkBundle leads to a tweaked behavior: we don't override the previous error handler in case it's not the Symfony one. This shouldn't change anything in practice since the error handler is already registered by the runtime component. The rest is closer to bug fixes. Commits ------- af9c035 Tweak error/exception handler registration
2 parents b6a5496 + af9c035 commit de1a6ac

File tree

7 files changed

+47
-17
lines changed

7 files changed

+47
-17
lines changed

src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
use Symfony\Component\Mime\DependencyInjection\AddMimeTypeGuesserPass;
6161
use Symfony\Component\PropertyInfo\DependencyInjection\PropertyInfoPass;
6262
use Symfony\Component\Routing\DependencyInjection\RoutingResolverPass;
63+
use Symfony\Component\Runtime\SymfonyRuntime;
6364
use Symfony\Component\Serializer\DependencyInjection\SerializerPass;
6465
use Symfony\Component\Translation\DependencyInjection\TranslationDumperPass;
6566
use Symfony\Component\Translation\DependencyInjection\TranslationExtractorPass;
@@ -91,7 +92,16 @@ class FrameworkBundle extends Bundle
9192
{
9293
public function boot()
9394
{
94-
ErrorHandler::register(null, false)->throwAt($this->container->getParameter('debug.error_handler.throw_at'), true);
95+
if (class_exists(SymfonyRuntime::class)) {
96+
$handler = set_error_handler('var_dump');
97+
restore_error_handler();
98+
} else {
99+
$handler = [ErrorHandler::register(null, false)];
100+
}
101+
102+
if (\is_array($handler) && $handler[0] instanceof ErrorHandler) {
103+
$handler[0]->throwAt($this->container->getParameter('debug.error_handler.throw_at'), true);
104+
}
95105

96106
if ($this->container->getParameter('kernel.http_method_override')) {
97107
Request::enableHttpMethodParameterOverride();

src/Symfony/Bundle/FrameworkBundle/composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
"symfony/mime": "<4.4",
8787
"symfony/property-info": "<4.4",
8888
"symfony/property-access": "<5.3",
89+
"symfony/runtime": "<5.4.45|>=6.0,<6.4.13|>=7.0,<7.1.6",
8990
"symfony/serializer": "<5.2",
9091
"symfony/service-contracts": ">=3.0",
9192
"symfony/security-csrf": "<5.3",

src/Symfony/Component/Console/Application.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,9 @@ public function run(?InputInterface $input = null, ?OutputInterface $output = nu
165165
}
166166
}
167167

168-
$this->configureIO($input, $output);
169-
170168
try {
169+
$this->configureIO($input, $output);
170+
171171
$exitCode = $this->doRun($input, $output);
172172
} catch (\Exception $e) {
173173
if (!$this->catchExceptions) {

src/Symfony/Component/HttpKernel/EventListener/DebugHandlersListener.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function __construct(?callable $exceptionHandler = null, ?LoggerInterface
5757
$deprecationLogger = $fileLinkFormat;
5858
}
5959

60-
$handler = set_exception_handler('is_int');
60+
$handler = set_exception_handler('var_dump');
6161
$this->earlyHandler = \is_array($handler) ? $handler[0] : null;
6262
restore_exception_handler();
6363

@@ -84,7 +84,7 @@ public function configure(?object $event = null)
8484
$this->firstCall = $this->hasTerminatedWithException = false;
8585
$hasRun = null;
8686

87-
$handler = set_exception_handler('is_int');
87+
$handler = set_exception_handler('var_dump');
8888
$handler = \is_array($handler) ? $handler[0] : null;
8989
restore_exception_handler();
9090

src/Symfony/Component/Runtime/GenericRuntime.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ public function __construct(array $options = [])
7171
if ($debug) {
7272
umask(0000);
7373
$_SERVER[$debugKey] = $_ENV[$debugKey] = '1';
74-
75-
if (false !== $errorHandler = ($options['error_handler'] ?? BasicErrorHandler::class)) {
76-
$errorHandler::register($debug);
77-
$options['error_handler'] = false;
78-
}
7974
} else {
8075
$_SERVER[$debugKey] = $_ENV[$debugKey] = '0';
8176
}
8277

78+
if (false !== $errorHandler = ($options['error_handler'] ?? BasicErrorHandler::class)) {
79+
$errorHandler::register($debug);
80+
$options['error_handler'] = false;
81+
}
82+
8383
$this->options = $options;
8484
}
8585

src/Symfony/Component/Runtime/Internal/BasicErrorHandler.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ public static function register(bool $debug): void
3030
}
3131

3232
if (0 <= \ini_get('zend.assertions')) {
33-
ini_set('zend.assertions', 1);
34-
ini_set('assert.active', $debug);
35-
ini_set('assert.exception', 1);
33+
ini_set('zend.assertions', (int) $debug);
3634
}
35+
ini_set('assert.active', 1);
36+
ini_set('assert.exception', 1);
3737

3838
set_error_handler(new self());
3939
}

src/Symfony/Component/Runtime/Internal/SymfonyErrorHandler.php

+23-4
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,31 @@ class SymfonyErrorHandler
2424
{
2525
public static function register(bool $debug): void
2626
{
27-
BasicErrorHandler::register($debug);
27+
if (!class_exists(ErrorHandler::class)) {
28+
BasicErrorHandler::register($debug);
2829

29-
if (class_exists(ErrorHandler::class)) {
30+
return;
31+
}
32+
33+
error_reporting(-1);
34+
35+
if (!\in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true)) {
36+
ini_set('display_errors', $debug);
37+
} elseif (!filter_var(\ini_get('log_errors'), \FILTER_VALIDATE_BOOL) || \ini_get('error_log')) {
38+
// CLI - display errors only if they're not already logged to STDERR
39+
ini_set('display_errors', 1);
40+
}
41+
42+
if (0 <= \ini_get('zend.assertions')) {
43+
ini_set('zend.assertions', (int) $debug);
44+
}
45+
ini_set('assert.active', 1);
46+
ini_set('assert.exception', 1);
47+
48+
if ($debug) {
3049
DebugClassLoader::enable();
31-
restore_error_handler();
32-
ErrorHandler::register(new ErrorHandler(new BufferingLogger(), $debug));
3350
}
51+
52+
ErrorHandler::register(new ErrorHandler(new BufferingLogger(), $debug));
3453
}
3554
}

0 commit comments

Comments
 (0)