Skip to content

Commit 95f8358

Browse files
committed
CSHARP-3990: Use throw; instead of throw ex; when re-throwing an unwrapped exception in BinaryConnection.
1 parent 08689b6 commit 95f8358

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

src/MongoDB.Driver.Core/Core/Connections/BinaryConnection.cs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,9 @@ private void OpenHelper(CancellationToken cancellationToken)
299299
catch (Exception ex)
300300
{
301301
_description ??= handshakeDescription;
302-
var wrappedException = WrapException(ex, "opening a connection to the server");
303-
helper.FailedOpeningConnection(wrappedException);
304-
throw wrappedException;
302+
var wrappedException = WrapExceptionIfRequired(ex, "opening a connection to the server");
303+
helper.FailedOpeningConnection(wrappedException ?? ex);
304+
if (wrappedException == null) { throw; } else { throw wrappedException; }
305305
}
306306
}
307307

@@ -324,9 +324,9 @@ private async Task OpenHelperAsync(CancellationToken cancellationToken)
324324
catch (Exception ex)
325325
{
326326
_description ??= handshakeDescription;
327-
var wrappedException = WrapException(ex, "opening a connection to the server");
328-
helper.FailedOpeningConnection(wrappedException);
329-
throw wrappedException;
327+
var wrappedException = WrapExceptionIfRequired(ex, "opening a connection to the server");
328+
helper.FailedOpeningConnection(wrappedException ?? ex);
329+
if (wrappedException == null) { throw; } else { throw wrappedException; }
330330
}
331331
}
332332

@@ -349,9 +349,9 @@ private IByteBuffer ReceiveBuffer(CancellationToken cancellationToken)
349349
}
350350
catch (Exception ex)
351351
{
352-
var wrappedException = WrapException(ex, "receiving a message from the server");
353-
ConnectionFailed(wrappedException);
354-
throw wrappedException;
352+
var wrappedException = WrapExceptionIfRequired(ex, "receiving a message from the server");
353+
ConnectionFailed(wrappedException ?? ex);
354+
if (wrappedException == null) { throw; } else { throw wrappedException; }
355355
}
356356
}
357357

@@ -419,9 +419,9 @@ private async Task<IByteBuffer> ReceiveBufferAsync(CancellationToken cancellatio
419419
}
420420
catch (Exception ex)
421421
{
422-
var wrappedException = WrapException(ex, "receiving a message from the server");
423-
ConnectionFailed(wrappedException);
424-
throw wrappedException;
422+
var wrappedException = WrapExceptionIfRequired(ex, "receiving a message from the server");
423+
ConnectionFailed(wrappedException ?? ex);
424+
if (wrappedException == null) { throw; } else { throw wrappedException; }
425425
}
426426
}
427427

@@ -492,7 +492,8 @@ public ResponseMessage ReceiveMessage(
492492
catch (Exception ex)
493493
{
494494
helper.FailedReceivingMessage(ex);
495-
throw WrapInOperationCanceledExceptionIfRequired(ex, cancellationToken);
495+
ThrowOperationCanceledExceptionIfRequired(ex);
496+
throw;
496497
}
497498
}
498499

@@ -519,7 +520,8 @@ public async Task<ResponseMessage> ReceiveMessageAsync(
519520
catch (Exception ex)
520521
{
521522
helper.FailedReceivingMessage(ex);
522-
throw WrapInOperationCanceledExceptionIfRequired(ex, cancellationToken);
523+
ThrowOperationCanceledExceptionIfRequired(ex);
524+
throw;
523525
}
524526
}
525527

@@ -540,9 +542,9 @@ private void SendBuffer(IByteBuffer buffer, CancellationToken cancellationToken)
540542
}
541543
catch (Exception ex)
542544
{
543-
var wrappedException = WrapException(ex, "sending a message to the server");
544-
ConnectionFailed(wrappedException);
545-
throw wrappedException;
545+
var wrappedException = WrapExceptionIfRequired(ex, "sending a message to the server");
546+
ConnectionFailed(wrappedException ?? ex);
547+
if (wrappedException == null) { throw; } else { throw wrappedException; }
546548
}
547549
}
548550
finally
@@ -569,9 +571,9 @@ private async Task SendBufferAsync(IByteBuffer buffer, CancellationToken cancell
569571
}
570572
catch (Exception ex)
571573
{
572-
var wrappedException = WrapException(ex, "sending a message to the server");
573-
ConnectionFailed(wrappedException);
574-
throw wrappedException;
574+
var wrappedException = WrapExceptionIfRequired(ex, "sending a message to the server");
575+
ConnectionFailed(wrappedException ?? ex);
576+
if (wrappedException == null) { throw; } else { throw wrappedException; }
575577
}
576578
}
577579
finally
@@ -612,7 +614,8 @@ public void SendMessages(IEnumerable<RequestMessage> messages, MessageEncoderSet
612614
catch (Exception ex)
613615
{
614616
helper.FailedSendingMessages(ex);
615-
throw WrapInOperationCanceledExceptionIfRequired(ex, cancellationToken);
617+
ThrowOperationCanceledExceptionIfRequired(ex);
618+
throw;
616619
}
617620
}
618621

@@ -648,7 +651,8 @@ public async Task SendMessagesAsync(IEnumerable<RequestMessage> messages, Messag
648651
catch (Exception ex)
649652
{
650653
helper.FailedSendingMessages(ex);
651-
throw WrapInOperationCanceledExceptionIfRequired(ex, cancellationToken);
654+
ThrowOperationCanceledExceptionIfRequired(ex);
655+
throw;
652656
}
653657
}
654658

@@ -744,7 +748,7 @@ private void ThrowIfDisposed()
744748
}
745749
}
746750

747-
private Exception WrapException(Exception ex, string action)
751+
private Exception WrapExceptionIfRequired(Exception ex, string action)
748752
{
749753
if (
750754
ex is ThreadAbortException ||
@@ -754,7 +758,7 @@ ex is OutOfMemoryException ||
754758
ex is OperationCanceledException ||
755759
ex is ObjectDisposedException)
756760
{
757-
return ex;
761+
return null;
758762
}
759763
else
760764
{
@@ -763,7 +767,7 @@ ex is OperationCanceledException ||
763767
}
764768
}
765769

766-
private Exception WrapInOperationCanceledExceptionIfRequired(Exception exception, CancellationToken cancellationToken)
770+
private void ThrowOperationCanceledExceptionIfRequired(Exception exception)
767771
{
768772
if (exception is ObjectDisposedException objectDisposedException)
769773
{
@@ -772,11 +776,7 @@ private Exception WrapInOperationCanceledExceptionIfRequired(Exception exception
772776
// objectDisposedException.Message == "The semaphore has been disposed."
773777
// but since the last one is language-specific, the only option we have is avoiding any additional conditions for ObjectDisposedException
774778
// TODO: this logic should be reviewed in the scope of https://jira.mongodb.org/browse/CSHARP-3165
775-
return new OperationCanceledException($"The {nameof(BinaryConnection)} operation has been cancelled.", exception);
776-
}
777-
else
778-
{
779-
return exception;
779+
throw new OperationCanceledException($"The {nameof(BinaryConnection)} operation has been cancelled.", exception);
780780
}
781781
}
782782

0 commit comments

Comments
 (0)