Skip to content

Commit 6768e8d

Browse files
Merge branch '5.4' into 6.0
* 5.4: [5.4] cs fixes [5.3] cs fixes [Cache] Fix saving items with no expiration through ProxyAdapter CS fixes [HttpClient] Fix tracing requests made after calling withOptions() [Cache] disable lock on CLI Revert "feature #41989 [Cache] make `LockRegistry` use semaphores when possible (nicolas-grekas)" [HttpKernel] fix how configuring log-level and status-code by exception works [VarDumper] add more "transient-on-macos" groups
2 parents 72cd822 + 52f997f commit 6768e8d

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

Controller/ControllerResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function getController(Request $request): callable|false
4747
if (isset($controller[0]) && \is_string($controller[0]) && isset($controller[1])) {
4848
try {
4949
$controller[0] = $this->instantiateController($controller[0]);
50-
} catch (\Error | \LogicException $e) {
50+
} catch (\Error|\LogicException $e) {
5151
if (\is_callable($controller)) {
5252
return $controller;
5353
}
@@ -109,7 +109,7 @@ protected function createController(string $controller): callable
109109

110110
try {
111111
$controller = [$this->instantiateController($class), $method];
112-
} catch (\Error | \LogicException $e) {
112+
} catch (\Error|\LogicException $e) {
113113
try {
114114
if ((new \ReflectionMethod($class, $method))->isStatic()) {
115115
return $class.'::'.$method;

EventListener/ErrorListener.php

Lines changed: 14 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,26 @@ 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+
continue;
61+
}
62+
if (!$throwable instanceof HttpExceptionInterface || $throwable->getStatusCode() !== $config['status_code']) {
63+
$headers = $throwable instanceof HttpExceptionInterface ? $throwable->getHeaders() : [];
64+
$throwable = new HttpException($config['status_code'], $throwable->getMessage(), $throwable, $headers);
65+
$event->setThrowable($throwable);
66+
}
67+
break;
68+
}
69+
5670
$e = FlattenException::createFromThrowable($throwable);
5771

5872
$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)
88102
throw $e;
89103
}
90104

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-
98105
$event->setResponse($response);
99106

100107
if ($this->debug) {

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)