Skip to content

Commit f9c9b5c

Browse files
committed
[ErrorHandler] Forward \Throwable
1 parent d94c4b4 commit f9c9b5c

File tree

8 files changed

+96
-46
lines changed

8 files changed

+96
-46
lines changed

src/Symfony/Bundle/FrameworkBundle/Console/Application.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Component\Console\Style\SymfonyStyle;
2222
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
2323
use Symfony\Component\ErrorHandler\Exception\FatalThrowableError;
24+
use Symfony\Component\ErrorHandler\ThrowableUtils;
2425
use Symfony\Component\HttpKernel\Bundle\Bundle;
2526
use Symfony\Component\HttpKernel\Kernel;
2627
use Symfony\Component\HttpKernel\KernelInterface;
@@ -174,10 +175,8 @@ protected function registerCommands()
174175
if ($bundle instanceof Bundle) {
175176
try {
176177
$bundle->registerCommands($this);
177-
} catch (\Exception $e) {
178-
$this->registrationErrors[] = $e;
179178
} catch (\Throwable $e) {
180-
$this->registrationErrors[] = new FatalThrowableError($e);
179+
$this->registrationErrors[] = $e;
181180
}
182181
}
183182
}
@@ -192,10 +191,8 @@ protected function registerCommands()
192191
if (!isset($lazyCommandIds[$id])) {
193192
try {
194193
$this->add($container->get($id));
195-
} catch (\Exception $e) {
196-
$this->registrationErrors[] = $e;
197194
} catch (\Throwable $e) {
198-
$this->registrationErrors[] = new FatalThrowableError($e);
195+
$this->registrationErrors[] = $e;
199196
}
200197
}
201198
}
@@ -211,6 +208,10 @@ private function renderRegistrationErrors(InputInterface $input, OutputInterface
211208
(new SymfonyStyle($input, $output))->warning('Some commands could not be registered:');
212209

213210
foreach ($this->registrationErrors as $error) {
211+
if (!$error instanceof \Exception) {
212+
$error = new FatalThrowableError($error);
213+
}
214+
214215
$this->doRenderException($error, $output);
215216
}
216217
}

src/Symfony/Component/Console/Application.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public function run(InputInterface $input = null, OutputInterface $output = null
127127
$output = new ConsoleOutput();
128128
}
129129

130-
$renderException = function ($e) use ($output) {
130+
$renderException = function (\Throwable $e) use ($output) {
131131
if (!$e instanceof \Exception) {
132132
$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()));
133133
}

src/Symfony/Component/ErrorHandler/ErrorHandler.php

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Psr\Log\LoggerInterface;
1515
use Psr\Log\LogLevel;
1616
use Symfony\Component\ErrorHandler\Exception\FatalErrorException;
17-
use Symfony\Component\ErrorHandler\Exception\FatalThrowableError;
1817
use Symfony\Component\ErrorHandler\Exception\OutOfMemoryException;
1918
use Symfony\Component\ErrorHandler\Exception\SilencedErrorContext;
2019
use Symfony\Component\ErrorHandler\FatalErrorHandler\ClassNotFoundFatalErrorHandler;
@@ -236,7 +235,7 @@ public function setLoggers(array $loggers): array
236235

