8000 feature #50911 [HttpKernel] Enhance exception if possible (lyrixx) · symfony/symfony@48b0d9d · GitHub
[go: up one dir, main page]

Skip to content
  • Commit 48b0d9d

    Browse files
    committed
    feature #50911 [HttpKernel] Enhance exception if possible (lyrixx)
    This PR was merged into the 6.4 branch. Discussion ---------- [HttpKernel] Enhance exception if possible | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | no | New feature? | restore an old feature | Deprecations? | no | Tickets | | License | MIT | Doc PR | I don't know if it's the best place to do that. I think in the kernel, it would be better, WDYT? Before ![image](https://github.com/symfony/symfony/assets/408368/e2a452aa-b669-4598-8b23-5366c5b80a74) After : ![image](https://github.com/symfony/symfony/assets/408368/0e30c50d-5c94-43db-a9e5-19be6ccd5f2b) Commits ------- 2bfb9d9 [HttpKernel] Enhance exception if possible
    2 parents 888054b + 2bfb9d9 commit 48b0d9d

    File tree

    4 files changed

    +38
    -13
    lines changed

    4 files changed

    +38
    -13
    lines changed

    src/Symfony/Component/ErrorHandler/ErrorHandler.php

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

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

    529522
    $exceptionHandler = $this->exceptionHandler;
    530523
    $this->exceptionHandler = [$this, 'renderException'];
    @@ -662,6 +655,21 @@ private function renderException(\Throwable $exception): void
    662655
    echo $exception->getAsString();
    663656
    }
    664657

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

    src/Symfony/Component/ErrorHandler/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

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

    Lines changed: 9 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -13,6 +13,7 @@
    1313

    1414
    use Psr\Log\LoggerInterface;
    1515
    use Psr\Log\LogLevel;
    16+
    use Symfony\Component\ErrorHandler\ErrorHandler;
    1617
    use Symfony\Component\ErrorHandler\Exception\FlattenException;
    1718
    use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    1819
    use Symfony\Component\HttpFoundation\Request;
    @@ -102,6 +103,14 @@ public function onKernelException(ExceptionEvent $event)
    102103
    }
    103104

    104105
    $throwable = $event->getThrowable();
    106+
    107+
    if ($exceptionHandler = set_exception_handler(var_dump(...))) {
    108+
    restore_exception_handler();
    109+
    if (\is_array($exceptionHandler) && $exceptionHandler[0] instanceof ErrorHandler) {
    110+
    $throwable = $exceptionHandler[0]->enhanceError($event->getThrowable());
    111+
    }
    112+
    }
    113+
    105114
    $request = $this->duplicateRequest($throwable, $event->getRequest());
    106115

    107116
    try {

    src/Symfony/Component/HttpKernel/composer.json

    Lines changed: 1 addition & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -18,7 +18,7 @@
    1818
    "require": {
    1919
    "php": ">=8.1",
    2020
    "symfony/deprecation-contracts": "^2.5|^3",
    21-
    "symfony/error-handler": "^6.3|^7.0",
    21+
    "symfony/error-handler": "^6.4|^7.0",
    2222
    "symfony/event-dispatcher": "^5.4|^6.0|^7.0",
    2323
    "symfony/http-foundation": "^6.2.7|^7.0",
    2424
    "symfony/polyfill-ctype": "^1.8",

    0 commit comments

    Comments
     (0)
    0