Skip to content

Commit 30ffb61

Browse files
committed
bug #27237 [Debug] Fix populating error_get_last() for handled silent errors (nicolas-grekas)
This PR was merged into the 2.7 branch. Discussion ---------- [Debug] Fix populating error_get_last() for handled silent errors | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - When a userland error handler doesn't return `false`, `error_get_last()` is not updated, so we cannot see the real last error, but the previous one. See https://3v4l.org/Smmt7 Commits ------- d7e612d [Debug] Fix populating error_get_last() for handled silent errors
2 parents 15b03a8 + d7e612d commit 30ffb61

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/Symfony/Component/Debug/ErrorHandler.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -377,13 +377,15 @@ private function reRegister($prev)
377377
*/
378378
public function handleError($type, $message, $file, $line)
379379
{
380-
$level = error_reporting() | E_RECOVERABLE_ERROR | E_USER_ERROR | E_DEPRECATED | E_USER_DEPRECATED;
380+
$level = error_reporting();
381+
$silenced = 0 === ($level & $type);
382+
$level |= E_RECOVERABLE_ERROR | E_USER_ERROR | E_DEPRECATED | E_USER_DEPRECATED;
381383
$log = $this->loggedErrors & $type;
382384
$throw = $this->thrownErrors & $type & $level;
383385
$type &= $level | $this->screamedErrors;
384386

385387
if (!$type || (!$log && !$throw)) {
386-
return $type && $log;
388+
return !$silenced && $type && $log;
387389
}
388390
$scope = $this->scopedErrors & $type;
389391

@@ -479,7 +481,7 @@ public function handleError($type, $message, $file, $line)
479481
}
480482
}
481483

482-
return $type && $log;
484+
return !$silenced && $type && $log;
483485
}
484486

485487
/**

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

+24
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,30 @@ public function testRegister()
6464
}
6565
}
6666

67+
public function testErrorGetLast()
68+
{
69+
$handler = ErrorHandler::register();
70+
$logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
71+
$handler->setDefaultLogger($logger);
72+
$handler->screamAt(E_ALL);
73+
74+
try {
75+
@trigger_error('Hello', E_USER_WARNING);
76+
$expected = array(
77+
'type' => E_USER_WARNING,
78+
'message' => 'Hello',
79+
'file' => __FILE__,
80+
'line' => __LINE__ - 5,
81+
);
82+
$this->assertSame($expected, error_get_last());
83+
} catch (\Exception $e) {
84+
restore_error_handler();
85+
restore_exception_handler();
86+
87+
throw $e;
88+
}
89+
}
90+
6791
public function testNotice()
6892
{
6993
ErrorHandler::register();

0 commit comments

Comments
 (0)