@@ -299,9 +299,9 @@ private void OpenHelper(CancellationToken cancellationToken)
299
299
catch ( Exception ex )
300
300
{
301
301
_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 ; }
305
305
}
306
306
}
307
307
@@ -324,9 +324,9 @@ private async Task OpenHelperAsync(CancellationToken cancellationToken)
324
324
catch ( Exception ex )
325
325
{
326
326
_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 ; }
330
330
}
331
331
}
332
332
@@ -349,9 +349,9 @@ private IByteBuffer ReceiveBuffer(CancellationToken cancellationToken)
349
349
}
350
350
catch ( Exception ex )
351
351
{
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 ; }
355
355
}
356
356
}
357
357
@@ -419,9 +419,9 @@ private async Task<IByteBuffer> ReceiveBufferAsync(CancellationToken cancellatio
419
419
}
420
420
catch ( Exception ex )
421
421
{
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 ; }
425
425
}
426
426
}
427
427
@@ -492,7 +492,8 @@ public ResponseMessage ReceiveMessage(
492
492
catch ( Exception ex )
493
493
{
494
494
helper . FailedReceivingMessage ( ex ) ;
495
- throw WrapInOperationCanceledExceptionIfRequired ( ex , cancellationToken ) ;
495
+ ThrowOperationCanceledExceptionIfRequired ( ex ) ;
496
+ throw ;
496
497
}
497
498
}
498
499
@@ -519,7 +520,8 @@ public async Task<ResponseMessage> ReceiveMessageAsync(
519
520
catch ( Exception ex )
520
521
{
521
522
helper . FailedReceivingMessage ( ex ) ;
522
- throw WrapInOperationCanceledExceptionIfRequired ( ex , cancellationToken ) ;
523
+ ThrowOperationCanceledExceptionIfRequired ( ex ) ;
524
+ throw ;
523
525
}
524
526
}
525
527
@@ -540,9 +542,9 @@ private void SendBuffer(IByteBuffer buffer, CancellationToken cancellationToken)
540
542
}
541
543
catch ( Exception ex )
542
544
{
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 ; }
546
548
}
547
549
}
548
550
finally
@@ -569,9 +571,9 @@ private async Task SendBufferAsync(IByteBuffer buffer, CancellationToken cancell
569
571
}
570
572
catch ( Exception ex )
571
573
{
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 ; }
575
577
}
576
578
}
577
579
finally
@@ -612,7 +614,8 @@ public void SendMessages(IEnumerable<RequestMessage> messages, MessageEncoderSet
612
614
catch ( Exception ex )
613
615
{
614
616
helper . FailedSendingMessages ( ex ) ;
615
- throw WrapInOperationCanceledExceptionIfRequired ( ex , cancellationToken ) ;
617
+ ThrowOperationCanceledExceptionIfRequired ( ex ) ;
618
+ throw ;
616
619
}
617
620
}
618
621
@@ -648,7 +651,8 @@ public async Task SendMessagesAsync(IEnumerable<RequestMessage> messages, Messag
648
651
catch ( Exception ex )
649
652
{
650
653
helper . FailedSendingMessages ( ex ) ;
651
- throw WrapInOperationCanceledExceptionIfRequired ( ex , cancellationToken ) ;
654
+ ThrowOperationCanceledExceptionIfRequired ( ex ) ;
655
+ throw ;
652
656
}
653
657
}
654
658
@@ -744,7 +748,7 @@ private void ThrowIfDisposed()
744
748
}
745
749
}
746
750
747
- private Exception WrapException ( Exception ex , string action )
751
+ private Exception WrapExceptionIfRequired ( Exception ex , string action )
748
752
{
749
753
if (
750
754
ex is ThreadAbortException ||
@@ -754,7 +758,7 @@ ex is OutOfMemoryException ||
754
758
ex is OperationCanceledException ||
755
759
ex is ObjectDisposedException )
756
760
{
757
- return ex ;
761
+ return null ;
758
762
}
759
763
else
760
764
{
@@ -763,7 +767,7 @@ ex is OperationCanceledException ||
763
767
}
764
768
}
765
769
766
- private Exception WrapInOperationCanceledExceptionIfRequired ( Exception exception , CancellationToken cancellationToken )
770
+ private void ThrowOperationCanceledExceptionIfRequired ( Exception exception )
767
771
{
768
772
if ( exception is ObjectDisposedException objectDisposedException )
769
773
{
@@ -772,11 +776,7 @@ private Exception WrapInOperationCanceledExceptionIfRequired(Exception exception
772
776
// objectDisposedException.Message == "The semaphore has been disposed."
773
777
// but since the last one is language-specific, the only option we have is avoiding any additional conditions for ObjectDisposedException
774
778
// 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 ) ;
780
780
}
781
781
}
782
782
0 commit comments