Skip to content

[Mailer] Include all transports' debug messages in RoundRobin transport exception #48126

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

Merged
merged 1 commit into from
Dec 14, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Mailer\Exception\TransportException;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\Transport\RoundRobinTransport;
use Symfony\Component\Mailer\Transport\TransportInterface;
use Symfony\Component\Mime\RawMessage;
Expand Down Expand Up @@ -60,10 +61,21 @@ public function testSendAllDead()
$t2 = $this->createMock(TransportInterface::class);
$t2->expects($this->once())->method('send')->will($this->throwException(new TransportException()));
$t = new RoundRobinTransport([$t1, $t2]);
$this->expectException(TransportException::class);
$this->expectExceptionMessage('All transports failed.');
$t->send(new RawMessage(''));
$this->assertTransports($t, 1, [$t1, $t2]);
$p = new \ReflectionProperty($t, 'cursor');
$p->setAccessible(true);
$p->setValue($t, 0);

try {
$t->send(new RawMessage(''));
} catch (\Exception $e) {
$this->assertInstanceOf(TransportException::class, $e);
$this->assertStringContainsString('All transports failed.', $e->getMessage());
$this->assertTransports($t, 0, [$t1, $t2]);

return;
}

$this->fail('The expected exception was not thrown.');
}

public function testSendOneDead()
Expand Down Expand Up @@ -127,6 +139,34 @@ public function testSendOneDeadAndRecoveryWithinRetryPeriod()
$this->assertTransports($t, 1, []);
}

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

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

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

try {
$t->send(new RawMessage(''));
} catch (TransportExceptionInterface $e) {
$this->assertStringContainsString('Transport "t1": Debug message 1', $e->getDebug());
$this->assertStringContainsString('Transport "t2": Debug message 2', $e->getDebug());

return;
}

$this->fail('Expected exception was not thrown!');
}

private function assertTransports(RoundRobinTransport $transport, int $cursor, array $deadTransports)
{
$p = new \ReflectionProperty($transport, 'cursor');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,19 @@ public function __construct(array $transports, int $retryPeriod = 60)

public function send(RawMessage $message, Envelope $envelope = null): ?SentMessage
{
$exception = null;

while ($transport = $this->getNextTransport()) {
try {
return $transport->send($message, $envelope);
} catch (TransportExceptionInterface $e) {
$exception = $exception ?? new TransportException('All transports failed.');
$exception->appendDebug(sprintf("Transport \"%s\": %s\n", $transport, $e->getDebug()));
$this->deadTransports[$transport] = microtime(true);
}
}

throw new TransportException('All transports failed.');
throw $exception ?? new TransportException('No transports found.');
}

public function __toString(): string
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy