Skip to content

Commit

Permalink
Merge changes from getsentry#340.
Browse files Browse the repository at this point in the history
  • Loading branch information
sh41 committed May 7, 2020
2 parents 6ab1d88 + 942ae46 commit 4ef473f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 21 deletions.
20 changes: 12 additions & 8 deletions src/EventListener/MessengerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,15 @@ public function onWorkerMessageFailed(WorkerMessageFailedEvent $event): void

$error = $event->getThrowable();

if ($error instanceof HandlerFailedException && null !== $error->getPrevious()) {
// Unwrap the messenger exception to get the original error
$error = $error->getPrevious();
if ($error instanceof HandlerFailedException) {
foreach ($error->getNestedExceptions() as $nestedException) {
$this->hub->captureException($nestedException);
}
} else {
$this->hub->captureException($error);
}

$this->hub->captureException($error);
$client = $this->hub->getClient();
if ($client instanceof FlushableClientInterface) {
$client->flush();
}
$this->flush();
}

/**
Expand All @@ -61,6 +60,11 @@ public function onWorkerMessageHandled(WorkerMessageHandledEvent $event): void
{
// Flush normally happens at shutdown... which only happens in the worker if it is run with a lifecycle limit
// such as --time=X or --limit=Y. Flush immediately in a background worker.
$this->flush();
}

private function flush(): void
{
$client = $this->hub->getClient();
if ($client instanceof FlushableClientInterface) {
$client->flush();
Expand Down
56 changes: 43 additions & 13 deletions test/EventListener/MessengerListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,37 @@ public function testSoftFailsAreRecorded(): void
self::markTestSkipped('Messenger not supported in this environment.');
}

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

$error = new \RuntimeException();
$wrappedError = new HandlerFailedException($envelope, [$error]);

$this->hub->captureException($error)->shouldBeCalled();
$this->client->flush()->shouldBeCalled();

$listener = new MessengerListener($this->hub->reveal(), true);
$event = $this->getMessageFailedEvent($envelope, 'receiver', $wrappedError, true);

$listener->onWorkerMessageFailed($event);
}

public function testNonMessengerErrorsAreRecorded(): void
{
if (! $this->supportsMessenger()) {
self::markTestSkipped('Messenger not supported in this environment.');
}

$message = (object) ['foo' => 'bar'];
$envelope = Envelope::wrap($message);
$event = $this->getMessageFailedEvent($envelope, 'receiver', $error, true);

$error = new \RuntimeException();

$this->client->captureException($error)->shouldBeCalled();
$this->client->flush()->shouldBeCalled();

$listener = new MessengerListener($this->hub->reveal(), true);
$event = $this->getMessageFailedEvent($envelope, 'receiver', $error, false);

$listener->onWorkerMessageFailed($event);
}
Expand All @@ -53,15 +75,17 @@ public function testHardFailsAreRecorded(): void
self::markTestSkipped('Messenger not supported in this environment.');
}

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

$error = new \RuntimeException();
$wrappedError = new HandlerFailedException($envelope, [$error]);

$this->hub->captureException($error)->shouldBeCalled();
$this->client->flush()->shouldBeCalled();

$listener = new MessengerListener($this->hub->reveal(), true);
$message = (object) ['foo' => 'bar'];
$envelope = Envelope::wrap($message);
$event = $this->getMessageFailedEvent($envelope, 'receiver', $error, false);
$event = $this->getMessageFailedEvent($envelope, 'receiver', $wrappedError, false);

$listener->onWorkerMessageFailed($event);
}
Expand All @@ -72,15 +96,17 @@ public function testSoftFailsAreNotRecorded(): void
self::markTestSkipped('Messenger not supported in this environment.');
}

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

$error = new \RuntimeException();
$wrappedError = new HandlerFailedException($envelope, [$error]);

$this->hub->captureException($error)->shouldNotBeCalled();
$this->client->flush()->shouldNotBeCalled();

$listener = new MessengerListener($this->hub->reveal(), false);
$message = (object) ['foo' => 'bar'];
$envelope = Envelope::wrap($message);
$event = $this->getMessageFailedEvent($envelope, 'receiver', $error, true);
$event = $this->getMessageFailedEvent($envelope, 'receiver', $wrappedError, true);

$listener->onWorkerMessageFailed($event);
}
Expand All @@ -91,15 +117,17 @@ public function testHardFailsAreRecordedWithCaptureSoftDisabled(): void
self::markTestSkipped('Messenger not supported in this environment.');
}

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

$error = new \RuntimeException();
$wrappedError = new HandlerFailedException($envelope, [$error]);

$this->hub->captureException($error)->shouldBeCalled();
$this->client->flush()->shouldBeCalled();

$listener = new MessengerListener($this->hub->reveal(), false);
$message = (object) ['foo' => 'bar'];
$envelope = Envelope::wrap($message);
$event = $this->getMessageFailedEvent($envelope, 'receiver', $error, false);
$event = $this->getMessageFailedEvent($envelope, 'receiver', $wrappedError, false);

$listener->onWorkerMessageFailed($event);
}
Expand All @@ -112,12 +140,14 @@ public function testHandlerFailedExceptionIsUnwrapped(): void

$message = (object) ['foo' => 'bar'];
$envelope = Envelope::wrap($message);
$error = new \RuntimeException();
$wrappedError = new HandlerFailedException($envelope, [$error]);
$error1 = new \RuntimeException();
$error2 = new \RuntimeException();
$wrappedError = new HandlerFailedException($envelope, [$error1, $error2]);

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

$this->hub->captureException($error)->shouldBeCalled();
$this->hub->captureException($error1)->shouldBeCalled();
$this->hub->captureException($error2)->shouldBeCalled();
$this->client->flush()->shouldBeCalled();

$listener = new MessengerListener($this->hub->reveal());
Expand Down

0 comments on commit 4ef473f

Please sign in to comment.