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

Unwrap HandlerFailedException for sync Messenger messages too #475

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Unwrap Messenger failures (`HandlerFailedException`) when using sync transport (#475)

## 4.2.1 (2021-08-24)

- Fix return type for `TracingDriver::getDatabase()` method (#541)
Expand Down
13 changes: 11 additions & 2 deletions src/EventListener/ErrorListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Sentry\State\HubInterface;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\Messenger\Exception\HandlerFailedException;

/**
* This listener listens for error events and logs them to Sentry.
Expand Down Expand Up @@ -36,9 +37,17 @@ public function handleExceptionEvent(ErrorListenerExceptionEvent $event): void
{
/** @psalm-suppress RedundantCondition */
if ($event instanceof ExceptionEvent) {
$this->hub->captureException($event->getThrowable());
$error = $event->getThrowable();
} else {
$this->hub->captureException($event->getException());
$error = $event->getException();
}

if ($error instanceof HandlerFailedException) {
foreach ($error->getNestedExceptions() as $nestedException) {
$this->hub->captureException($nestedException);
}
} else {
$this->hub->captureException($error);
}
}
}
8 changes: 8 additions & 0 deletions tests/End2End/App/Controller/MessengerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Sentry\SentryBundle\Tests\End2End\App\Controller;

use Sentry\SentryBundle\Tests\End2End\App\Messenger\FooMessage;
use Sentry\SentryBundle\Tests\End2End\App\Messenger\SyncMessage;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\MessageBusInterface;

Expand Down Expand Up @@ -33,4 +34,11 @@ public function dispatchUnrecoverableMessage(): Response

return new Response('Success');
}

public function dispatchUnrecoverableSyncMessage(): Response
{
$this->messenger->dispatch(new SyncMessage());

return new Response('Success');
}
}
9 changes: 9 additions & 0 deletions tests/End2End/App/Messenger/IgnorableException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Sentry\SentryBundle\Tests\End2End\App\Messenger;

final class IgnorableException extends \Exception
{
}
9 changes: 9 additions & 0 deletions tests/End2End/App/Messenger/SyncMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Sentry\SentryBundle\Tests\End2End\App\Messenger;

final class SyncMessage
{
}
15 changes: 15 additions & 0 deletions tests/End2End/App/Messenger/SyncMessageHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Sentry\SentryBundle\Tests\End2End\App\Messenger;

use Symfony\Component\Messenger\Handler\MessageHandlerInterface;

final class SyncMessageHandler implements MessageHandlerInterface
{
public function __invoke(SyncMessage $message): void
{
throw new IgnorableException('This is an intentional failure while handling a message of class ' . \get_class($message));
}
}
8 changes: 8 additions & 0 deletions tests/End2End/App/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ sentry:
capture_silenced_errors: true
error_types: E_ALL & ~E_USER_DEPRECATED
traces_sample_rate: 0
integrations:
- 'Sentry\Integration\IgnoreErrorsIntegration'

framework:
router: { resource: "%routing_config_dir%/routing.yml" }
Expand All @@ -29,6 +31,12 @@ services:
Sentry\SentryBundle\Tests\End2End\App\Command\MainCommand:
tags: [{ name: 'console.command', command: 'main-command' }]

Sentry\Integration\IgnoreErrorsIntegration:
arguments:
$options:
ignore_exceptions:
- '\Sentry\SentryBundle\Tests\End2End\App\Messenger\IgnorableException'

monolog:
handlers:
main:
Expand Down
8 changes: 7 additions & 1 deletion tests/End2End/App/messenger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ services:
Sentry\SentryBundle\Tests\End2End\App\Messenger\FooMessageHandler:
class: \Sentry\SentryBundle\Tests\End2End\App\Messenger\FooMessageHandler

Sentry\SentryBundle\Tests\End2End\App\Messenger\SyncMessageHandler:
class: \Sentry\SentryBundle\Tests\End2End\App\Messenger\SyncMessageHandler

Sentry\SentryBundle\Tests\End2End\App\Controller\MessengerController:
autowire: true
tags:
Expand All @@ -16,12 +19,15 @@ services:
framework:
messenger:
transports:
sync:
dsn: 'sync://'
async:
dsn: 'static://'
retry_strategy:
max_retries: 1
routing:
'*': async
Sentry\SentryBundle\Tests\End2End\App\Messenger\FooMessage: async
Sentry\SentryBundle\Tests\End2End\App\Messenger\SyncMessage: sync

sentry:
messenger:
Expand Down
4 changes: 4 additions & 0 deletions tests/End2End/App/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ dispatch_unrecoverable:
path: /dispatch-unrecoverable-message
defaults: { _controller: 'Sentry\SentryBundle\Tests\End2End\App\Controller\MessengerController::dispatchUnrecoverableMessage' }

dispatch_unrecoverable_sync:
path: /dispatch-unrecoverable-sync-message
defaults: { _controller: 'Sentry\SentryBundle\Tests\End2End\App\Controller\MessengerController::dispatchUnrecoverableSyncMessage' }

tracing_ping_database:
path: /tracing/ping-database
defaults: { _controller: 'Sentry\SentryBundle\Tests\End2End\App\Controller\TracingController::pingDatabase' }
16 changes: 16 additions & 0 deletions tests/End2End/End2EndTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,22 @@ public function testMessengerCaptureSoftFailCanBeDisabled(): void
$this->assertLastEventIdIsNull($client);
}

public function testMessengerSyncDispatchUnwrapsErrors(): void
{
$this->skipIfMessengerIsMissing();

$client = static::createClient();

$client->request('GET', '/dispatch-unrecoverable-sync-message');

$response = $client->getResponse();

$this->assertInstanceOf(Response::class, $response);
$this->assertSame(500, $response->getStatusCode());

$this->assertLastEventIdIsNull($client);
}

private function consumeOneMessage(KernelInterface $kernel): void
{
$application = new Application($kernel);
Expand Down