Skip to content

Commit 3035e6b

Browse files
authored
fix: post hook invalid when symfony messenger sender throws exception (#267)
* fix: post hook invalid when symfony messenger sender throws exception * test: add test
1 parent 92cf684 commit 3035e6b

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

src/MessengerInstrumentation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public static function register(): void
150150
post: static function (
151151
SenderInterface $sender,
152152
array $params,
153-
Envelope $result,
153+
?Envelope $result,
154154
?\Throwable $exception
155155
): void {
156156
$scope = Context::storage()->scope();

tests/Integration/MessengerInstrumentationTest.php

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
use Symfony\Component\Messenger\MessageBus;
1212
use Symfony\Component\Messenger\MessageBusInterface;
1313
use Symfony\Component\Messenger\Transport\InMemory\InMemoryTransport;
14-
use Symfony\Component\Messenger\Transport\InMemoryTransport as LegacyInmemoryTransport;
14+
use Symfony\Component\Messenger\Transport\InMemoryTransport as LegacyInMemoryTransport;
15+
use Symfony\Component\Messenger\Transport\TransportInterface;
1516

1617
final class SendEmailMessage
1718
{
@@ -42,7 +43,7 @@ protected function getTransport()
4243
}
4344

4445
// Symfony 5+
45-
return new LegacyInmemoryTransport();
46+
return new LegacyInMemoryTransport();
4647
}
4748

4849
/**
@@ -98,7 +99,7 @@ public function test_send_message($message, string $spanName, int $kind, array $
9899
}
99100
}
100101

101-
public function test_can_sustain_throw()
102+
public function test_can_sustain_throw_while_dispatching()
102103
{
103104
$bus = new class() implements MessageBusInterface {
104105
public function dispatch(object $message, array $stamps = []): Envelope
@@ -117,6 +118,39 @@ public function dispatch(object $message, array $stamps = []): Envelope
117118
}
118119
}
119120

121+
public function test_can_sustain_throw_while_sending()
122+
{
123+
$transport = new class() implements TransportInterface {
124+
public function get(): iterable
125+
{
126+
throw new \Exception('booo!');
127+
}
128+
129+
public function ack(Envelope $envelope): void
130+
{
131+
throw new \Exception('booo!');
132+
}
133+
134+
public function reject(Envelope $envelope): void
135+
{
136+
throw new \Exception('booo!');
137+
}
138+
139+
public function send(Envelope $envelope): Envelope
140+
{
141+
throw new \Exception('booo!');
142+
}
143+
};
144+
145+
try {
146+
$transport->send(new Envelope(new SendEmailMessage('Hello Again')));
147+
} catch (\Throwable $e) {
148+
$this->assertCount(1, $this->storage);
149+
150+
$span = $this->storage[0];
151+
}
152+
}
153+
120154
public function sendDataProvider(): array
121155
{
122156
return [

0 commit comments

Comments
 (0)