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

Skip to content

Commit d555d90

Browse files
committed
[Mailer] Enhance debugging when all transports fail
1 parent 55822f7 commit d555d90

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/Symfony/Component/Mailer/Tests/Transport/RoundRobinTransportTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Mailer\Exception\TransportException;
16+
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
1617
use Symfony\Component\Mailer\Transport\RoundRobinTransport;
1718
use Symfony\Component\Mailer\Transport\TransportInterface;
1819
use Symfony\Component\Mime\RawMessage;
@@ -61,6 +62,7 @@ public function testSendAllDead()
6162
$t2->expects($this->once())->method('send')->will($this->throwException(new TransportException()));
6263
$t = new RoundRobinTransport([$t1, $t2]);
6364
$p = new \ReflectionProperty($t, 'cursor');
65+
$p->setAccessible(true);
6466
$p->setValue($t, 0);
6567

6668
try {
@@ -137,6 +139,30 @@ public function testSendOneDeadAndRecoveryWithinRetryPeriod()
137139
$this->assertTransports($t, 1, []);
138140
}
139141

142+
public function testFailureDebugInformation()
143+
{
144+
$t1 = $this->createMock(TransportInterface::class);
145+
$e1 = new TransportException();
146+
$e1->appendDebug('Debug message 1');
147+
$t1->expects($this->once())->method('send')->will($this->throwException($e1));
148+
$t2 = $this->createMock(TransportInterface::class);
149+
$e2 = new TransportException();
150+
$e2->appendDebug('Debug message 2');
151+
$t2->expects($this->once())->method('send')->will($this->throwException($e2));
152+
$t = new RoundRobinTransport([$t1, $t2]);
153+
154+
try {
155+
$t->send(new RawMessage(''));
156+
} catch (TransportExceptionInterface $e) {
157+
$this->assertStringContainsString($e1->getDebug(), $e->getDebug());
158+
$this->assertStringContainsString($e2->getDebug(), $e->getDebug());
159+
160+
return;
161+
}
162+
163+
$this->fail('Expected exception was not thrown!');
164+
}
165+
140166
private function assertTransports(RoundRobinTransport $transport, int $cursor, array $deadTransports)
141167
{
142168
$p = new \ReflectionProperty($transport, 'cursor');

src/Symfony/Component/Mailer/Transport/RoundRobinTransport.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,18 @@ public function __construct(array $transports, int $retryPeriod = 60)
4848

4949
public function send(RawMessage $message, Envelope $envelope = null): ?SentMessage
5050
{
51+
$exception = new TransportException('All transports failed.');
52+
5153
while ($transport = $this->getNextTransport()) {
5254
try {
5355
return $transport->send($message, $envelope);
5456
} catch (TransportExceptionInterface $e) {
57+
$exception->appendDebug($this->formatException($transport, $e));
5558
$this->deadTransports[$transport] = microtime(true);
5659
}
5760
}
5861

59-
throw new TransportException('All transports failed.');
62+
throw $exception;
6063
}
6164

6265
public function __toString(): string
@@ -118,4 +121,9 @@ private function moveCursor(int $cursor): int
118121
{
119122
return ++$cursor >= \count($this->transports) ? 0 : $cursor;
120123
}
124+
125+
private function formatException(TransportInterface $transport, TransportExceptionInterface $exception): string
126+
{
127+
return sprintf('Transport %s: %s', $transport, $exception->getDebug());
128+
}
121129
}

0 commit comments

Comments
 (0)