Skip to content

Commit f1f3834

Browse files
committed
[Messenger] Added new TransportException which is thrown if transport could not send a message
1 parent 3d33cb3 commit f1f3834

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ CHANGELOG
1111
changed from `Serializer` to `PhpSerializer` inside `AmqpReceiver`,
1212
`AmqpSender`, `AmqpTransport` and `AmqpTransportFactory`.
1313

14+
* Added `TransportException` to mark an exception transport-related
15+
16+
* [BC BREAK] If listening to exceptions while using `AmqpSender`, `\AMQPException` is
17+
no longer thrown in favor of `TransportException`.
18+
1419
4.2.0
1520
-----
1621

Exception/TransportException.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\Exception;
13+
14+
/**
15+
* @author Eric Masoero <[email protected]>
16+
*
17+
* @experimental in 4.2
18+
*/
19+
class TransportException extends RuntimeException
20+
{
21+
}

Tests/Transport/AmqpExt/AmqpSenderTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,22 @@ public function testItSendsTheEncodedMessage()
3737
$sender = new AmqpSender($connection, $serializer);
3838
$sender->send($envelope);
3939
}
40+
41+
/**
42+
* @expectedException Symfony\Component\Messenger\Exception\TransportException
43+
*/
44+
public function testItThrowsATransportExceptionIfItCannotSendTheMessage()
45+
{
46+
$envelope = new Envelope(new DummyMessage('Oy'));
47+
$encoded = ['body' => '...', 'headers' => ['type' => DummyMessage::class]];
48+
49+
$serializer = $this->getMockBuilder(SerializerInterface::class)->getMock();
50+
$serializer->method('encode')->with($envelope)->willReturnOnConsecutiveCalls($encoded);
51+
52+
$connection = $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock();
53+
$connection->method('publish')->with($encoded['body'], $encoded['headers'])->willThrowException(new \AMQPException());
54+
55+
$sender = new AmqpSender($connection, $serializer);
56+
$sender->send($envelope);
57+
}
4058
}

Transport/AmqpExt/AmqpSender.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Messenger\Transport\AmqpExt;
1313

1414
use Symfony\Component\Messenger\Envelope;
15+
use Symfony\Component\Messenger\Exception\TransportException;
1516
use Symfony\Component\Messenger\Transport\Sender\SenderInterface;
1617
use Symfony\Component\Messenger\Transport\Serialization\PhpSerializer;
1718
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
@@ -41,7 +42,11 @@ public function send(Envelope $envelope): Envelope
4142
{
4243
$encodedMessage = $this->serializer->encode($envelope);
4344

44-
$this->connection->publish($encodedMessage['body'], $encodedMessage['headers']);
45+
try {
46+
$this->connection->publish($encodedMessage['body'], $encodedMessage['headers']);
47+
} catch (\AMQPException $e) {
48+
throw new TransportException('Current transport was not able to send given message, please try again');
49+
}
4550

4651
return $envelope;
4752
}

0 commit comments

Comments
 (0)