Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capture all messenger exceptions #340

Merged
merged 6 commits into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/EventListener/MessengerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,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->client->captureException($nestedException);
}
} else {
$this->client->captureException($error);
}

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

/**
Expand All @@ -57,6 +59,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
{
$this->client->flush();
Jean85 marked this conversation as resolved.
Show resolved Hide resolved
}
}
56 changes: 43 additions & 13 deletions test/EventListener/MessengerListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,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->client->captureException($error)->shouldBeCalled();
$this->client->flush()->shouldBeCalled();

$listener = new MessengerListener($this->client->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->client->reveal(), true);
$event = $this->getMessageFailedEvent($envelope, 'receiver', $error, false);

$listener->onWorkerMessageFailed($event);
}
Expand All @@ -47,15 +69,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->client->captureException($error)->shouldBeCalled();
$this->client->flush()->shouldBeCalled();

$listener = new MessengerListener($this->client->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 @@ -66,15 +90,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->client->captureException($error)->shouldNotBeCalled();
$this->client->flush()->shouldNotBeCalled();

$listener = new MessengerListener($this->client->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 @@ -85,15 +111,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->client->captureException($error)->shouldBeCalled();
$this->client->flush()->shouldBeCalled();

$listener = new MessengerListener($this->client->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 @@ -106,12 +134,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->client->captureException($error)->shouldBeCalled();
$this->client->captureException($error1)->shouldBeCalled();
$this->client->captureException($error2)->shouldBeCalled();
$this->client->flush()->shouldBeCalled();

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