Skip to content

Conversation

@witskeeper
Copy link

@witskeeper witskeeper commented Oct 30, 2025

Description:

When _connectionPool.RentConnection() throw exception

The publisher.PublishAsync will throw exception.

Issue(s) addressed:

  • [Link to the related issue(s), if any]

Changes:

  • List the major changes made in this PR, preferably in bullet points.

Affected components:

  • List the components of the project that are affected by this PR.

How to test:

Provide a step-by-step guide on how to test your changes. Include any relevant information like environment setup, dependencies, etc.

Additional notes (optional):

Provide any additional notes or context that may be relevant to the changes.

Checklist:

  • I have tested my changes locally
  • I have added necessary documentation (if applicable)
  • I have updated the relevant tests (if applicable)
  • My changes follow the project's code style guidelines

Reviewers:

  • Mention any reviewers you would like to review your PR. This can be helpful if you know someone who is familiar with the part of the codebase you're working on.

When `_connectionPool.RentConnection() ` throw exception

The `publisher.PublishAsync` will throw exception,
@yang-xiaodong yang-xiaodong requested a review from Copilot October 31, 2025 10:03
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors the exception handling in the SendAsync method by introducing nested try-catch blocks. The connection acquisition is moved inside the outer try block, and a new outer catch handler is added to handle exceptions during connection rental.

Key changes:

  • Connection rental moved from outside try-catch into outer try block
  • Added nested try-catch-finally structure with inner block handling message sending and outer block handling connection rental failures
  • Outer catch block uses different exception variable name (e vs ex)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 33 to 37
try
{
var msg = new Msg(message.GetName(), message.Body.ToArray());
foreach (var header in message.Headers)
var connection = _connectionPool.RentConnection();
try
{
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.
}
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.
{
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.
Comment on lines +70 to 74
catch (Exception e)
{
var warpEx = new PublisherSentFailedException(ex.Message, ex);

var warpEx = new PublisherSentFailedException(e.Message, e);
return OperateResult.Failed(warpEx);
}
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 +59 to +64
catch (Exception ex)
{
var warpEx = new PublisherSentFailedException(ex.Message, ex);

throw new PublisherSentFailedException("NATS message send failed, no consumer reply!");
return OperateResult.Failed(warpEx);
}
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant