Skip to content

Commit b64b6c0

Browse files
authored
Allow use of current hub scope in Messenger Listener (#339)
* Alter MessengerListener to use current hub with methods for capturing exception and accessing client. This allows the scope of the current hub to be updated in a prior event listener. * Inject the hub rather than rely upon singleton instance. * Correctly typehint and remove phpstan exclusions for the MessengerListenerTest properties. * Merge changes from #340. * Fix up new test. Co-authored-by: Steve Hall <[email protected]>
1 parent 942ae46 commit b64b6c0

File tree

4 files changed

+34
-29
lines changed

4 files changed

+34
-29
lines changed

Diff for: phpstan-baseline.neon

-5
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,6 @@ parameters:
270270
count: 1
271271
path: test/EventListener/ConsoleListenerTest.php
272272

273-
-
274-
message: "#^Property Sentry\\\\SentryBundle\\\\Test\\\\EventListener\\\\MessengerListenerTest\\:\\:\\$client has no typehint specified\\.$#"
275-
count: 1
276-
path: test/EventListener/MessengerListenerTest.php
277-
278273
-
279274
message: "#^Class Symfony\\\\Component\\\\Messenger\\\\Event\\\\WorkerMessageHandledEvent constructor invoked with 3 parameters, 2 required\\.$#"
280275
count: 1

Diff for: src/EventListener/MessengerListener.php

+13-9
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,30 @@
33
namespace Sentry\SentryBundle\EventListener;
44

55
use Sentry\FlushableClientInterface;
6+
use Sentry\State\HubInterface;
67
use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
78
use Symfony\Component\Messenger\Event\WorkerMessageHandledEvent;
89
use Symfony\Component\Messenger\Exception\HandlerFailedException;
910

1011
final class MessengerListener
1112
{
1213
/**
13-
* @var FlushableClientInterface
14+
* @var HubInterface
1415
*/
15-
private $client;
16+
private $hub;
1617

1718
/**
1819
* @var bool
1920
*/
2021
private $captureSoftFails;
2122

2223
/**
23-
* @param FlushableClientInterface $client
24-
* @param bool $captureSoftFails
24+
* @param HubInterface $hub
25+
* @param bool $captureSoftFails
2526
*/
26-
public function __construct(FlushableClientInterface $client, bool $captureSoftFails = true)
27+
public function __construct(HubInterface $hub, bool $captureSoftFails = true)
2728
{
28-
$this->client = $client;
29+
$this->hub = $hub;
2930
$this->captureSoftFails = $captureSoftFails;
3031
}
3132

@@ -43,10 +44,10 @@ public function onWorkerMessageFailed(WorkerMessageFailedEvent $event): void
4344

4445
if ($error instanceof HandlerFailedException) {
4546
foreach ($error->getNestedExceptions() as $nestedException) {
46-
$this->client->captureException($nestedException);
47+
$this->hub->captureException($nestedException);
4748
}
4849
} else {
49-
$this->client->captureException($error);
50+
$this->hub->captureException($error);
5051
}
5152

5253
$this->flush();
@@ -64,6 +65,9 @@ public function onWorkerMessageHandled(WorkerMessageHandledEvent $event): void
6465

6566
private function flush(): void
6667
{
67-
$this->client->flush();
68+
$client = $this->hub->getClient();
69+
if ($client instanceof FlushableClientInterface) {
70+
$client->flush();
71+
}
6872
}
6973
}

Diff for: src/Resources/config/services.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
</service>
5454

5555
<service id="Sentry\SentryBundle\EventListener\MessengerListener" class="Sentry\SentryBundle\EventListener\MessengerListener" public="false">
56-
<argument type="service" id="Sentry\FlushableClientInterface" />
56+
<argument type="service" id="Sentry\State\HubInterface" />
5757
<tag name="kernel.event_listener" event="Symfony\Component\Messenger\Event\WorkerMessageFailedEvent" method="onWorkerMessageFailed" priority="%sentry.listener_priorities.worker_error%" />
5858
<tag name="kernel.event_listener" event="Symfony\Component\Messenger\Event\WorkerMessageHandledEvent" method="onWorkerMessageHandled" priority="%sentry.listener_priorities.worker_error%" />
5959
</service>

Diff for: test/EventListener/MessengerListenerTest.php

+20-14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Sentry\FlushableClientInterface;
66
use Sentry\SentryBundle\EventListener\MessengerListener;
77
use Sentry\SentryBundle\Test\BaseTestCase;
8+
use Sentry\State\HubInterface;
89
use Symfony\Component\Messenger\Envelope;
910
use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
1011
use Symfony\Component\Messenger\Event\WorkerMessageHandledEvent;
@@ -13,13 +14,18 @@
1314

1415
class MessengerListenerTest extends BaseTestCase
1516
{
17+
/** @var \Prophecy\Prophecy\ObjectProphecy|FlushableClientInterface */
1618
private $client;
19+
/** @var \Prophecy\Prophecy\ObjectProphecy|HubInterface */
20+
private $hub;
1721

1822
protected function setUp(): void
1923
{
2024
parent::setUp();
2125

2226
$this->client = $this->prophesize(FlushableClientInterface::class);
27+
$this->hub = $this->prophesize(HubInterface::class);
28+
$this->hub->getClient()->willReturn($this->client);
2329
}
2430

2531
public function testSoftFailsAreRecorded(): void
@@ -34,10 +40,10 @@ public function testSoftFailsAreRecorded(): void
3440
$error = new \RuntimeException();
3541
$wrappedError = new HandlerFailedException($envelope, [$error]);
3642

37-
$this->client->captureException($error)->shouldBeCalled();
43+
$this->hub->captureException($error)->shouldBeCalled();
3844
$this->client->flush()->shouldBeCalled();
3945

40-
$listener = new MessengerListener($this->client->reveal(), true);
46+
$listener = new MessengerListener($this->hub->reveal(), true);
4147
$event = $this->getMessageFailedEvent($envelope, 'receiver', $wrappedError, true);
4248

4349
$listener->onWorkerMessageFailed($event);
@@ -54,10 +60,10 @@ public function testNonMessengerErrorsAreRecorded(): void
5460

5561
$error = new \RuntimeException();
5662

57-
$this->client->captureException($error)->shouldBeCalled();
63+
$this->hub->captureException($error)->shouldBeCalled();
5864
$this->client->flush()->shouldBeCalled();
5965

60-
$listener = new MessengerListener($this->client->reveal(), true);
66+
$listener = new MessengerListener($this->hub->reveal(), true);
6167
$event = $this->getMessageFailedEvent($envelope, 'receiver', $error, false);
6268

6369
$listener->onWorkerMessageFailed($event);
@@ -75,10 +81,10 @@ public function testHardFailsAreRecorded(): void
7581
$error = new \RuntimeException();
7682
$wrappedError = new HandlerFailedException($envelope, [$error]);
7783

78-
$this->client->captureException($error)->shouldBeCalled();
84+
$this->hub->captureException($error)->shouldBeCalled();
7985
$this->client->flush()->shouldBeCalled();
8086

81-
$listener = new MessengerListener($this->client->reveal(), true);
87+
$listener = new MessengerListener($this->hub->reveal(), true);
8288
$event = $this->getMessageFailedEvent($envelope, 'receiver', $wrappedError, false);
8389

8490
$listener->onWorkerMessageFailed($event);
@@ -96,10 +102,10 @@ public function testSoftFailsAreNotRecorded(): void
96102
$error = new \RuntimeException();
97103
$wrappedError = new HandlerFailedException($envelope, [$error]);
98104

99-
$this->client->captureException($error)->shouldNotBeCalled();
105+
$this->hub->captureException($error)->shouldNotBeCalled();
100106
$this->client->flush()->shouldNotBeCalled();
101107

102-
$listener = new MessengerListener($this->client->reveal(), false);
108+
$listener = new MessengerListener($this->hub->reveal(), false);
103109
$event = $this->getMessageFailedEvent($envelope, 'receiver', $wrappedError, true);
104110

105111
$listener->onWorkerMessageFailed($event);
@@ -117,10 +123,10 @@ public function testHardFailsAreRecordedWithCaptureSoftDisabled(): void
117123
$error = new \RuntimeException();
118124
$wrappedError = new HandlerFailedException($envelope, [$error]);
119125

120-
$this->client->captureException($error)->shouldBeCalled();
126+
$this->hub->captureException($error)->shouldBeCalled();
121127
$this->client->flush()->shouldBeCalled();
122128

123-
$listener = new MessengerListener($this->client->reveal(), false);
129+
$listener = new MessengerListener($this->hub->reveal(), false);
124130
$event = $this->getMessageFailedEvent($envelope, 'receiver', $wrappedError, false);
125131

126132
$listener->onWorkerMessageFailed($event);
@@ -140,11 +146,11 @@ public function testHandlerFailedExceptionIsUnwrapped(): void
140146

141147
$event = $this->getMessageFailedEvent($envelope, 'receiver', $wrappedError, false);
142148

143-
$this->client->captureException($error1)->shouldBeCalled();
144-
$this->client->captureException($error2)->shouldBeCalled();
149+
$this->hub->captureException($error1)->shouldBeCalled();
150+
$this->hub->captureException($error2)->shouldBeCalled();
145151
$this->client->flush()->shouldBeCalled();
146152

147-
$listener = new MessengerListener($this->client->reveal());
153+
$listener = new MessengerListener($this->hub->reveal());
148154
$listener->onWorkerMessageFailed($event);
149155
}
150156

@@ -155,7 +161,7 @@ public function testClientIsFlushedWhenMessageHandled(): void
155161
}
156162

157163
$this->client->flush()->shouldBeCalled();
158-
$listener = new MessengerListener($this->client->reveal());
164+
$listener = new MessengerListener($this->hub->reveal());
159165

160166
$message = (object) ['foo' => 'bar'];
161167
$envelope = Envelope::wrap($message);

0 commit comments

Comments
 (0)