@@ -214,7 +214,7 @@ private async Task OpenAsyncHelper(CancellationToken cancellationToken)
214
214
{
215
215
_state . TryChange ( State . Failed ) ;
216
216
217
- var wrappedException = new MongoConnectionException ( _connectionId , "An exception occurred while opening a connection to the server." , ex ) ;
217
+ var wrappedException = WrapException ( ex , "opening a connection to the server" ) ;
218
218
219
219
if ( _listener != null )
220
220
{
@@ -244,7 +244,7 @@ private async Task<IByteBuffer> ReceiveBufferAsync()
244
244
}
245
245
catch ( Exception ex )
246
246
{
247
- var wrappedException = new MongoConnectionException ( _connectionId , "An exception occurred while receiving a message from the server." , ex ) ;
247
+ var wrappedException = WrapException ( ex , "receiving a message from the server" ) ;
248
248
ConnectionFailed ( wrappedException ) ;
249
249
throw wrappedException ;
250
250
}
@@ -357,7 +357,7 @@ private async Task SendBufferAsync(IByteBuffer buffer, CancellationToken cancell
357
357
}
358
358
catch ( Exception ex )
359
359
{
360
- var wrappedException = new MongoConnectionException ( _connectionId , "An exception occurred while sending a message to the server." , ex ) ;
360
+ var wrappedException = WrapException ( ex , "sending a message to the server" ) ;
361
361
ConnectionFailed ( wrappedException ) ;
362
362
throw wrappedException ;
363
363
}
@@ -448,6 +448,19 @@ private void ThrowIfDisposedOrNotOpen()
448
448
}
449
449
}
450
450
451
+ private Exception WrapException ( Exception ex , string action )
452
+ {
453
+ if ( ex is ThreadAbortException || ex is StackOverflowException || ex is OutOfMemoryException )
454
+ {
455
+ return ex ;
456
+ }
457
+ else
458
+ {
459
+ var message = string . Format ( "An exception occurred while {0}." , action ) ;
460
+ return new MongoConnectionException ( _connectionId , message , ex ) ;
461
+ }
462
+ }
463
+
451
464
// nested classes
452
465
private static class State
453
466
{
0 commit comments