bug #34411 [HttpKernel] Flatten "exception" controller argument if not typed (chalasr)
This PR was merged into the 4.4 branch.
Discussion
----------
[HttpKernel] Flatten "exception" controller argument if not typed
| Q | A
| ------------- | ---
| Branch? | 4.4
| Bug fix? | yes
| New feature? | no
| Deprecations? | no
| Tickets | -
| License | MIT
| Doc PR | -
Fixes a BC break and makes it easier for libs to support both debug & error-handler e.g. https://github.com/api-platform/core/pull/3246#discussion_r346912822
Commits
-------
585216a8e8
[HttpKernel] Flatten "exception" controller argument if not typed
This commit is contained in:
commit
1f73247092
@ -99,7 +99,7 @@ class ErrorListener implements EventSubscriberInterface
|
|||||||
$r = new \ReflectionFunction(\Closure::fromCallable($event->getController()));
|
$r = new \ReflectionFunction(\Closure::fromCallable($event->getController()));
|
||||||
$r = $r->getParameters()[$k] ?? null;
|
$r = $r->getParameters()[$k] ?? null;
|
||||||
|
|
||||||
if ($r && $r->hasType() && FlattenException::class === $r->getType()->getName()) {
|
if ($r && (!$r->hasType() || FlattenException::class === $r->getType()->getName())) {
|
||||||
$arguments = $event->getArguments();
|
$arguments = $event->getArguments();
|
||||||
$arguments[$k] = FlattenException::createFromThrowable($e);
|
$arguments[$k] = FlattenException::createFromThrowable($e);
|
||||||
$event->setArguments($arguments);
|
$event->setArguments($arguments);
|
||||||
|
@ -13,8 +13,8 @@ namespace Symfony\Component\HttpKernel\Tests\EventListener;
|
|||||||
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
use Symfony\Component\EventDispatcher\EventDispatcher;
|
|
||||||
use Symfony\Component\ErrorHandler\Exception\FlattenException;
|
use Symfony\Component\ErrorHandler\Exception\FlattenException;
|
||||||
|
use Symfony\Component\EventDispatcher\EventDispatcher;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use Symfony\Component\HttpKernel\Controller\ArgumentResolver;
|
use Symfony\Component\HttpKernel\Controller\ArgumentResolver;
|
||||||
@ -158,12 +158,11 @@ class ErrorListenerTest extends TestCase
|
|||||||
$this->assertFalse($dispatcher->hasListeners(KernelEvents::RESPONSE), 'CSP removal listener has been removed');
|
$this->assertFalse($dispatcher->hasListeners(KernelEvents::RESPONSE), 'CSP removal listener has been removed');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testOnControllerArguments()
|
/**
|
||||||
|
* @dataProvider controllerProvider
|
||||||
|
*/
|
||||||
|
public function testOnControllerArguments(callable $controller)
|
||||||
{
|
{
|
||||||
$controller = function (FlattenException $exception) {
|
|
||||||
return new Response('OK: '.$exception->getMessage());
|
|
||||||
};
|
|
||||||
|
|
||||||
$listener = new ErrorListener($controller, $this->createMock(LoggerInterface::class), true);
|
$listener = new ErrorListener($controller, $this->createMock(LoggerInterface::class), true);
|
||||||
|
|
||||||
$kernel = $this->createMock(HttpKernelInterface::class);
|
$kernel = $this->createMock(HttpKernelInterface::class);
|
||||||
@ -181,6 +180,23 @@ class ErrorListenerTest extends TestCase
|
|||||||
|
|
||||||
$this->assertSame('OK: foo', $event->getResponse()->getContent());
|
$this->assertSame('OK: foo', $event->getResponse()->getContent());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function controllerProvider()
|
||||||
|
{
|
||||||
|
yield [function (FlattenException $exception) {
|
||||||
|
return new Response('OK: '.$exception->getMessage());
|
||||||
|
}];
|
||||||
|
|
||||||
|
yield [function ($exception) {
|
||||||
|
$this->assertInstanceOf(FlattenException::class, $exception);
|
||||||
|
|
||||||
|
return new Response('OK: '.$exception->getMessage());
|
||||||
|
}];
|
||||||
|
|
||||||
|
yield [function (\Throwable $exception) {
|
||||||
|
return new Response('OK: '.$exception->getMessage());
|
||||||
|
}];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class TestLogger extends Logger implements DebugLoggerInterface
|
class TestLogger extends Logger implements DebugLoggerInterface
|
||||||
|
Reference in New Issue
Block a user