Skip to content

Commit

Permalink
Fix some exception handling (#5704)
Browse files Browse the repository at this point in the history
Preserves the original stacktrace when AsyncResult is throwing stored exception

Fix order of null checks so that SocketConnection actually throws stored exception when failing to connect
  • Loading branch information
mconnew authored Dec 5, 2024
1 parent 783e979 commit 871981f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -969,17 +969,17 @@ public async ValueTask<IConnection> ConnectAsync(Uri uri, TimeSpan timeout)
}
}

if (socketConnection == null)
if (lastException != null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
new EndpointNotFoundException(SR.Format(SR.NoIPEndpointsFoundForHost, uri.Host)));
SocketConnectionInitiator.ConvertConnectException(lastException, uri,
timeoutHelper.ElapsedTime(), lastException));
}

if (lastException != null)
if (socketConnection == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
SocketConnectionInitiator.ConvertConnectException(lastException, uri,
timeoutHelper.ElapsedTime(), lastException));
new EndpointNotFoundException(SR.Format(SR.NoIPEndpointsFoundForHost, uri.Host)));
}

return socketConnection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.


using System.Runtime.ExceptionServices;
using System.Threading;

namespace System.Runtime
Expand Down Expand Up @@ -348,7 +349,7 @@ protected static TAsyncResult End<TAsyncResult>(IAsyncResult result)

if (asyncResult._exception != null)
{
throw Fx.Exception.AsError(asyncResult._exception);
ExceptionDispatchInfo.Capture(Fx.Exception.AsError(asyncResult._exception)).Throw();
}

return asyncResult;
Expand Down

0 comments on commit 871981f

Please sign in to comment.