From 6617be449e239a01e2273929b2b18b0666f6c279 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Wed, 15 Dec 2021 18:16:59 +0100 Subject: [PATCH] [HttpKernel] fix how configuring log-level and status-code by exception works --- .../EventListener/ErrorListener.php | 21 ++++++++++++------- .../Tests/EventListener/ErrorListenerTest.php | 6 ++++++ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/Symfony/Component/HttpKernel/EventListener/ErrorListener.php b/src/Symfony/Component/HttpKernel/EventListener/ErrorListener.php index bc4ca2f7d861d..9dc3871c25df7 100644 --- a/src/Symfony/Component/HttpKernel/EventListener/ErrorListener.php +++ b/src/Symfony/Component/HttpKernel/EventListener/ErrorListener.php @@ -19,6 +19,7 @@ use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent; use Symfony\Component\HttpKernel\Event\ExceptionEvent; use Symfony\Component\HttpKernel\Event\ResponseEvent; +use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\KernelEvents; @@ -46,6 +47,7 @@ public function logKernelException(ExceptionEvent $event) { $throwable = $event->getThrowable(); $logLevel = null; + foreach ($this->exceptionsMapping as $class => $config) { if ($throwable instanceof $class && $config['log_level']) { $logLevel = $config['log_level']; @@ -53,6 +55,18 @@ public function logKernelException(ExceptionEvent $event) } } + foreach ($this->exceptionsMapping as $class => $config) { + if (!$throwable instanceof $class || !$config['status_code']) { + continue; + } + if (!$throwable instanceof HttpExceptionInterface || $throwable->getStatusCode() !== $config['status_code']) { + $headers = $throwable instanceof HttpExceptionInterface ? $throwable->getHeaders() : []; + $throwable = new HttpException($config['status_code'], $throwable->getMessage(), $throwable, $headers); + $event->setThrowable($throwable); + } + break; + } + $e = FlattenException::createFromThrowable($throwable); $this->logException($throwable, sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', $e->getClass(), $e->getMessage(), $e->getFile(), $e->getLine()), $logLevel); @@ -88,13 +102,6 @@ public function onKernelException(ExceptionEvent $event) throw $e; } - foreach ($this->exceptionsMapping as $exception => $config) { - if ($throwable instanceof $exception && $config['status_code']) { - $response->setStatusCode($config['status_code']); - break; - } - } - $event->setResponse($response); if ($this->debug) { diff --git a/src/Symfony/Component/HttpKernel/Tests/EventListener/ErrorListenerTest.php b/src/Symfony/Component/HttpKernel/Tests/EventListener/ErrorListenerTest.php index 2c8d725466e20..00a6bde9004ce 100644 --- a/src/Symfony/Component/HttpKernel/Tests/EventListener/ErrorListenerTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/EventListener/ErrorListenerTest.php @@ -22,6 +22,7 @@ use Symfony\Component\HttpKernel\Event\ExceptionEvent; use Symfony\Component\HttpKernel\Event\ResponseEvent; use Symfony\Component\HttpKernel\EventListener\ErrorListener; +use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpKernel\Log\DebugLoggerInterface; @@ -231,6 +232,11 @@ class TestKernel implements HttpKernelInterface { public function handle(Request $request, $type = self::MAIN_REQUEST, $catch = true): Response { + $e = $request->attributes->get('exception'); + if ($e instanceof HttpExceptionInterface) { + return new Response('foo', $e->getStatusCode(), $e->getHeaders()); + } + return new Response('foo'); } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy