diff --git a/src/EventListener/ExceptionListener.php b/src/EventListener/ExceptionListener.php index c5ebbc60407..35f3f8f7752 100644 --- a/src/EventListener/ExceptionListener.php +++ b/src/EventListener/ExceptionListener.php @@ -45,18 +45,19 @@ public function onKernelException(GetResponseForExceptionEvent $event): void return; } - // unwrap the exception thrown in handler for Symfony Messenger >= 4.3 $exception = $event->getException(); - if ($exception instanceof HandlerFailedException) { - /** @var \Throwable $previousException */ - $previousException = $exception->getPrevious(); - if (!$previousException instanceof \Exception) { - throw $previousException; - } - $event->setException($previousException); + // unwrap the exception thrown in handler for Symfony Messenger >= 4.3 + while ($exception instanceof HandlerFailedException) { + /** @var \Throwable $exception */ + $exception = $exception->getPrevious(); + if (!$exception instanceof \Exception) { + throw $exception; + } } + $event->setException($exception); + $this->exceptionListener->onKernelException($event); } } diff --git a/tests/EventListener/ExceptionListenerTest.php b/tests/EventListener/ExceptionListenerTest.php index 72098a7fdbd..0cbbb0e48ed 100644 --- a/tests/EventListener/ExceptionListenerTest.php +++ b/tests/EventListener/ExceptionListenerTest.php @@ -35,9 +35,10 @@ public function testOnKernelException(Request $request) $kernel->handle(Argument::type(Request::class), HttpKernelInterface::SUB_REQUEST, false)->willReturn(new Response())->shouldBeCalled(); $eventProphecy = $this->prophesize(GetResponseForExceptionEvent::class); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); - $eventProphecy->getException()->willReturn(new \Exception())->shouldBeCalled(); - $eventProphecy->getKernel()->willReturn($kernel)->shouldBeCalled(); + $eventProphecy->getRequest()->willReturn($request); + $eventProphecy->getException()->willReturn(new \Exception()); + $eventProphecy->setException(Argument::type(\Exception::class))->will(function () {}); + $eventProphecy->getKernel()->willReturn($kernel); $eventProphecy->setResponse(Argument::type(Response::class))->shouldBeCalled(); $listener = new ExceptionListener('foo:bar'); @@ -55,7 +56,8 @@ public function getRequest() public function testDoNothingWhenNotAnApiCall() { $eventProphecy = $this->prophesize(GetResponseForExceptionEvent::class); - $eventProphecy->getRequest()->willReturn(new Request())->shouldBeCalled(); + $eventProphecy->getRequest()->willReturn(new Request()); + $eventProphecy->setResponse(Argument::type(Response::class))->shouldNotBeCalled(); $listener = new ExceptionListener('foo:bar'); $listener->onKernelException($eventProphecy->reveal()); @@ -67,7 +69,8 @@ public function testDoNothingWhenHtmlRequested() $request->setRequestFormat('html'); $eventProphecy = $this->prophesize(GetResponseForExceptionEvent::class); - $eventProphecy->getRequest()->willReturn($request)->shouldBeCalled(); + $eventProphecy->getRequest()->willReturn($request); + $eventProphecy->setResponse(Argument::type(Response::class))->shouldNotBeCalled(); $listener = new ExceptionListener('foo:bar'); $listener->onKernelException($eventProphecy->reveal());