237236
if ($flush) {
238237
foreach ($this->bootstrappingLogger->cleanLogs() as $log) {
239-
$type = $log[2]['exception'] instanceof \ErrorException ? $log[2]['exception']->getSeverity() : E_ERROR;
238+
$type = $log[2]['exception'] instanceof \ErrorException ? $log[2]['exception']->getSeverity() : ThrowableUtils::getSeverity($log[2]['exception']);
240239
if (!isset($flush[$type])) {
241240
$this->bootstrappingLogger->log($log[0], $log[1], $log[2]);
242241
} elseif ($this->loggers[$type][0]) {
@@ -252,6 +251,7 @@ public function setLoggers(array $loggers): array
252251
* Sets a user exception handler.
253252
*
254253
* @param callable|null $handler A handler that will be called on Exception
254+
* It must support \Throwable instances.
255255
*
256256
* @return callable|null The previous exception handler
257257
*/
@@ -510,57 +510,65 @@ public function handleError(int $type, string $message, string $file, int $line)
510510
/**
511511
* Handles an exception by logging then forwarding it to another handler.
512512
*
513-
* @param \Exception|\Throwable $exception An exception to handle
514-
* @param array $error An array as returned by error_get_last()
513+
* @param \Throwable $exception An exception to handle
514+
* @param array $error An array as returned by error_get_last()
515515
*
516516
* @internal
517517
*/
518-
public function handleException($exception, array $error = null)
518+
public function handleException(\Throwable $exception, array $error = null)
519519
{
520520
if (null === $error) {
521521
self::$exitCode = 255;
522522
}
523-
if (!$exception instanceof \Exception) {
524-
$exception = new FatalThrowableError($exception);
525-
}
526-
$type = $exception instanceof FatalErrorException ? $exception->getSeverity() : E_ERROR;
523+
524+
$type = $exception instanceof FatalErrorException ? $exception->getSeverity() : ThrowableUtils::getSeverity($exception);
527525
$handlerException = null;
528526

529-
if (($this->loggedErrors & $type) || $exception instanceof FatalThrowableError) {
527+
if (($this->loggedErrors & $type) || $exception instanceof \Error) {
530528
if (false !== strpos($message = $exception->getMessage(), "class@anonymous\0")) {
531529
$message = $this->parseAnonymousClass($message);
532530
}
531+
533532
if ($exception instanceof FatalErrorException) {
534-
if ($exception instanceof FatalThrowableError) {
535-
$error = [
536-
'type' => $type,
537-
'message' => $message,
538-
'file' => $exception->getFile(),
539-
'line' => $exception->getLine(),
540-
];
541-
} else {
542-
$message = 'Fatal '.$message;
543-
}
533+
$message = 'Fatal '.$message;
544534
} elseif ($exception instanceof \ErrorException) {
545535
$message = 'Uncaught '.$message;
536+
} elseif ($exception instanceof \Error) {
537+
$error = [
538+
'type' => $type,
539+
'message' => $message,
540+
'file' => $exception->getFile(),
541+
'line' => $exception->getLine(),
542+
];
543+
$message = 'Uncaught Error: '.$message;
546544
} else {
547545
$message = 'Uncaught Exception: '.$message;
548546
}
549547
}
548+
550549
if ($this->loggedErrors & $type) {
551550
try {
552551
$this->loggers[$type][0]->log($this->loggers[$type][1], $message, ['exception' => $exception]);
553552
} catch (\Throwable $handlerException) {
554553
}
555554
}
555+
556+
// temporary until fatal error handlers rework
557+
$originalException = $exception;
558+
if (!$exception instanceof \Exception) {
559+
$exception = new FatalErrorException($exception->getMessage(), $exception->getCode(), $type, $exception->getFile(), $exception->getLine(), null, true, $exception->getTrace());
560+
}
561+
556562
if ($exception instanceof FatalErrorException && !$exception instanceof OutOfMemoryException && $error) {
557563
foreach ($this->getFatalErrorHandlers() as $handler) {
558564
if ($e = $handler->handleError($error, $exception)) {
559-
$exception = $e;
565+
$convertedException = $e;
560566
break;
561567
}
562568
}
563569
}
570+
571+
$exception = $convertedException ?? $originalException;
564572
$exceptionHandler = $this->exceptionHandler;
565573
$this->exceptionHandler = null;
566574
try {

src/Symfony/Component/ErrorHandler/Exception/FatalThrowableError.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\ErrorHandler\Exception;
1313

14+
use Symfony\Component\ErrorHandler\ThrowableUtils;
15+
1416
/**
1517
* Fatal Throwable Error.
1618
*
@@ -24,18 +26,10 @@ public function __construct(\Throwable $e)
2426
{
2527
$this->originalClassName = \get_class($e);
2628

27-
if ($e instanceof \ParseError) {
28-
$severity = E_PARSE;
29-
} elseif ($e instanceof \TypeError) {
30-
$severity = E_RECOVERABLE_ERROR;
31-
} else {
32-
$severity = E_ERROR;
33-
}
34-
3529
\ErrorException::__construct(
3630
$e->getMessage(),
3731
$e->getCode(),
38-
$severity,
32+
ThrowableUtils::getSeverity($e),
3933
$e->getFile(),
4034
$e->getLine(),
4135
$e->getPrevious()

src/Symfony/Component/ErrorHandler/Tests/ErrorHandlerTest.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\ErrorHandler\BufferingLogger;
1818
use Symfony\Component\ErrorHandler\ErrorHandler;
1919
use Symfony\Component\ErrorHandler\Exception\SilencedErrorContext;
20+
use Symfony\Component\ErrorHandler\Tests\Fixtures\CustomThrowable;
2021
use Symfony\Component\ErrorHandler\Tests\Fixtures\ErrorHandlerThatUsesThePreviousOne;
2122
use Symfony\Component\ErrorHandler\Tests\Fixtures\LoggerThatSetAnErrorHandler;
2223

@@ -327,18 +328,19 @@ public function testHandleDeprecation()
327328
restore_error_handler();
328329
}
329330

330-
public function testHandleException()
331+
/**
332+
* @dataProvider handleExceptionProvider
333+
*/
334+
public function testHandleExceptiossssn(string $expectedMessage, \Throwable $exception)
331335
{
332336
try {
333337
$logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
334338
$handler = ErrorHandler::register();
335339

336-
$exception = new \Exception('foo');
337-
338-
$logArgCheck = function ($level, $message, $context) {
339-
$this->assertSame('Uncaught Exception: foo', $message);
340+
$logArgCheck = function ($level, $message, $context) use ($expectedMessage, $exception) {
341+
$this->assertSame($expectedMessage, $message);
340342
$this->assertArrayHasKey('exception', $context);
341-
$this->assertInstanceOf(\Exception::class, $context['exception']);
343+
$this->assertInstanceOf(get_class($exception), $context['exception']);
342344
};
343345

344346
$logger
@@ -352,7 +354,7 @@ public function testHandleException()
352354
try {
353355
$handler->handleException($exception);
354356
$this->fail('Exception expected');
355-
} catch (\Exception $e) {
357+
} catch (\Throwable $e) {
356358
$this->assertSame($exception, $e);
357359
}
358360

@@ -367,6 +369,15 @@ public function testHandleException()
367369
}
368370
}
369371

372+
public function handleExceptionProvider(): array
373+
{
374+
return [
375+
['Uncaught Exception: foo', new \Exception('foo')],
376+
['Uncaught Error: bar', new \Error('bar')],
377+
['Uncaught ccc', new \ErrorException('ccc')],
378+
];
379+
}
380+
370381
public function testBootstrappingLogger()
371382
{
372383
$bootLogger = new BufferingLogger();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\ErrorHandler;
13+
14+
/**
15+
* @internal
16+
*/
17+
class ThrowableUtils
18+
{
19+
public static function getSeverity(\Throwable $throwable): int
20+
{
21+
if ($throwable instanceof \ParseError) {
22+
return E_PARSE;
23+
} elseif ($throwable instanceof \TypeError) {
24+
return E_RECOVERABLE_ERROR;
25+
}
26+
27+
return E_ERROR;
28+
}
29+
}

src/Symfony/Component/ErrorRenderer/Exception/FlattenException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public static function createFromThrowable(\Throwable $exception, int $statusCod
6464
$e->setStatusCode($statusCode);
6565
$e->setHeaders($headers);
6666
$e->setTraceFromThrowable($exception);
67-
$e->setClass($exception instanceof FatalThrowableError ? $exception->getOriginalClassName() : \get_class($exception));
67+
$e->setClass($exception instanceof FatalThrowableError ? $exception->getOriginalClassName() : get_class($exception));
6868
$e->setFile($exception->getFile());
6969
$e->setLine($exception->getLine());
7070

src/Symfony/Component/HttpKernel/EventListener/DebugHandlersListener.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Console\Event\ConsoleEvent;
1717
use Symfony\Component\Console\Output\ConsoleOutputInterface;
1818
use Symfony\Component\ErrorHandler\ErrorHandler;
19+
use Symfony\Component\ErrorHandler\Exception\FatalThrowableError;
1920
use Symfony\Component\ErrorRenderer\ErrorRenderer;
2021
use Symfony\Component\ErrorRenderer\ErrorRenderer\HtmlErrorRenderer;
2122
use Symfony\Component\ErrorRenderer\Exception\ErrorRendererNotFoundException;
@@ -51,6 +52,7 @@ class DebugHandlersListener implements EventSubscriberInterface
5152

5253
/**
5354
* @param callable|null $exceptionHandler A handler that will be called on Exception
55+
* It must support \Throwable instances.
5456
* @param LoggerInterface|null $logger A PSR-3 logger
5557
* @param array|int $levels An array map of E_* to LogLevel::* or an integer bit field of E_* constants
5658
* @param int|null $throwAt Thrown errors in a bit field of E_* constants, or null to keep the current value
@@ -117,10 +119,15 @@ public function configure(Event $event = null)
117119
if (method_exists($kernel = $event->getKernel(), 'terminateWithException')) {
118120
$request = $event->getRequest();
119121
$hasRun = &$this->hasTerminatedWithException;
120-
$this->exceptionHandler = static function (\Exception $e) use ($kernel, $request, &$hasRun) {
122+
$this->exceptionHandler = static function (\Throwable $e) use ($kernel, $request, &$hasRun) {
121123
if ($hasRun) {
122124
throw $e;
123125
}
126+
127+
if (!$e instanceof \Exception) {
128+
$e = new FatalThrowableError($e);
129+
}
130+
124131
$hasRun = true;
125132
$kernel->terminateWithException($e, $request);
126133
};

0 commit comments

Comments
 (0)