Issue #2505315 by stefan.r, dawehner, tim.plunkett, Berdir: Catch PHP7 Throwable objects instead of BaseExceptions in the error handler

8.0.x
Alex Pott 2015-07-22 16:46:53 +01:00
parent 4988a81a75
commit a8f8b33e72
3 changed files with 20 additions and 18 deletions

View File

@ -679,7 +679,7 @@ function _drupal_error_handler($error_level, $message, $filename, $line, $contex
* always fatal: the execution of the script will stop as soon as the exception * always fatal: the execution of the script will stop as soon as the exception
* handler exits. * handler exits.
* *
* @param \Exception|\BaseException $exception * @param \Exception|\Throwable $exception
* The exception object that was thrown. * The exception object that was thrown.
*/ */
function _drupal_exception_handler($exception) { function _drupal_exception_handler($exception) {
@ -689,12 +689,13 @@ function _drupal_exception_handler($exception) {
// Log the message to the watchdog and return an error page to the user. // Log the message to the watchdog and return an error page to the user.
_drupal_log_error(Error::decodeException($exception), TRUE); _drupal_log_error(Error::decodeException($exception), TRUE);
} }
// PHP 7 introduces BaseExceptions. // PHP 7 introduces Throwable, which covers both Error and
catch (BaseException $exception2) { // Exception throwables.
_drupal_exception_handler_additional($exception, $exception2); catch (\Throwable $error) {
_drupal_exception_handler_additional($exception, $error);
} }
// In order to be compatibile with PHP 5 we also catch regular Exceptions. // In order to be compatibile with PHP 5 we also catch regular Exceptions.
catch (Exception $exception2) { catch (\Exception $exception2) {
_drupal_exception_handler_additional($exception, $exception2); _drupal_exception_handler_additional($exception, $exception2);
} }
} }
@ -702,9 +703,9 @@ function _drupal_exception_handler($exception) {
/** /**
* Displays any additional errors caught while handling an exception. * Displays any additional errors caught while handling an exception.
* *
* @param \Exception|\BaseException $exception * @param \Exception|\Throwable $exception
* The first exception object that was thrown. * The first exception object that was thrown.
* @param \Exception|\BaseException $exception * @param \Exception|\Throwable $exception2
* The second exception object that was thrown. * The second exception object that was thrown.
*/ */
function _drupal_exception_handler_additional($exception, $exception2) { function _drupal_exception_handler_additional($exception, $exception2) {
@ -1098,12 +1099,13 @@ function _drupal_shutdown_function() {
call_user_func_array($callback['callback'], $callback['arguments']); call_user_func_array($callback['callback'], $callback['arguments']);
} }
} }
// PHP 7 introduces BaseExceptions. // PHP 7 introduces Throwable, which covers both Error and
catch (BaseException $exception) { // Exception throwables.
_drupal_shutdown_function_handle_exception($exception); catch (\Throwable $error) {
_drupal_shutdown_function_handle_exception($error);
} }
// In order to be compatibile with PHP 5 we also catch regular Exceptions. // In order to be compatibile with PHP 5 we also catch regular Exceptions.
catch (Exception $exception) { catch (\Exception $exception) {
_drupal_shutdown_function_handle_exception($exception); _drupal_shutdown_function_handle_exception($exception);
} }
} }
@ -1111,7 +1113,7 @@ function _drupal_shutdown_function() {
/** /**
* Displays and logs any errors that may happen during shutdown. * Displays and logs any errors that may happen during shutdown.
* *
* @param \Exception|\BaseException $exception * @param \Exception|\Throwable $exception
* The exception object that was thrown. * The exception object that was thrown.
* *
* @see _drupal_shutdown_function() * @see _drupal_shutdown_function()

View File

@ -33,7 +33,7 @@ class Error {
/** /**
* Decodes an exception and retrieves the correct caller. * Decodes an exception and retrieves the correct caller.
* *
* @param \Exception|\BaseException $exception * @param \Exception|\Throwable $exception
* The exception object that was thrown. * The exception object that was thrown.
* *
* @return array * @return array
@ -82,7 +82,7 @@ class Error {
/** /**
* Renders an exception error message without further exceptions. * Renders an exception error message without further exceptions.
* *
* @param \Exception|\BaseException $exception * @param \Exception|\Throwable $exception
* The exception object that was thrown. * The exception object that was thrown.
* *
* @return string * @return string

View File

@ -52,8 +52,8 @@ class ErrorHandlerTest extends WebTestBase {
'!message' => 'Argument 1 passed to Drupal\error_test\Controller\ErrorTestController::Drupal\error_test\Controller\{closure}() must be of the type array, string given, called in ' . \Drupal::root() . '/core/modules/system/tests/modules/error_test/src/Controller/ErrorTestController.php on line 66 and defined', '!message' => 'Argument 1 passed to Drupal\error_test\Controller\ErrorTestController::Drupal\error_test\Controller\{closure}() must be of the type array, string given, called in ' . \Drupal::root() . '/core/modules/system/tests/modules/error_test/src/Controller/ErrorTestController.php on line 66 and defined',
); );
if (version_compare(PHP_VERSION, '7.0.0-dev') >= 0) { if (version_compare(PHP_VERSION, '7.0.0-dev') >= 0) {
// In PHP 7, instead of a recoverable fatal error we get a TypeException. // In PHP 7, instead of a recoverable fatal error we get a TypeError.
$fatal_error['%type'] = 'TypeException'; $fatal_error['%type'] = 'TypeError';
// The error message also changes in PHP 7. // The error message also changes in PHP 7.
$fatal_error['!message'] = 'Argument 1 passed to Drupal\error_test\Controller\ErrorTestController::Drupal\error_test\Controller\{closure}() must be of the type array, string given, called in ' . \Drupal::root() . '/core/modules/system/tests/modules/error_test/src/Controller/ErrorTestController.php on line 66'; $fatal_error['!message'] = 'Argument 1 passed to Drupal\error_test\Controller\ErrorTestController::Drupal\error_test\Controller\{closure}() must be of the type array, string given, called in ' . \Drupal::root() . '/core/modules/system/tests/modules/error_test/src/Controller/ErrorTestController.php on line 66';
} }
@ -76,9 +76,9 @@ class ErrorHandlerTest extends WebTestBase {
// Remove the recoverable fatal error from the assertions, it's wanted here. // Remove the recoverable fatal error from the assertions, it's wanted here.
// Ensure that we just remove this one recoverable fatal error (in PHP 7 this // Ensure that we just remove this one recoverable fatal error (in PHP 7 this
// is a TypeException). // is a TypeError).
foreach ($this->assertions as $key => $assertion) { foreach ($this->assertions as $key => $assertion) {
if (in_array($assertion['message_group'], ['Recoverable fatal error', 'TypeException']) && strpos($assertion['message'], 'Argument 1 passed to Drupal\error_test\Controller\ErrorTestController::Drupal\error_test\Controller\{closure}() must be of the type array, string given, called in') !== FALSE) { if (in_array($assertion['message_group'], ['Recoverable fatal error', 'TypeError']) && strpos($assertion['message'], 'Argument 1 passed to Drupal\error_test\Controller\ErrorTestController::Drupal\error_test\Controller\{closure}() must be of the type array, string given, called in') !== FALSE) {
unset($this->assertions[$key]); unset($this->assertions[$key]);
$this->deleteAssert($assertion['message_id']); $this->deleteAssert($assertion['message_id']);
} }