Skip to content

Commit 0c564b1

Browse files
committed
Add reply code and reply text to the new PublishBasicException
exception type.
1 parent 0d4d632 commit 0c564b1

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

projects/RabbitMQ.Client/Exceptions/PublishException.cs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,40 +73,50 @@ public class PublishReturnException : PublishException
7373
{
7474
private readonly string _exchange;
7575
private readonly string _routingKey;
76+
private readonly ushort _replyCode;
77+
private readonly string _replyText;
7678

77-
public PublishReturnException(ulong publishSequenceNumber, string exchange, string routingKey)
79+
public PublishReturnException(ulong publishSequenceNumber,
80+
string? exchange = null, string? routingKey = null,
81+
ushort? replyCode = null, string? replyText = null)
7882
: base(publishSequenceNumber, true)
7983
{
80-
_exchange = exchange;
81-
_routingKey = routingKey;
84+
_exchange = exchange ?? string.Empty;
85+
_routingKey = routingKey ?? string.Empty;
86+
_replyCode = replyCode ?? 0;
87+
_replyText = replyText ?? string.Empty;
8288
}
8389

8490
/// <summary>
85-
/// Get the Exchange associated with this <c>basic.return</c>
91+
/// Get the exchange associated with this <c>basic.return</c>
8692
/// </summary>
8793
public string Exchange => _exchange;
8894

8995
/// <summary>
90-
/// Get the RoutingKey associated with this <c>basic.return</c>
96+
/// Get the routing key associated with this <c>basic.return</c>
9197
/// </summary>
9298
public string RoutingKey => _routingKey;
99+
100+
/// <summary>
101+
/// Get the reply code associated with this <c>basic.return</c>
102+
/// </summary>
103+
public ushort ReplyCode => _replyCode;
104+
105+
/// <summary>
106+
/// Get the reply text associated with this <c>basic.return</c>
107+
/// </summary>
108+
public string ReplyText => _replyText;
93109
}
94110

95111
internal static class PublishExceptionFactory
96112
{
97113
internal static PublishException Create(bool isReturn,
98-
ulong deliveryTag, string? exchange = null, string? routingKey = null)
114+
ulong deliveryTag, string? exchange = null, string? routingKey = null,
115+
ushort? replyCode = null, string? replyText = null)
99116
{
100117
if (isReturn)
101118
{
102-
if (exchange is not null && routingKey is not null)
103-
{
104-
return new PublishReturnException(deliveryTag, exchange, routingKey);
105-
}
106-
else
107-
{
108-
return new PublishException(deliveryTag, isReturn);
109-
}
119+
return new PublishReturnException(deliveryTag, exchange, routingKey, replyCode, replyText);
110120
}
111121
else
112122
{

projects/RabbitMQ.Client/Impl/Channel.PublisherConfirms.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ private void HandleAck(ulong deliveryTag, bool multiple)
201201

202202
[MethodImpl(MethodImplOptions.AggressiveInlining)]
203203
private void HandleNack(ulong deliveryTag, bool multiple, bool isReturn,
204-
string? exchange = null, string? routingKey = null)
204+
string? exchange = null, string? routingKey = null,
205+
ushort? replyCode = null, string? replyText = null)
205206
{
206207
if (ShouldHandleAckOrNack(deliveryTag))
207208
{
@@ -211,7 +212,8 @@ private void HandleNack(ulong deliveryTag, bool multiple, bool isReturn,
211212
{
212213
if (pair.Key <= deliveryTag)
213214
{
214-
PublishException ex = PublishExceptionFactory.Create(isReturn, pair.Key, exchange, routingKey);
215+
PublishException ex = PublishExceptionFactory.Create(isReturn, pair.Key,
216+
exchange, routingKey, replyCode, replyText);
215217
pair.Value.SetException(ex);
216218
_confirmsTaskCompletionSources.Remove(pair.Key, out _);
217219
}
@@ -253,7 +255,8 @@ private void HandleReturn(BasicReturnEventArgs basicReturnEvent)
253255
}
254256

255257
HandleNack(publishSequenceNumber, multiple: false, isReturn: true,
256-
exchange: basicReturnEvent.Exchange, routingKey: basicReturnEvent.RoutingKey);
258+
exchange: basicReturnEvent.Exchange, routingKey: basicReturnEvent.RoutingKey,
259+
replyCode: basicReturnEvent.ReplyCode, replyText: basicReturnEvent.ReplyText);
257260
}
258261
}
259262

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
RabbitMQ.Client.Exceptions.PublishReturnException
22
RabbitMQ.Client.Exceptions.PublishReturnException.Exchange.get -> string!
3-
RabbitMQ.Client.Exceptions.PublishReturnException.PublishReturnException(ulong publishSequenceNumber, string! exchange, string! routingKey) -> void
3+
RabbitMQ.Client.Exceptions.PublishReturnException.PublishReturnException(ulong publishSequenceNumber, string? exchange = null, string? routingKey = null, ushort? replyCode = null, string? replyText = null) -> void
4+
RabbitMQ.Client.Exceptions.PublishReturnException.ReplyCode.get -> ushort
5+
RabbitMQ.Client.Exceptions.PublishReturnException.ReplyText.get -> string!
46
RabbitMQ.Client.Exceptions.PublishReturnException.RoutingKey.get -> string!

0 commit comments

Comments
 (0)