Skip to content
Open
Changes from all commits
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: 29 additions & 22 deletions src/DotNetCore.CAP.NATS/ITransport.NATS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,40 +30,47 @@ public NATSTransport(ILogger<NATSTransport> logger, IConnectionPool connectionPo

public async Task<OperateResult> SendAsync(TransportMessage message)
{
var connection = _connectionPool.RentConnection();

try
{
var msg = new Msg(message.GetName(), message.Body.ToArray());
foreach (var header in message.Headers)
var connection = _connectionPool.RentConnection();
try
{
Comment on lines 33 to 37
Copy link

Copilot AI Oct 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The outer try-catch structure introduces a bug. If RentConnection() at line 35 throws an exception, the connection variable will not be initialized, but the finally block at line 67 will still attempt to call _connectionPool.Return(connection) with an uninitialized variable. Consider removing the outer try-catch and handling RentConnection() exceptions separately, or ensure the finally block checks if the connection was successfully initialized before returning it.

Copilot uses AI. Check for mistakes.
msg.Header[header.Key] = header.Value;
}
var msg = new Msg(message.GetName(), message.Body.ToArray());
foreach (var header in message.Headers)
{
msg.Header[header.Key] = header.Value;
}

var js = connection.CreateJetStreamContext(_jetStreamOptions);
var js = connection.CreateJetStreamContext(_jetStreamOptions);

var builder = PublishOptions.Builder().WithMessageId(message.GetId());
var builder = PublishOptions.Builder().WithMessageId(message.GetId());

var resp = await js.PublishAsync(msg, builder.Build());
var resp = await js.PublishAsync(msg, builder.Build());

if (resp.Seq > 0)
{
_logger.LogDebug($"NATS stream message [{message.GetName()}] has been published.");
if (resp.Seq > 0)
{
_logger.LogDebug($"NATS stream message [{message.GetName()}] has been published.");

return OperateResult.Success;
return OperateResult.Success;
}

throw new PublisherSentFailedException("NATS message send failed, no consumer reply!");
}
catch (Exception ex)
{
var warpEx = new PublisherSentFailedException(ex.Message, ex);
Copy link

Copilot AI Oct 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable name 'warpEx' appears to be a misspelling. Consider renaming to 'wrappedEx' or 'wrapEx' for clarity.

Copilot uses AI. Check for mistakes.
Copy link

Copilot AI Nov 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected spelling of 'warp' to 'wrap' in variable name 'warpEx' to 'wrapEx'.

Copilot uses AI. Check for mistakes.

throw new PublisherSentFailedException("NATS message send failed, no consumer reply!");
return OperateResult.Failed(warpEx);
}
Comment on lines +59 to +64
Copy link

Copilot AI Oct 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generic catch clause.

Copilot uses AI. Check for mistakes.
finally
{
_connectionPool.Return(connection);
}
}
catch (Exception ex)
catch (Exception e)
{
var warpEx = new PublisherSentFailedException(ex.Message, ex);

var warpEx = new PublisherSentFailedException(e.Message, e);
Copy link

Copilot AI Oct 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable name 'warpEx' appears to be a misspelling. Consider renaming to 'wrappedEx' or 'wrapEx' for clarity.

Copilot uses AI. Check for mistakes.
Copy link

Copilot AI Nov 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected spelling of 'warp' to 'wrap' in variable name 'warpEx' to 'wrapEx'.

Copilot uses AI. Check for mistakes.
return OperateResult.Failed(warpEx);
}
Comment on lines +70 to 74
Copy link

Copilot AI Oct 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generic catch clause.

Copilot uses AI. Check for mistakes.
Comment on lines +70 to 74
Copy link

Copilot AI Nov 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The outer catch block at lines 70-74 will never catch exceptions from RentConnection() because the connection variable used in the finally block at line 67 would be uninitialized if RentConnection() throws. This creates a potential compilation error or null reference issue. Consider initializing connection to null before the try block and adding a null check in the finally block, similar to the RabbitMQ implementation pattern.

Copilot uses AI. Check for mistakes.
finally
{
_connectionPool.Return(connection);
}
}
}
}
Loading