Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit eb45bd7

Browse filesBrowse files
[HttpKernel] fix how configuring log-level and status-code by exception works
1 parent 9870e7e commit eb45bd7
Copy full SHA for eb45bd7

File tree

2 files changed

+19
-7
lines changed
Filter options

2 files changed

+19
-7
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/EventListener/ErrorListener.php
+13-7Lines changed: 13 additions & 7 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,13 +47,25 @@ public function logKernelException(ExceptionEvent $event)
4647
{
4748
$throwable = $event->getThrowable();
4849
$logLevel = null;
50+
4951
foreach ($this->exceptionsMapping as $class => $config) {
5052
if ($throwable instanceof $class && $config['log_level']) {
5153
$logLevel = $config['log_level'];
5254
break;
5355
}
5456
}
5557

58+
foreach ($this->exceptionsMapping as $class => $config) {
59+
if ($throwable instanceof $class && $config['status_code']) {
60+
if (!$throwable instanceof HttpExceptionInterface || $throwable->getStatusCode() !== $config['status_code']) {
61+
$headers = $throwable instanceof HttpExceptionInterface ? $throwable->getHeaders() : [];
62+
$throwable = new HttpException($config['status_code'], $throwable->getMessage(), $throwable, $headers);
63+
$event->setThrowable($throwable);
64+
}
65+
break;
66+
}
67+
}
68+
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']);
94-
break;
95-
}
96-
}
97-
98104
$event->setResponse($response);
99105

100106
if ($this->debug) {

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Tests/EventListener/ErrorListenerTest.php
+6Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.