Skip to content

Commit 0e4455b

Browse files
committed
[Messenger] Do not throw 'no handlers' exception when skipping due to duplicate handling
1 parent ad0e25f commit 0e4455b

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/Symfony/Component/Messenger/Middleware/HandleMessageMiddleware.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope
5353
];
5454

5555
$exceptions = [];
56+
$alreadyHandled = false;
5657
foreach ($this->handlersLocator->getHandlers($envelope) as $handlerDescriptor) {
5758
if ($this->messageHasAlreadyBeenHandled($envelope, $handlerDescriptor)) {
59+
$alreadyHandled = true;
5860
continue;
5961
}
6062

@@ -68,7 +70,7 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope
6870
}
6971
}
7072

71-
if (null === $handler) {
73+
if (null === $handler && !$alreadyHandled) {
7274
if (!$this->allowNoHandlers) {
7375
throw new NoHandlerForMessageException(sprintf('No handler for message "%s".', $context['class']));
7476
}

src/Symfony/Component/Messenger/Tests/Middleware/HandleMessageMiddlewareTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,24 @@ public function testThrowsNoHandlerException()
123123
$middleware->handle(new Envelope(new DummyMessage('Hey')), new StackMiddleware());
124124
}
125125

126+
public function testMessageAlreadyHandled()
127+
{
128+
$handler = $this->createPartialMock(HandleMessageMiddlewareTestCallable::class, ['__invoke']);
129+
130+
$middleware = new HandleMessageMiddleware(new HandlersLocator([
131+
DummyMessage::class => [$handler],
132+
]));
133+
134+
$envelope = new Envelope(new DummyMessage('Hey'));
135+
136+
$envelope = $middleware->handle($envelope, $this->getStackMock());
137+
$handledStamp = $envelope->all(HandledStamp::class);
138+
139+
$envelope = $middleware->handle($envelope, $this->getStackMock());
140+
141+
$this->assertSame($envelope->all(HandledStamp::class), $handledStamp);
142+
}
143+
126144
public function testAllowNoHandlers()
127145
{
128146
$middleware = new HandleMessageMiddleware(new HandlersLocator([]), true);

0 commit comments

Comments
 (0)