8000 [HttpKernel] fix how configuring log-level and status-code by excepti… · symfony/symfony@aa330c4 · GitHub
[go: up one dir, main page]

Skip to content

Commit aa330c4

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

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

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

Lines changed: 17 additions & 11 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\Eve 8000 nt\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,13 +47,25 @@ public function logKernelException(ExceptionEvent $event)
4647
{
4748
$throwable = $event->getThrowable();
4849
$logLevel = null;
49-
foreach ($this->exceptionsMapping as $class => $config) {
50-
if ($throwable instanceof $class && $config['log_level']) {
51-
$logLevel = $config['log_level'];
52-
break;
50+
51+
if (!$throwable instanceof HttpExceptionInterface) {
52+
foreach ($this->exceptionsMapping as $class => $config) {
53+
if (!$throwable instanceof $class) {
54+
continue;
55+
}
56+
if ($config['log_level'] && null === $logLevel) {
57+
$logLevel = $config['log_level'];
58+
}
59+
if ($config['status_code'] && $event->getThrowable() === $throwable) {
60+
$throwable = new HttpException($config['status_code'], $throwable->getMessage(), $throwable);
61+
}
62+
if (null !== $logLevel && $event->getThrowable() !== $throwable) {
63+
break;
64+
}
5365
}
5466
}
5567

68+
$event->setThrowable($throwable);
5669
$e = FlattenException::createFromThrowable($throwable);
5770

5871
$this->logException($throwable, sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', $e->getClass(), $e->getMessage(), $e->getFile(), $e->getLine()), $logLevel);
@@ -88,13 +101,6 @@ public function onKernelException(ExceptionEvent $event)
88101
throw $e;
89102
}
90103

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

100106
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)
0