[Debug] Fix handling of php7 throwables

This commit is contained in:
Nicolas Grekas 2016-03-28 11:08:12 +02:00
parent 09cc0b20d5
commit b032096763
2 changed files with 30 additions and 0 deletions

View File

@ -46,6 +46,30 @@ class Debug
ErrorHandler::register($errorReportingLevel, $displayErrors);
if ('cli' !== PHP_SAPI) {
ExceptionHandler::register();
if (PHP_VERSION_ID >= 70000) {
$exceptionHandler = set_exception_handler(function ($throwable) use (&$exceptionHandler) {
if ($throwable instanceof \Exception) {
$exception = $throwable;
} else {
static $refl = null;
if (null === $refl) {
$refl = array();
foreach (array('file', 'line', 'trace') as $prop) {
$prop = new \ReflectionProperty('Exception', $prop);
$prop->setAccessible(true);
$refl[] = $prop;
}
}
$exception = new \Exception($throwable->getMessage(), $throwable->getCode());
foreach ($refl as $prop) {
$prop->setValue($exception, $throwable->{'get'.$prop->name}());
}
}
$exceptionHandler($exception);
});
}
// CLI - display errors only if they're not already logged to STDERR
} elseif ($displayErrors && (!ini_get('log_errors') || ini_get('error_log'))) {
ini_set('display_errors', 1);

View File

@ -197,6 +197,12 @@ class ErrorHandler
$exceptionHandler = set_exception_handler(function () {});
restore_exception_handler();
if (PHP_VERSION_ID >= 70000 && $exceptionHandler instanceof \Closure) {
$reflector = new \ReflectionFunction($exceptionHandler);
foreach ($reflector->getStaticVariables() as $exceptionHandler) {
break;
}
}
if (is_array($exceptionHandler) && $exceptionHandler[0] instanceof ExceptionHandler) {
$level = isset($this->levels[$type]) ? $this->levels[$type] : $type;
$message = sprintf('%s: %s in %s line %d', $level, $error['message'], $error['file'], $error['line']);