Skip to content

Commit 6fe4593

Browse files
authored
Merge pull request #2960 from teohhanhui/fix/messenger-nested-handler-failed-exception
Unwrap nested HandlerFailedException
2 parents 5207379 + 54662ee commit 6fe4593

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

src/EventListener/ExceptionListener.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,19 @@ public function onKernelException(GetResponseForExceptionEvent $event): void
4545
return;
4646
}
4747

48-
// unwrap the exception thrown in handler for Symfony Messenger >= 4.3
4948
$exception = $event->getException();
50-
if ($exception instanceof HandlerFailedException) {
51-
/** @var \Throwable $previousException */
52-
$previousException = $exception->getPrevious();
53-
if (!$previousException instanceof \Exception) {
54-
throw $previousException;
55-
}
5649

57-
$event->setException($previousException);
50+
// unwrap the exception thrown in handler for Symfony Messenger >= 4.3
51+
while ($exception instanceof HandlerFailedException) {
52+
/** @var \Throwable $exception */
53+
$exception = $exception->getPrevious();
54+
if (!$exception instanceof \Exception) {
55+
throw $exception;
56+
}
5857
}
5958

59+
$event->setException($exception);
60+
6061
$this->exceptionListener->onKernelException($event);
6162
}
6263
}

tests/EventListener/ExceptionListenerTest.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ public function testOnKernelException(Request $request)
3535
$kernel->handle(Argument::type(Request::class), HttpKernelInterface::SUB_REQUEST, false)->willReturn(new Response())->shouldBeCalled();
3636

3737
$eventProphecy = $this->prophesize(GetResponseForExceptionEvent::class);
38-
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();
39-
$eventProphecy->getException()->willReturn(new \Exception())->shouldBeCalled();
40-
$eventProphecy->getKernel()->willReturn($kernel)->shouldBeCalled();
38+
$eventProphecy->getRequest()->willReturn($request);
39+
$eventProphecy->getException()->willReturn(new \Exception());
40+
$eventProphecy->setException(Argument::type(\Exception::class))->will(function () {});
41+
$eventProphecy->getKernel()->willReturn($kernel);
4142
$eventProphecy->setResponse(Argument::type(Response::class))->shouldBeCalled();
4243

4344
$listener = new ExceptionListener('foo:bar');
@@ -55,7 +56,8 @@ public function getRequest()
5556
public function testDoNothingWhenNotAnApiCall()
5657
{
5758
$eventProphecy = $this->prophesize(GetResponseForExceptionEvent::class);
58-
$eventProphecy->getRequest()->willReturn(new Request())->shouldBeCalled();
59+
$eventProphecy->getRequest()->willReturn(new Request());
60+
$eventProphecy->setResponse(Argument::type(Response::class))->shouldNotBeCalled();
5961

6062
$listener = new ExceptionListener('foo:bar');
6163
$listener->onKernelException($eventProphecy->reveal());
@@ -67,7 +69,8 @@ public function testDoNothingWhenHtmlRequested()
6769
$request->setRequestFormat('html');
6870

6971
$eventProphecy = $this->prophesize(GetResponseForExceptionEvent::class);
70-
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();
72+
$eventProphecy->getRequest()->willReturn($request);
73+
$eventProphecy->setResponse(Argument::type(Response::class))->shouldNotBeCalled();
7174

7275
$listener = new ExceptionListener('foo:bar');
7376
$listener->onKernelException($eventProphecy->reveal());

0 commit comments

Comments
 (0)