8000 bug #35645 [ErrorHandler] Never throw on warnings triggered by assert… · symfony/symfony@138439a · GitHub
[go: up one dir, main page]

Skip to content

Commit 138439a

Browse files
committed
bug #35645 [ErrorHandler] Never throw on warnings triggered by assert() and set assert.exception=1 in Debug::enable() (nicolas-grekas)
This PR was merged into the 4.4 branch. Discussion ---------- [ErrorHandler] Never throw on warnings triggered by assert() and set assert.exception=1 in Debug::enable() | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Because we don't use `assert()`, this is something we completely overlooked, but warnings triggered should not throw as there is already a dedicated exception mode when using `assert()`. This turns this exception mode to 1 in debug mode and logs the assert() warnings in prod. Commits ------- f18ef6c [ErrorHandler] Never throw on warnings triggered by assert() and set assert.exception=1 in Debug::enable()
2 parents cb42480 + f18ef6c commit 138439a

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

src/Symfony/Component/ErrorHandler/Debug.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public static function enable(): ErrorHandler
2929
ini_set('display_errors', 1);
3030
}
3131

32+
ini_set('zend.assertions', 1);
33+
ini_set('assert.active', 1);
34+
ini_set('assert.warning', 0);
35+
ini_set('assert.exception', 1);
36+
3237
DebugClassLoader::enable();
3338

3439
return ErrorHandler::register(new ErrorHandler(new BufferingLogger(), true));

src/Symfony/Component/ErrorHandler/ErrorHandler.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,11 @@ public function handleError(int $type, string $message, string $file, int $line)
413413
$throw = $this->thrownErrors & $type & $level;
414414
$type &= $level | $this->screamedErrors;
415415

416+
// Never throw on warnings triggered by assert()
417+
if (E_WARNING === $type && 'a' === $message[0] && 0 === strncmp($message, 'assert(): ', 10)) {
418+
$throw = 0;
419+
}
420+
416421
if (!$type || (!$log && !$throw)) {
417422
return !$silenced && $type && $log;
418423
}

src/Symfony/Component/ErrorHandler/Tests/ErrorHandlerTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,4 +615,39 @@ public function errorHandlerWhenLoggingProvider(): iterable
615615
}
616616
}
617617
}
618+
619+
public function testAssertQuietEval()
620+
{
621+
$ini = [
622+
ini_set('zend.assertions', 1),
623+
ini_set('assert.active', 1),
624+
ini_set('assert.bail', 0),
625+
ini_set('assert.warning', 1),
626+
ini_set('assert.callback', null),
627+
ini_set('assert.exception', 0),
628+
];
629+
630+
$logger = new BufferingLogger();
631+
$handler = new ErrorHandler($logger);
632+
$handler = ErrorHandler::register($handler);
633+
634+
try {
635+
\assert(false);
636+
} finally {
637+
restore_error_handler();
638+
restore_exception_handler();
639+
640+
ini_set('zend.assertions', $ini[0]);
641+
ini_set('assert.active', $ini[1]);
642+
ini_set('assert.bail', $ini[2]);
643+
ini_set('assert.warning', $ini[3]);
644+
ini_set('assert.callback', $ini[4]);
645+
ini_set('assert.exception', $ini[5]);
646+
}
647+
648+
$logs = $logger->cleanLogs();
649+
650+
$this->assertSame('warning', $logs[0][0]);
651+
$this->assertSame('Warning: assert(): assert(false) failed', $logs[0][1]);
652+
}
618653
}

0 commit comments

Comments
 (0)
0