[ErrorHandler] Forward \Throwable
This commit is contained in:
parent
51ffc18b6d
commit
62483ed305
@ -174,10 +174,8 @@ class Application extends BaseApplication
|
|||||||
if ($bundle instanceof Bundle) {
|
if ($bundle instanceof Bundle) {
|
||||||
try {
|
try {
|
||||||
$bundle->registerCommands($this);
|
$bundle->registerCommands($this);
|
||||||
} catch (\Exception $e) {
|
|
||||||
$this->registrationErrors[] = $e;
|
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
$this->registrationErrors[] = new FatalThrowableError($e);
|
$this->registrationErrors[] = $e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -192,10 +190,8 @@ class Application extends BaseApplication
|
|||||||
if (!isset($lazyCommandIds[$id])) {
|
if (!isset($lazyCommandIds[$id])) {
|
||||||
try {
|
try {
|
||||||
$this->add($container->get($id));
|
$this->add($container->get($id));
|
||||||
} catch (\Exception $e) {
|
|
||||||
$this->registrationErrors[] = $e;
|
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
$this->registrationErrors[] = new FatalThrowableError($e);
|
$this->registrationErrors[] = $e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -211,6 +207,10 @@ class Application extends BaseApplication
|
|||||||
(new SymfonyStyle($input, $output))->warning('Some commands could not be registered:');
|
(new SymfonyStyle($input, $output))->warning('Some commands could not be registered:');
|
||||||
|
|
||||||
foreach ($this->registrationErrors as $error) {
|
foreach ($this->registrationErrors as $error) {
|
||||||
|
if (!$error instanceof \Exception) {
|
||||||
|
$error = new FatalThrowableError($error);
|
||||||
|
}
|
||||||
|
|
||||||
$this->doRenderException($error, $output);
|
$this->doRenderException($error, $output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -127,7 +127,7 @@ class Application implements ResetInterface
|
|||||||
$output = new ConsoleOutput();
|
$output = new ConsoleOutput();
|
||||||
}
|
}
|
||||||
|
|
||||||
$renderException = function ($e) use ($output) {
|
$renderException = function (\Throwable $e) use ($output) {
|
||||||
if (!$e instanceof \Exception) {
|
if (!$e instanceof \Exception) {
|
||||||
$e = class_exists(FatalThrowableError::class) ? new FatalThrowableError($e) : (class_exists(LegacyFatalThrowableError::class) ? new LegacyFatalThrowableError($e) : new \ErrorException($e->getMessage(), $e->getCode(), E_ERROR, $e->getFile(), $e->getLine()));
|
$e = class_exists(FatalThrowableError::class) ? new FatalThrowableError($e) : (class_exists(LegacyFatalThrowableError::class) ? new LegacyFatalThrowableError($e) : new \ErrorException($e->getMessage(), $e->getCode(), E_ERROR, $e->getFile(), $e->getLine()));
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,6 @@ namespace Symfony\Component\ErrorHandler;
|
|||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
use Psr\Log\LogLevel;
|
use Psr\Log\LogLevel;
|
||||||
use Symfony\Component\ErrorHandler\Exception\FatalErrorException;
|
use Symfony\Component\ErrorHandler\Exception\FatalErrorException;
|
||||||
use Symfony\Component\ErrorHandler\Exception\FatalThrowableError;
|
|
||||||
use Symfony\Component\ErrorHandler\Exception\OutOfMemoryException;
|
use Symfony\Component\ErrorHandler\Exception\OutOfMemoryException;
|
||||||
use Symfony\Component\ErrorHandler\Exception\SilencedErrorContext;
|
use Symfony\Component\ErrorHandler\Exception\SilencedErrorContext;
|
||||||
use Symfony\Component\ErrorHandler\FatalErrorHandler\ClassNotFoundFatalErrorHandler;
|
use Symfony\Component\ErrorHandler\FatalErrorHandler\ClassNotFoundFatalErrorHandler;
|
||||||
@ -266,7 +265,7 @@ class ErrorHandler
|
|||||||
|
|
||||||
if ($flush) {
|
if ($flush) {
|
||||||
foreach ($this->bootstrappingLogger->cleanLogs() as $log) {
|
foreach ($this->bootstrappingLogger->cleanLogs() as $log) {
|
||||||
$type = $log[2]['exception'] instanceof \ErrorException ? $log[2]['exception']->getSeverity() : E_ERROR;
|
$type = ThrowableUtils::getSeverity($log[2]['exception']);
|
||||||
if (!isset($flush[$type])) {
|
if (!isset($flush[$type])) {
|
||||||
$this->bootstrappingLogger->log($log[0], $log[1], $log[2]);
|
$this->bootstrappingLogger->log($log[0], $log[1], $log[2]);
|
||||||
} elseif ($this->loggers[$type][0]) {
|
} elseif ($this->loggers[$type][0]) {
|
||||||
@ -281,7 +280,7 @@ class ErrorHandler
|
|||||||
/**
|
/**
|
||||||
* Sets a user exception handler.
|
* Sets a user exception handler.
|
||||||
*
|
*
|
||||||
* @param callable|null $handler A handler that will be called on Exception
|
* @param callable|null $handler A handler that must support \Throwable instances that will be called on Exception
|
||||||
*
|
*
|
||||||
* @return callable|null The previous exception handler
|
* @return callable|null The previous exception handler
|
||||||
*/
|
*/
|
||||||
@ -540,57 +539,64 @@ class ErrorHandler
|
|||||||
/**
|
/**
|
||||||
* Handles an exception by logging then forwarding it to another handler.
|
* Handles an exception by logging then forwarding it to another handler.
|
||||||
*
|
*
|
||||||
* @param \Exception|\Throwable $exception An exception to handle
|
* @param array $error An array as returned by error_get_last()
|
||||||
* @param array $error An array as returned by error_get_last()
|
|
||||||
*
|
*
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
public function handleException($exception, array $error = null)
|
public function handleException(\Throwable $exception, array $error = null)
|
||||||
{
|
{
|
||||||
if (null === $error) {
|
if (null === $error) {
|
||||||
self::$exitCode = 255;
|
self::$exitCode = 255;
|
||||||
}
|
}
|
||||||
if (!$exception instanceof \Exception) {
|
|
||||||
$exception = new FatalThrowableError($exception);
|
$type = ThrowableUtils::getSeverity($exception);
|
||||||
}
|
|
||||||
$type = $exception instanceof FatalErrorException ? $exception->getSeverity() : E_ERROR;
|
|
||||||
$handlerException = null;
|
$handlerException = null;
|
||||||
|
|
||||||
if (($this->loggedErrors & $type) || $exception instanceof FatalThrowableError) {
|
if (($this->loggedErrors & $type) || $exception instanceof \Error) {
|
||||||
if (false !== strpos($message = $exception->getMessage(), "class@anonymous\0")) {
|
if (false !== strpos($message = $exception->getMessage(), "class@anonymous\0")) {
|
||||||
$message = $this->parseAnonymousClass($message);
|
$message = $this->parseAnonymousClass($message);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($exception instanceof FatalErrorException) {
|
if ($exception instanceof FatalErrorException) {
|
||||||
if ($exception instanceof FatalThrowableError) {
|
$message = 'Fatal '.$message;
|
||||||
$error = [
|
|
||||||
'type' => $type,
|
|
||||||
'message' => $message,
|
|
||||||
'file' => $exception->getFile(),
|
|
||||||
'line' => $exception->getLine(),
|
|
||||||
];
|
|
||||||
} else {
|
|
||||||
$message = 'Fatal '.$message;
|
|
||||||
}
|
|
||||||
} elseif ($exception instanceof \ErrorException) {
|
} elseif ($exception instanceof \ErrorException) {
|
||||||
$message = 'Uncaught '.$message;
|
$message = 'Uncaught '.$message;
|
||||||
|
} elseif ($exception instanceof \Error) {
|
||||||
|
$error = [
|
||||||
|
'type' => $type,
|
||||||
|
'message' => $message,
|
||||||
|
'file' => $exception->getFile(),
|
||||||
|
'line' => $exception->getLine(),
|
||||||
|
];
|
||||||
|
$message = 'Uncaught Error: '.$message;
|
||||||
} else {
|
} else {
|
||||||
$message = 'Uncaught Exception: '.$message;
|
$message = 'Uncaught Exception: '.$message;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->loggedErrors & $type) {
|
if ($this->loggedErrors & $type) {
|
||||||
try {
|
try {
|
||||||
$this->loggers[$type][0]->log($this->loggers[$type][1], $message, ['exception' => $exception]);
|
$this->loggers[$type][0]->log($this->loggers[$type][1], $message, ['exception' => $exception]);
|
||||||
} catch (\Throwable $handlerException) {
|
} catch (\Throwable $handlerException) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// temporary until fatal error handlers rework
|
||||||
|
$originalException = $exception;
|
||||||
|
if (!$exception instanceof \Exception) {
|
||||||
|
$exception = new FatalErrorException($exception->getMessage(), $exception->getCode(), $type, $exception->getFile(), $exception->getLine(), null, true, $exception->getTrace());
|
||||||
|
}
|
||||||
|
|
||||||
if ($exception instanceof FatalErrorException && !$exception instanceof OutOfMemoryException && $error) {
|
if ($exception instanceof FatalErrorException && !$exception instanceof OutOfMemoryException && $error) {
|
||||||
foreach ($this->getFatalErrorHandlers() as $handler) {
|
foreach ($this->getFatalErrorHandlers() as $handler) {
|
||||||
if ($e = $handler->handleError($error, $exception)) {
|
if ($e = $handler->handleError($error, $exception)) {
|
||||||
$exception = $e;
|
$convertedException = $e;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$exception = $convertedException ?? $originalException;
|
||||||
$exceptionHandler = $this->exceptionHandler;
|
$exceptionHandler = $this->exceptionHandler;
|
||||||
if ((!\is_array($exceptionHandler) || !$exceptionHandler[0] instanceof self || 'sendPhpResponse' !== $exceptionHandler[1]) && !\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true)) {
|
if ((!\is_array($exceptionHandler) || !$exceptionHandler[0] instanceof self || 'sendPhpResponse' !== $exceptionHandler[1]) && !\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true)) {
|
||||||
$this->exceptionHandler = [$this, 'sendPhpResponse'];
|
$this->exceptionHandler = [$this, 'sendPhpResponse'];
|
||||||
|
@ -11,6 +11,8 @@
|
|||||||
|
|
||||||
namespace Symfony\Component\ErrorHandler\Exception;
|
namespace Symfony\Component\ErrorHandler\Exception;
|
||||||
|
|
||||||
|
use Symfony\Component\ErrorHandler\ThrowableUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fatal Throwable Error.
|
* Fatal Throwable Error.
|
||||||
*
|
*
|
||||||
@ -24,18 +26,10 @@ class FatalThrowableError extends FatalErrorException
|
|||||||
{
|
{
|
||||||
$this->originalClassName = \get_class($e);
|
$this->originalClassName = \get_class($e);
|
||||||
|
|
||||||
if ($e instanceof \ParseError) {
|
|
||||||
$severity = E_PARSE;
|
|
||||||
} elseif ($e instanceof \TypeError) {
|
|
||||||
$severity = E_RECOVERABLE_ERROR;
|
|
||||||
} else {
|
|
||||||
$severity = E_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
\ErrorException::__construct(
|
\ErrorException::__construct(
|
||||||
$e->getMessage(),
|
$e->getMessage(),
|
||||||
$e->getCode(),
|
$e->getCode(),
|
||||||
$severity,
|
ThrowableUtils::getSeverity($e),
|
||||||
$e->getFile(),
|
$e->getFile(),
|
||||||
$e->getLine(),
|
$e->getLine(),
|
||||||
$e->getPrevious()
|
$e->getPrevious()
|
||||||
|
@ -382,18 +382,19 @@ class ErrorHandlerTest extends TestCase
|
|||||||
restore_error_handler();
|
restore_error_handler();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testHandleException()
|
/**
|
||||||
|
* @dataProvider handleExceptionProvider
|
||||||
|
*/
|
||||||
|
public function testHandleException(string $expectedMessage, \Throwable $exception)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
|
$logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
|
||||||
$handler = ErrorHandler::register();
|
$handler = ErrorHandler::register();
|
||||||
|
|
||||||
$exception = new \Exception('foo');
|
$logArgCheck = function ($level, $message, $context) use ($expectedMessage, $exception) {
|
||||||
|
$this->assertSame($expectedMessage, $message);
|
||||||
$logArgCheck = function ($level, $message, $context) {
|
|
||||||
$this->assertSame('Uncaught Exception: foo', $message);
|
|
||||||
$this->assertArrayHasKey('exception', $context);
|
$this->assertArrayHasKey('exception', $context);
|
||||||
$this->assertInstanceOf(\Exception::class, $context['exception']);
|
$this->assertInstanceOf(\get_class($exception), $context['exception']);
|
||||||
};
|
};
|
||||||
|
|
||||||
$logger
|
$logger
|
||||||
@ -407,7 +408,7 @@ class ErrorHandlerTest extends TestCase
|
|||||||
try {
|
try {
|
||||||
$handler->handleException($exception);
|
$handler->handleException($exception);
|
||||||
$this->fail('Exception expected');
|
$this->fail('Exception expected');
|
||||||
} catch (\Exception $e) {
|
} catch (\Throwable $e) {
|
||||||
$this->assertSame($exception, $e);
|
$this->assertSame($exception, $e);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -422,6 +423,15 @@ class ErrorHandlerTest extends TestCase
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function handleExceptionProvider(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
['Uncaught Exception: foo', new \Exception('foo')],
|
||||||
|
['Uncaught Error: bar', new \Error('bar')],
|
||||||
|
['Uncaught ccc', new \ErrorException('ccc')],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
public function testBootstrappingLogger()
|
public function testBootstrappingLogger()
|
||||||
{
|
{
|
||||||
$bootLogger = new BufferingLogger();
|
$bootLogger = new BufferingLogger();
|
||||||
|
35
src/Symfony/Component/ErrorHandler/ThrowableUtils.php
Normal file
35
src/Symfony/Component/ErrorHandler/ThrowableUtils.php
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\ErrorHandler;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
class ThrowableUtils
|
||||||
|
{
|
||||||
|
public static function getSeverity(\Throwable $throwable): int
|
||||||
|
{
|
||||||
|
if ($throwable instanceof \ErrorException) {
|
||||||
|
return $throwable->getSeverity();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($throwable instanceof \ParseError) {
|
||||||
|
return E_PARSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($throwable instanceof \TypeError) {
|
||||||
|
return E_RECOVERABLE_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
return E_ERROR;
|
||||||
|
}
|
||||||
|
}
|
@ -16,6 +16,7 @@ use Symfony\Component\Console\ConsoleEvents;
|
|||||||
use Symfony\Component\Console\Event\ConsoleEvent;
|
use Symfony\Component\Console\Event\ConsoleEvent;
|
||||||
use Symfony\Component\Console\Output\ConsoleOutputInterface;
|
use Symfony\Component\Console\Output\ConsoleOutputInterface;
|
||||||
use Symfony\Component\ErrorHandler\ErrorHandler;
|
use Symfony\Component\ErrorHandler\ErrorHandler;
|
||||||
|
use Symfony\Component\ErrorHandler\Exception\FatalThrowableError;
|
||||||
use Symfony\Component\EventDispatcher\Event;
|
use Symfony\Component\EventDispatcher\Event;
|
||||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||||
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
|
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
|
||||||
@ -42,7 +43,7 @@ class DebugHandlersListener implements EventSubscriberInterface
|
|||||||
private $hasTerminatedWithException;
|
private $hasTerminatedWithException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param callable|null $exceptionHandler A handler that will be called on Exception
|
* @param callable|null $exceptionHandler A handler that must support \Throwable instances that will be called on Exception
|
||||||
* @param array|int $levels An array map of E_* to LogLevel::* or an integer bit field of E_* constants
|
* @param array|int $levels An array map of E_* to LogLevel::* or an integer bit field of E_* constants
|
||||||
* @param int|null $throwAt Thrown errors in a bit field of E_* constants, or null to keep the current value
|
* @param int|null $throwAt Thrown errors in a bit field of E_* constants, or null to keep the current value
|
||||||
* @param bool $scream Enables/disables screaming mode, where even silenced errors are logged
|
* @param bool $scream Enables/disables screaming mode, where even silenced errors are logged
|
||||||
@ -106,10 +107,15 @@ class DebugHandlersListener implements EventSubscriberInterface
|
|||||||
if (method_exists($kernel = $event->getKernel(), 'terminateWithException')) {
|
if (method_exists($kernel = $event->getKernel(), 'terminateWithException')) {
|
||||||
$request = $event->getRequest();
|
$request = $event->getRequest();
|
||||||
$hasRun = &$this->hasTerminatedWithException;
|
$hasRun = &$this->hasTerminatedWithException;
|
||||||
$this->exceptionHandler = static function (\Exception $e) use ($kernel, $request, &$hasRun) {
|
$this->exceptionHandler = static function (\Throwable $e) use ($kernel, $request, &$hasRun) {
|
||||||
if ($hasRun) {
|
if ($hasRun) {
|
||||||
throw $e;
|
throw $e;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!$e instanceof \Exception) {
|
||||||
|
$e = new FatalThrowableError($e);
|
||||||
|
}
|
||||||
|
|
||||||
$hasRun = true;
|
$hasRun = true;
|
||||||
$kernel->terminateWithException($e, $request);
|
$kernel->terminateWithException($e, $request);
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user