Skip to content

Add dedicated exception for basic.return messages. #1832

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 6 commits into from
May 12, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
51 changes: 51 additions & 0 deletions projects/RabbitMQ.Client/Exceptions/PublishException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,55 @@ public PublishException(ulong publishSequenceNumber, bool isReturn) : base()
/// </summary>
public ulong PublishSequenceNumber => _publishSequenceNumber;
}

/// <summary>
/// Class for exceptions related to publisher confirmations
/// or the <c>mandatory</c> flag, when <c>basic.return</c> is
/// sent from the broker.
/// </summary>
public class PublishReturnException : PublishException
{
private readonly string _exchange;
private readonly string _routingKey;

public PublishReturnException(ulong publishSequenceNumber, string exchange, string routingKey)
: base(publishSequenceNumber, true)
{
_exchange = exchange;
_routingKey = routingKey;
}

/// <summary>
/// Get the Exchange associated with this <c>basic.return</c>
/// </summary>
public string Exchange => _exchange;

/// <summary>
/// Get the RoutingKey associated with this <c>basic.return</c>
/// </summary>
public string RoutingKey => _routingKey;
}

internal static class PublishExceptionFactory
{
internal static PublishException Create(bool isReturn,
ulong deliveryTag, string? exchange = null, string? routingKey = null)
{
if (isReturn)
{
if (exchange is not null && routingKey is not null)
{
return new PublishReturnException(deliveryTag, exchange, routingKey);
}
else
{
return new PublishException(deliveryTag, isReturn);
}
}
else
{
return new PublishException(deliveryTag, isReturn);
}
}
}
}
12 changes: 8 additions & 4 deletions projects/RabbitMQ.Client/Impl/Channel.PublisherConfirms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ private void HandleAck(ulong deliveryTag, bool multiple)
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void HandleNack(ulong deliveryTag, bool multiple, bool isReturn)
private void HandleNack(ulong deliveryTag, bool multiple, bool isReturn,
string? exchange = null, string? routingKey = null)
{
if (ShouldHandleAckOrNack(deliveryTag))
{
Expand All @@ -210,7 +211,8 @@ private void HandleNack(ulong deliveryTag, bool multiple, bool isReturn)
{
if (pair.Key <= deliveryTag)
{
pair.Value.SetException(new PublishException(pair.Key, isReturn));
PublishException ex = PublishExceptionFactory.Create(isReturn, pair.Key, exchange, routingKey);
pair.Value.SetException(ex);
_confirmsTaskCompletionSources.Remove(pair.Key, out _);
}
}
Expand All @@ -219,7 +221,8 @@ private void HandleNack(ulong deliveryTag, bool multiple, bool isReturn)
{
if (_confirmsTaskCompletionSources.Remove(deliveryTag, out TaskCompletionSource<bool>? tcs))
{
tcs.SetException(new PublishException(deliveryTag, isReturn));
PublishException ex = PublishExceptionFactory.Create(isReturn, deliveryTag);
tcs.SetException(ex);
}
}
}
Expand Down Expand Up @@ -249,7 +252,8 @@ private void HandleReturn(BasicReturnEventArgs basicReturnEvent)
}
}

HandleNack(publishSequenceNumber, multiple: false, isReturn: true);
HandleNack(publishSequenceNumber, multiple: false, isReturn: true,
exchange: basicReturnEvent.Exchange, routingKey: basicReturnEvent.RoutingKey);
}
}

Expand Down
4 changes: 4 additions & 0 deletions projects/RabbitMQ.Client/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
RabbitMQ.Client.Exceptions.PublishReturnException
RabbitMQ.Client.Exceptions.PublishReturnException.Exchange.get -> string!
RabbitMQ.Client.Exceptions.PublishReturnException.PublishReturnException(ulong publishSequenceNumber, string! exchange, string! routingKey) -> void
RabbitMQ.Client.Exceptions.PublishReturnException.RoutingKey.get -> string!