Skip to content

Commit fbf6d92

Browse files
[HttpKernel] fix how configuring log-level and status-code by exception works
1 parent 9870e7e commit fbf6d92

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
2020
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
2121
use Symfony\Component\HttpKernel\Event\ResponseEvent;
22+
use Symfony\Component\HttpKernel\Exception\HttpException;
2223
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
2324
use Symfony\Component\HttpKernel\HttpKernelInterface;
2425
use Symfony\Component\HttpKernel\KernelEvents;
@@ -46,9 +47,19 @@ public function logKernelException(ExceptionEvent $event)
4647
{
4748
$throwable = $event->getThrowable();
4849
$logLevel = null;
50+
4951
foreach ($this->exceptionsMapping as $class => $config) {
50-
if ($throwable instanceof $class && $config['log_level']) {
52+
if (!$throwable instanceof $class) {
53+
continue;
54+
}
55+
if ($config['log_level'] && null === $logLevel) {
5156
$logLevel = $config['log_level'];
57+
}
58+
if ($config['status_code'] && $event->getThrowable() === $throwable) {
59+
$throwable = new HttpException($config['status_code'], $throwable->getMessage(), $throwable);
60+
$event->setThrowable($throwable);
61+
}
62+
if (null !== $logLevel && $event->getThrowable() !== $throwable) {
5263
break;
5364
}
5465
}
@@ -88,13 +99,6 @@ public function onKernelException(ExceptionEvent $event)
8899
throw $e;
89100
}
90101

91-
foreach ($this->exceptionsMapping as $exception => $config) {
92-
if ($throwable instanceof $exception && $config['status_code']) {
93-
$response->setStatusCode($config['status_code']);
94-
break;
95-
}
96-
}
97-
98102
$event->setResponse($response);
99103

100104
if ($this->debug) {

src/Symfony/Component/HttpKernel/Tests/EventListener/ErrorListenerTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
2323
use Symfony\Component\HttpKernel\Event\ResponseEvent;
2424
use Symfony\Component\HttpKernel\EventListener\ErrorListener;
25+
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
2526
use Symfony\Component\HttpKernel\HttpKernelInterface;
2627
use Symfony\Component\HttpKernel\KernelEvents;
2728
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
@@ -231,6 +232,11 @@ class TestKernel implements HttpKernelInterface
231232
{
232233
public function handle(Request $request, $type = self::MAIN_REQUEST, $catch = true): Response
233234
{
235+
$e = $request->attributes->get('exception');
236+
if ($e instanceof HttpExceptionInterface) {
237+
return new Response('foo', $e->getStatusCode(), $e->getHeaders());
238+
}
239+
234240
return new Response('foo');
235241
}
236242
}

0 commit comments

Comments
 (0)
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