Skip to content

Commit 1fd4b63

Browse files
committed
bug symfony#48120 [Messenger] Do not throw 'no handlers' exception when skipping handlers due to duplicate handling (wouterj)
This PR was merged into the 4.4 branch. Discussion ---------- [Messenger] Do not throw 'no handlers' exception when skipping handlers due to duplicate handling | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | n/a I'm not sure if this change is expected or not, but I found this [while investigating another bug](https://github.com/wouterj/sf-reproducer/tree/messenger-star-routing in the Messenger component. When you handle the same message twice, you'll get an `NoHandlerForMessageException` because all handlers are skipped. What would be the expected behavior? Maybe we have to throw a different exception when the same envelope is handled twice? Commits ------- 0e4455b [Messenger] Do not throw 'no handlers' exception when skipping due to duplicate handling
2 parents 3752e65 + 0e4455b commit 1fd4b63

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)