Skip to content

Commit 1de20c4

Browse files
committed
[HttpKernel] Enhance exception if possible
1 parent ce64303 commit 1de20c4

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

ErrorHandler.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -516,14 +516,7 @@ public function handleException(\Throwable $exception): void
516516
}
517517
}
518518

519-
if (!$exception instanceof OutOfMemoryError) {
520-
foreach ($this->getErrorEnhancers() as $errorEnhancer) {
521-
if ($e = $errorEnhancer->enhance($exception)) {
522-
$exception = $e;
523-
break;
524-
}
525-
}
526-
}
519+
$exception = $this->enhanceError($exception);
527520

528521
$exceptionHandler = $this->exceptionHandler;
529522
$this->exceptionHandler = [$this, 'renderException'];
@@ -661,6 +654,21 @@ private function renderException(\Throwable $exception): void
661654
echo $exception->getAsString();
662655
}
663656

657+
public function enhanceError(\Throwable $exception): \Throwable
658+
{
659+
if ($exception instanceof OutOfMemoryError) {
660+
return $exception;
661+
}
662+
663+
foreach ($this->getErrorEnhancers() as $errorEnhancer) {
664+
if ($e = $errorEnhancer->enhance($exception)) {
665+
return $e;
666+
}
667+
}
668+
669+
return $exception;
670+
}
671+
664672
/**
665673
* Override this method if you want to define more error enhancers.
666674
*

Tests/ErrorHandlerTest.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,13 +363,14 @@ public function testHandleDeprecation()
363363
/**
364364
* @dataProvider handleExceptionProvider
365365
*/
366-
public function testHandleException(string $expectedMessage, \Throwable $exception)
366+
public function testHandleException(string $expectedMessage, \Throwable $exception, string $enhancedMessage = null)
367367
{
368368
try {
369369
$logger = $this->createMock(LoggerInterface::class);
370370
$handler = ErrorHandler::register();
371371

372372
$logArgCheck = function ($level, $message, $context) use ($expectedMessage, $exception) {
373+
$this->assertSame('critical', $level);
373374
$this->assertSame($expectedMessage, $message);
374375
$this->assertArrayHasKey('exception', $context);
375376
$this->assertInstanceOf($exception::class, $context['exception']);
@@ -388,11 +389,13 @@ public function testHandleException(string $expectedMessage, \Throwable $excepti
388389
$handler->handleException($exception);
389390
$this->fail('Exception expected');
390391
} catch (\Throwable $e) {
391-
$this->assertSame($exception, $e);
392+
$this->assertInstanceOf($exception::class, $e);
393+
$this->assertSame($enhancedMessage ?? $exception->getMessage(), $e->getMessage());
392394
}
393395

394-
$handler->setExceptionHandler(function ($e) use ($exception) {
395-
$this->assertSame($exception, $e);
396+
$handler->setExceptionHandler(function ($e) use ($exception, $enhancedMessage) {
397+
$this->assertInstanceOf($exception::class, $e);
398+
$this->assertSame($enhancedMessage ?? $exception->getMessage(), $e->getMessage());
396399
});
397400

398401
$handler->handleException($exception);
@@ -412,6 +415,11 @@ public static function handleExceptionProvider(): array
412415
})::class.' bar')],
413416
['Uncaught Error: bar', new \Error('bar')],
414417
['Uncaught ccc', new \ErrorException('ccc')],
418+
[
419+
'Uncaught Error: Class "App\Controller\ClassDoesNotExist" not found',
420+
new \Error('Class "App\Controller\ClassDoesNotExist" not found'),
421+
"Attempted to load class \"ClassDoesNotExist\" from namespace \"App\Controller\".\nDid you forget a \"use\" statement for another namespace?",
422+
],
415423
];
416424
}
417425

0 commit comments

Comments
 (0)