From 95f3276309ac7e3d939156f25fc2de0cdeff82a2 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Mon, 5 May 2014 07:39:13 +0000 Subject: [PATCH] [Debug] fix handling deprecated warnings and stacked errors turned into exceptions --- src/Symfony/Component/Debug/ErrorHandler.php | 49 ++++++++++--------- .../Debug/Tests/ErrorHandlerTest.php | 6 +-- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/src/Symfony/Component/Debug/ErrorHandler.php b/src/Symfony/Component/Debug/ErrorHandler.php index c6b71c36ab..b62e5b7521 100644 --- a/src/Symfony/Component/Debug/ErrorHandler.php +++ b/src/Symfony/Component/Debug/ErrorHandler.php @@ -154,12 +154,10 @@ class ErrorHandler self::$loggers['deprecation']->warning($message, array('type' => self::TYPE_DEPRECATION, 'stack' => $stack)); } + + return true; } - - return true; - } - - if ($this->displayErrors && error_reporting() & $level && $this->level & $level) { + } elseif ($this->displayErrors && error_reporting() & $level && $this->level & $level) { if (self::$stackedErrorLevels) { self::$stackedErrors[] = func_get_args(); @@ -264,22 +262,35 @@ class ErrorHandler gc_collect_cycles(); $error = error_get_last(); - while (self::$stackedErrorLevels) { - static::unstackErrors(); + // get current exception handler + $exceptionHandler = set_exception_handler('var_dump'); + restore_exception_handler(); + + try { + while (self::$stackedErrorLevels) { + static::unstackErrors(); + } + } catch (\Exception $exception) { + if ($exceptionHandler) { + call_user_func($exceptionHandler, $exception); + + return; + } + + if ($this->displayErrors) { + ini_set('display_errors', 1); + } + + throw $exception; } - if (null === $error) { - return; - } - - $type = $error['type']; - if (0 === $this->level || !in_array($type, array(E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE))) { + if (!$error || !$this->level || !in_array($error['type'], array(E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE))) { return; } if (isset(self::$loggers['emergency'])) { $fatal = array( - 'type' => $type, + 'type' => $error['type'], 'file' => $error['file'], 'line' => $error['line'], ); @@ -287,14 +298,8 @@ class ErrorHandler self::$loggers['emergency']->emergency($error['message'], $fatal); } - if ($this->displayErrors) { - // get current exception handler - $exceptionHandler = set_exception_handler('var_dump'); - restore_exception_handler(); - - if ($exceptionHandler || self::$fatalHandler) { - $this->handleFatalError($exceptionHandler, $error); - } + if ($this->displayErrors && ($exceptionHandler || self::$fatalHandler)) { + $this->handleFatalError($exceptionHandler, $error); } } diff --git a/src/Symfony/Component/Debug/Tests/ErrorHandlerTest.php b/src/Symfony/Component/Debug/Tests/ErrorHandlerTest.php index 4c12a06d32..4765285404 100644 --- a/src/Symfony/Component/Debug/Tests/ErrorHandlerTest.php +++ b/src/Symfony/Component/Debug/Tests/ErrorHandlerTest.php @@ -134,12 +134,12 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase restore_error_handler(); $handler = ErrorHandler::register(E_USER_DEPRECATED); - $this->assertTrue($handler->handle(E_USER_DEPRECATED, 'foo', 'foo.php', 12, array())); + $this->assertFalse($handler->handle(E_USER_DEPRECATED, 'foo', 'foo.php', 12, array())); restore_error_handler(); $handler = ErrorHandler::register(E_DEPRECATED); - $this->assertTrue($handler->handle(E_DEPRECATED, 'foo', 'foo.php', 12, array())); + $this->assertFalse($handler->handle(E_DEPRECATED, 'foo', 'foo.php', 12, array())); restore_error_handler(); @@ -162,7 +162,7 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase $handler = ErrorHandler::register(E_USER_DEPRECATED); $handler->setLogger($logger); - $handler->handle(E_USER_DEPRECATED, 'foo', 'foo.php', 12, array()); + $this->assertTrue($handler->handle(E_USER_DEPRECATED, 'foo', 'foo.php', 12, array())); restore_error_handler();