Skip to content

Commit 0d4d632

Browse files
committed
Add dedicated exception for basic.return messages.
Fixes #1831 This PR adds the `PublishReturnException` class that includes the originating exchange and routing key for a `basic.return` message. It should be backwards-compatible in the API.
1 parent 9ecad93 commit 0d4d632

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

projects/RabbitMQ.Client/Exceptions/PublishException.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,55 @@ public PublishException(ulong publishSequenceNumber, bool isReturn) : base()
6363
/// </summary>
6464
public ulong PublishSequenceNumber => _publishSequenceNumber;
6565
}
66+
67+
/// <summary>
68+
/// Class for exceptions related to publisher confirmations
69+
/// or the <c>mandatory</c> flag, when <c>basic.return</c> is
70+
/// sent from the broker.
71+
/// </summary>
72+
public class PublishReturnException : PublishException
73+
{
74+
private readonly string _exchange;
75+
private readonly string _routingKey;
76+
77+
public PublishReturnException(ulong publishSequenceNumber, string exchange, string routingKey)
78+
: base(publishSequenceNumber, true)
79+
{
80+
_exchange = exchange;
81+
_routingKey = routingKey;
82+
}
83+
84+
/// <summary>
85+
/// Get the Exchange associated with this <c>basic.return</c>
86+
/// </summary>
87+
public string Exchange => _exchange;
88+
89+
/// <summary>
90+
/// Get the RoutingKey associated with this <c>basic.return</c>
91+
/// </summary>
92+
public string RoutingKey => _routingKey;
93+
}
94+
95+
internal static class PublishExceptionFactory
96+
{
97+
internal static PublishException Create(bool isReturn,
98+
ulong deliveryTag, string? exchange = null, string? routingKey = null)
99+
{
100+
if (isReturn)
101+
{
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+
}
110+
}
111+
else
112+
{
113+
return new PublishException(deliveryTag, isReturn);
114+
}
115+
}
116+
}
66117
}

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ private void HandleAck(ulong deliveryTag, bool multiple)
200200
}
201201

202202
[MethodImpl(MethodImplOptions.AggressiveInlining)]
203-
private void HandleNack(ulong deliveryTag, bool multiple, bool isReturn)
203+
private void HandleNack(ulong deliveryTag, bool multiple, bool isReturn,
204+
string? exchange = null, string? routingKey = null)
204205
{
205206
if (ShouldHandleAckOrNack(deliveryTag))
206207
{
@@ -210,7 +211,8 @@ private void HandleNack(ulong deliveryTag, bool multiple, bool isReturn)
210211
{
211212
if (pair.Key <= deliveryTag)
212213
{
213-
pair.Value.SetException(new PublishException(pair.Key, isReturn));
214+
PublishException ex = PublishExceptionFactory.Create(isReturn, pair.Key, exchange, routingKey);
215+
pair.Value.SetException(ex);
214216
_confirmsTaskCompletionSources.Remove(pair.Key, out _);
215217
}
216218
}
@@ -219,7 +221,8 @@ private void HandleNack(ulong deliveryTag, bool multiple, bool isReturn)
219221
{
220222
if (_confirmsTaskCompletionSources.Remove(deliveryTag, out TaskCompletionSource<bool>? tcs))
221223
{
222-
tcs.SetException(new PublishException(deliveryTag, isReturn));
224+
PublishException ex = PublishExceptionFactory.Create(isReturn, deliveryTag);
225+
tcs.SetException(ex);
223226
}
224227
}
225228
}
@@ -249,7 +252,8 @@ private void HandleReturn(BasicReturnEventArgs basicReturnEvent)
249252
}
250253
}
251254

252-
HandleNack(publishSequenceNumber, multiple: false, isReturn: true);
255+
HandleNack(publishSequenceNumber, multiple: false, isReturn: true,
256+
exchange: basicReturnEvent.Exchange, routingKey: basicReturnEvent.RoutingKey);
253257
}
254258
}
255259

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
RabbitMQ.Client.Exceptions.PublishReturnException
2+
RabbitMQ.Client.Exceptions.PublishReturnException.Exchange.get -> string!
3+
RabbitMQ.Client.Exceptions.PublishReturnException.PublishReturnException(ulong publishSequenceNumber, string! exchange, string! routingKey) -> void
4+
RabbitMQ.Client.Exceptions.PublishReturnException.RoutingKey.get -> string!

0 commit comments

Comments
 (0)