Skip to content

[Mailer][Transport] Allow exception logging for RoundRobinTransport mailer #60110

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

Open
wants to merge 3 commits into
base: 7.4
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/Symfony/Component/Mailer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ CHANGELOG
* Add DSN param `source_ip` to allow binding to a (specific) IPv4 or IPv6 address.
* Add DSN param `require_tls` to enforce use of TLS/STARTTLS
* Add `DkimSignedMessageListener`, `SmimeEncryptedMessageListener`, and `SmimeSignedMessageListener`
* Add `logger` (constructor) property to `RoundRobinTransport`

7.2
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Mailer\Tests\Transport;

use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Symfony\Component\Mailer\Exception\TransportException;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\Transport\RoundRobinTransport;
Expand Down Expand Up @@ -144,19 +145,45 @@ public function testSendOneDeadAndRecoveryWithinRetryPeriod()
$this->assertTransports($t, 1, []);
}

public function testLoggerReceivesExceptions()
{
$t1 = $this->createMock(TransportInterface::class);
$t1->expects($this->exactly(2))->method('send');

$ex = new TransportException();
$t2 = $this->createMock(TransportInterface::class);
$t2->expects($this->exactly(1))
->method('send')
->willReturnCallback(fn () => throw $ex);
$t2->expects($this->atLeast(1))->method('__toString')->willReturn('t2');

$log = $this->createMock(LoggerInterface::class);
$log->expects($this->exactly(1))
->method('error')
->with('Transport "t2" failed.', ['exception' => $ex]);

$t = new RoundRobinTransport([$t1, $t2], logger: $log);
$p = new \ReflectionProperty($t, 'cursor');
$p->setValue($t, 0);
$t->send(new RawMessage(''));
$this->assertTransports($t, 1, []);
$t->send(new RawMessage(''));
$this->assertTransports($t, 1, [$t2]);
}

public function testFailureDebugInformation()
{
$t1 = $this->createMock(TransportInterface::class);
$e1 = new TransportException();
$e1->appendDebug('Debug message 1');
$t1->expects($this->once())->method('send')->willThrowException($e1);
$t1->expects($this->once())->method('__toString')->willReturn('t1');
$t1->expects($this->atLeast(1))->method('__toString')->willReturn('t1');

$t2 = $this->createMock(TransportInterface::class);
$e2 = new TransportException();
$e2->appendDebug('Debug message 2');
$t2->expects($this->once())->method('send')->willThrowException($e2);
$t2->expects($this->once())->method('__toString')->willReturn('t2');
$t2->expects($this->atLeast(1))->method('__toString')->willReturn('t2');

$t = new RoundRobinTransport([$t1, $t2]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Mailer\Transport;

use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\Mailer\Envelope;
use Symfony\Component\Mailer\Exception\TransportException;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
Expand All @@ -24,6 +26,7 @@
*/
class RoundRobinTransport implements TransportInterface
{
private LoggerInterface $logger;
/**
* @var \SplObjectStorage<TransportInterface, float>
*/
Expand All @@ -36,11 +39,13 @@ class RoundRobinTransport implements TransportInterface
public function __construct(
private array $transports,
private int $retryPeriod = 60,
?LoggerInterface $logger = null,
) {
if (!$transports) {
throw new TransportException(\sprintf('"%s" must have at least one transport configured.', static::class));
}

$this->logger = $logger ?? new NullLogger();
$this->deadTransports = new \SplObjectStorage();
}

Expand All @@ -54,6 +59,7 @@ public function send(RawMessage $message, ?Envelope $envelope = null): ?SentMess
} catch (TransportExceptionInterface $e) {
$exception ??= new TransportException('All transports failed.');
$exception->appendDebug(\sprintf("Transport \"%s\": %s\n", $transport, $e->getDebug()));
$this->logger->error(\sprintf("Transport \"%s\" failed.", $transport), ['exception' => $e]);
$this->deadTransports[$transport] = microtime(true);
}
}
Expand Down
Loading