Skip to content

Commit d910fb0

Browse files
committed
Unwrap Messenger HandlerFailedException at the dispatch call site
1 parent 6fe4593 commit d910fb0

File tree

5 files changed

+61
-20
lines changed

5 files changed

+61
-20
lines changed

src/Bridge/Doctrine/EventListener/PublishMercureUpdatesListener.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use ApiPlatform\Core\Api\IriConverterInterface;
1717
use ApiPlatform\Core\Api\ResourceClassResolverInterface;
1818
use ApiPlatform\Core\Api\UrlGeneratorInterface;
19+
use ApiPlatform\Core\Bridge\Symfony\Messenger\DispatchTrait;
1920
use ApiPlatform\Core\Exception\InvalidArgumentException;
2021
use ApiPlatform\Core\Exception\RuntimeException;
2122
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
@@ -35,12 +36,12 @@
3536
*/
3637
final class PublishMercureUpdatesListener
3738
{
39+
use DispatchTrait;
3840
use ResourceClassInfoTrait;
3941

4042
private $iriConverter;
4143
private $resourceMetadataFactory;
4244
private $serializer;
43-
private $messageBus;
4445
private $publisher;
4546
private $expressionLanguage;
4647
private $createdEntities;
@@ -180,6 +181,6 @@ private function publishUpdate($entity, array $targets): void
180181
}
181182

182183
$update = new Update($iri, $data, $targets);
183-
$this->messageBus ? $this->messageBus->dispatch($update) : ($this->publisher)($update);
184+
$this->messageBus ? $this->dispatch($update) : ($this->publisher)($update);
184185
}
185186
}

src/Bridge/Symfony/Messenger/DataPersister.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
final class DataPersister implements ContextAwareDataPersisterInterface
3333
{
3434
use ClassInfoTrait;
35+
use DispatchTrait;
3536

3637
private $resourceMetadataFactory;
37-
private $messageBus;
3838

3939
public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, MessageBusInterface $messageBus)
4040
{
@@ -75,7 +75,7 @@ public function supports($data, array $context = []): bool
7575
*/
7676
public function persist($data, array $context = [])
7777
{
78-
$envelope = $this->messageBus->dispatch($data);
78+
$envelope = $this->dispatch($data);
7979

8080
$handledStamp = $envelope->last(HandledStamp::class);
8181
if (!$handledStamp instanceof HandledStamp) {
@@ -90,7 +90,7 @@ public function persist($data, array $context = [])
9090
*/
9191
public function remove($data, array $context = [])
9292
{
93-
$this->messageBus->dispatch(
93+
$this->dispatch(
9494
(new Envelope($data))
9595
->with(new RemoveStamp())
9696
);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Bridge\Symfony\Messenger;
15+
16+
use Symfony\Component\Messenger\Envelope;
17+
use Symfony\Component\Messenger\Exception\HandlerFailedException;
18+
use Symfony\Component\Messenger\MessageBusInterface;
19+
20+
/**
21+
* @internal
22+
*/
23+
trait DispatchTrait
24+
{
25+
/**
26+
* @var MessageBusInterface|null
27+
*/
28+
private $messageBus;
29+
30+
/**
31+
* @param object|Envelope $message
32+
*/
33+
private function dispatch($message)
34+
{
35+
if (!$this->messageBus instanceof MessageBusInterface) {
36+
throw new \InvalidArgumentException('The message bus is not set.');
37+
}
38+
39+
if (!class_exists(HandlerFailedException::class)) {
40+
return $this->messageBus->dispatch($message);
41+
}
42+
43+
try {
44+
return $this->messageBus->dispatch($message);
45+
} catch (HandlerFailedException $e) {
46+
// unwrap the exception thrown in handler for Symfony Messenger >= 4.3
47+
while ($e instanceof HandlerFailedException) {
48+
/** @var \Throwable $e */
49+
$e = $e->getPrevious();
50+
}
51+
52+
throw $e;
53+
}
54+
}
55+
}

src/EventListener/ExceptionListener.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Psr\Log\LoggerInterface;
1818
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
1919
use Symfony\Component\HttpKernel\EventListener\ExceptionListener as BaseExceptionListener;
20-
use Symfony\Component\Messenger\Exception\HandlerFailedException;
2120

2221
/**
2322
* Handles requests errors.
@@ -45,19 +44,6 @@ public function onKernelException(GetResponseForExceptionEvent $event): void
4544
return;
4645
}
4746

48-
$exception = $event->getException();
49-
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-
}
57-
}
58-
59-
$event->setException($exception);
60-
6147
$this->exceptionListener->onKernelException($event);
6248
}
6349
}

tests/EventListener/ExceptionListenerTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public function testOnKernelException(Request $request)
3737
$eventProphecy = $this->prophesize(GetResponseForExceptionEvent::class);
3838
$eventProphecy->getRequest()->willReturn($request);
3939
$eventProphecy->getException()->willReturn(new \Exception());
40-
$eventProphecy->setException(Argument::type(\Exception::class))->will(function () {});
4140
$eventProphecy->getKernel()->willReturn($kernel);
4241
$eventProphecy->setResponse(Argument::type(Response::class))->shouldBeCalled();
4342

0 commit comments

Comments
 (0)