Skip to content

Commit fdfbedc

Browse files
committed
CSHARP-1192: When wrapping connection exceptions in a MongoConnectionException don't wrap ThreadAbortException, StackOverflowException or OutOfMemoryException.
1 parent 1c6fa13 commit fdfbedc

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ private async Task OpenAsyncHelper(CancellationToken cancellationToken)
214214
{
215215
_state.TryChange(State.Failed);
216216

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");
218218

219219
if (_listener != null)
220220
{
@@ -244,7 +244,7 @@ private async Task<IByteBuffer> ReceiveBufferAsync()
244244
}
245245
catch (Exception ex)
246246
{
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");
248248
ConnectionFailed(wrappedException);
249249
throw wrappedException;
250250
}
@@ -357,7 +357,7 @@ private async Task SendBufferAsync(IByteBuffer buffer, CancellationToken cancell
357357
}
358358
catch (Exception ex)
359359
{
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");
361361
ConnectionFailed(wrappedException);
362362
throw wrappedException;
363363
}
@@ -448,6 +448,19 @@ private void ThrowIfDisposedOrNotOpen()
448448
}
449449
}
450450

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+
451464
// nested classes
452465
private static class State
453466
{

0 commit comments

Comments
 (0)