8000 [Debug] Fix handling of php7 throwables · symfony/symfony@b032096 · GitHub
[go: up one dir, main page]

Skip to content

Commit b032096

Browse files
[Debug] Fix handling of php7 throwables
1 parent 09cc0b2 commit b032096

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/Symfony/Component/Debug/Debug.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,30 @@ public static function enable($errorReportingLevel = null, $displayErrors = true
4646
ErrorHandler::register($errorReportingLevel, $displayErrors);
4747
if ('cli' !== PHP_SAPI) {
4848
ExceptionHandler::register();
49+
50+
if (PHP_VERSION_ID >= 70000) {
51+
$exceptionHandler = set_exception_handler(function ($throwable) use (&$exceptionHandler) {
52+
if ($throwable instanceof \Exception) {
53+
$exception = $throwable;
54+
} else {
55+
static $refl = null;
56+
57+
if (null === $refl) {
58+
$refl = array();
59+
foreach (array('file', 'line', 'trace') as $prop) {
60+
$prop = new \ReflectionProperty('Exception', $prop);
61+
$prop->setAccessible(true);
62+
$refl[] = $prop;
63+
}
64+
}
65+
$exception = new \Exception($throwable->getMessage(), $throwable->getCode());
66+
foreach ($refl as $prop) {
67+
$prop->setValue($exception, $throwable->{'get'.$prop->name}());
68+
}
69+
}
70+
$exceptionHandler($exception);
71+
});
72+
}
4973
// CLI - display errors only if they're not already logged to STDERR
5074
} elseif ($displayErrors && (!ini_get('log_errors') || ini_get('error_log'))) {
5175
ini_set('display_errors', 1);

src/Symfony/Component/Debug/ErrorHandler.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,12 @@ public function handleFatal()
197197
$exceptionHandler = set_exception_handler(function () {});
198198
restore_exception_handler();
199199

200+
if (PHP_VERSION_ID >= 70000 && $exceptionHandler instanceof \Closure) {
201+
$reflector = new \ReflectionFunction($exceptionHandler);
202+
foreach ($reflector->getStaticVariables() as $exceptionHandler) {
203+
break;
204+
}
205+
}
200206
if (is_array($exceptionHandler) && $exceptionHandler[0] instanceof ExceptionHandler) {
201207
$level = isset($this->levels[$type]) ? $this->levels[$type] : $type;
202208
$message = sprintf('%s: %s in %s line %d', $level, $error['message'], $error['file'], $error['line']);

0 commit comments

Comments
 (0)
0