Skip to content

Commit

Permalink
bug #34411 [HttpKernel] Flatten "exception" controller argument if no…
Browse files Browse the repository at this point in the history
…t 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. api-platform/core#3246 (comment)

Commits
-------

585216a [HttpKernel] Flatten "exception" controller argument if not typed
  • Loading branch information
nicolas-grekas committed Nov 16, 2019
2 parents 278dcfb + 585216a commit 1f73247
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
Expand Up @@ -99,7 +99,7 @@ public function onControllerArguments(ControllerArgumentsEvent $event)
$r = new \ReflectionFunction(\Closure::fromCallable($event->getController()));
$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[$k] = FlattenException::createFromThrowable($e);
$event->setArguments($arguments);
Expand Down
Expand Up @@ -13,8 +13,8 @@

use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\ErrorHandler\Exception\FlattenException;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Controller\ArgumentResolver;
Expand Down Expand Up @@ -158,12 +158,11 @@ public function testCSPHeaderIsRemoved()
$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);

$kernel = $this->createMock(HttpKernelInterface::class);
Expand All @@ -181,6 +180,23 @@ public function testOnControllerArguments()

$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
Expand Down

0 comments on commit 1f73247

Please sign in to comment.