Thanks to visit codestin.com
Credit goes to github.com

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
5 changes: 5 additions & 0 deletions src/Symfony/Component/Mailer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.4
---

* Add `logger` (constructor) property to `RoundRobinTransport`

7.3
---

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 @@ -167,19 +168,45 @@ public function testSendOneDeadMessageAlterationsDoNotPersist()
$this->assertFalse($message->getHeaders()->has('X-Transport-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 @@ -36,6 +38,7 @@ class RoundRobinTransport implements TransportInterface
public function __construct(
private array $transports,
private int $retryPeriod = 60,
private LoggerInterface $logger = new NullLogger(),
) {
if (!$transports) {
throw new TransportException(\sprintf('"%s" must have at least one transport configured.', static::class));
Expand All @@ -54,6 +57,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