Skip to content

Commit 49ab93b

Browse files
committed
CSHARP-2296: MongoNotPrimaryException and MongoNodeIsRecoveringException should derive from MongoCommandException.
1 parent 21aff04 commit 49ab93b

11 files changed

+36
-83
lines changed

src/MongoDB.Driver.Core/Core/Misc/ExceptionMapper.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,11 @@ public static Exception Map(ConnectionId connectionId, WriteConcernResult writeC
113113
/// Maps the server response to a MongoNotPrimaryException or MongoNodeIsRecoveringException (if appropriate).
114114
/// </summary>
115115
/// <param name="connectionId">The connection identifier.</param>
116+
/// <param name="command">The command.</param>
116117
/// <param name="response">The server response.</param>
117118
/// <param name="errorMessageFieldName">Name of the error message field.</param>
118119
/// <returns>The exception, or null if no exception necessary.</returns>
119-
public static Exception MapNotPrimaryOrNodeIsRecovering(ConnectionId connectionId, BsonDocument response, string errorMessageFieldName)
120+
public static Exception MapNotPrimaryOrNodeIsRecovering(ConnectionId connectionId, BsonDocument command, BsonDocument response, string errorMessageFieldName)
120121
{
121122
BsonValue codeBsonValue;
122123
if (response.TryGetValue("code", out codeBsonValue) && codeBsonValue.IsNumeric)
@@ -126,14 +127,14 @@ public static Exception MapNotPrimaryOrNodeIsRecovering(ConnectionId connectionI
126127
{
127128
case ServerErrorCode.NotMaster:
128129
case ServerErrorCode.NotMasterNoSlaveOk:
129-
return new MongoNotPrimaryException(connectionId, response);
130+
return new MongoNotPrimaryException(connectionId, command, response);
130131

131132
case ServerErrorCode.InterruptedAtShutdown:
132133
case ServerErrorCode.InterruptedDueToReplStateChange:
133134
case ServerErrorCode.NotMasterOrSecondary:
134135
case ServerErrorCode.PrimarySteppedDown:
135136
case ServerErrorCode.ShutdownInProgress:
136-
return new MongoNodeIsRecoveringException(connectionId, response);
137+
return new MongoNodeIsRecoveringException(connectionId, command, response);
137138
}
138139
}
139140

@@ -144,11 +145,11 @@ public static Exception MapNotPrimaryOrNodeIsRecovering(ConnectionId connectionI
144145
if (errorMessage.IndexOf("node is recovering", StringComparison.OrdinalIgnoreCase) != -1 ||
145146
errorMessage.IndexOf("not master or secondary", StringComparison.OrdinalIgnoreCase) != -1)
146147
{
147-
return new MongoNodeIsRecoveringException(connectionId, response);
148+
return new MongoNodeIsRecoveringException(connectionId, command, response);
148149
}
149150
else if (errorMessage.IndexOf("not master", StringComparison.OrdinalIgnoreCase) != -1)
150151
{
151-
return new MongoNotPrimaryException(connectionId, response);
152+
return new MongoNotPrimaryException(connectionId, command, response);
152153
}
153154
}
154155

src/MongoDB.Driver.Core/Core/WireProtocol/CommandUsingCommandMessageWireProtocol.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ private TCommandResult ProcessResponse(ConnectionId connectionId, CommandMessage
260260
commandName = _command["$query"].AsBsonDocument.GetElement(0).Name;
261261
}
262262

263-
var notPrimaryOrNodeIsRecoveringException = ExceptionMapper.MapNotPrimaryOrNodeIsRecovering(connectionId, materializedDocument, "errmsg");
263+
var notPrimaryOrNodeIsRecoveringException = ExceptionMapper.MapNotPrimaryOrNodeIsRecovering(connectionId, _command, materializedDocument, "errmsg");
264264
if (notPrimaryOrNodeIsRecoveringException != null)
265265
{
266266
throw notPrimaryOrNodeIsRecoveringException;

src/MongoDB.Driver.Core/Core/WireProtocol/CommandUsingQueryMessageWireProtocol.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ private TCommandResult ProcessReply(ConnectionId connectionId, ReplyMessage<RawB
273273
commandName = _command["$query"].AsBsonDocument.GetElement(0).Name;
274274
}
275275

276-
var notPrimaryOrNodeIsRecoveringException = ExceptionMapper.MapNotPrimaryOrNodeIsRecovering(connectionId, materializedDocument, "errmsg");
276+
var notPrimaryOrNodeIsRecoveringException = ExceptionMapper.MapNotPrimaryOrNodeIsRecovering(connectionId, _command, materializedDocument, "errmsg");
277277
if (notPrimaryOrNodeIsRecoveringException != null)
278278
{
279279
throw notPrimaryOrNodeIsRecoveringException;

src/MongoDB.Driver.Core/Core/WireProtocol/QueryWireProtocol.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ private CursorBatch<TDocument> ProcessReply(ConnectionId connectionId, ReplyMess
120120
{
121121
var response = reply.QueryFailureDocument;
122122

123-
var notPrimaryOrNodeIsRecoveringException = ExceptionMapper.MapNotPrimaryOrNodeIsRecovering(connectionId, response, "$err");
123+
var notPrimaryOrNodeIsRecoveringException = ExceptionMapper.MapNotPrimaryOrNodeIsRecovering(connectionId, _query, response, "$err");
124124
if (notPrimaryOrNodeIsRecoveringException != null)
125125
{
126126
throw notPrimaryOrNodeIsRecoveringException;

src/MongoDB.Driver.Core/Core/WireProtocol/WriteWireProtocolBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ private WriteConcernResult ProcessReply(ConnectionId connectionId, BsonDocument
160160

161161
var response = reply.Documents.Single();
162162

163-
var notPrimaryOrNodeIsRecoveringException = ExceptionMapper.MapNotPrimaryOrNodeIsRecovering(connectionId, response, "err");
163+
var notPrimaryOrNodeIsRecoveringException = ExceptionMapper.MapNotPrimaryOrNodeIsRecovering(connectionId, getLastErrorCommand, response, "err");
164164
if (notPrimaryOrNodeIsRecoveringException != null)
165165
{
166166
throw notPrimaryOrNodeIsRecoveringException;

src/MongoDB.Driver.Core/MongoNodeIsRecoveringException.cs

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
using System.Runtime.Serialization;
1919
#endif
2020
using MongoDB.Bson;
21-
using MongoDB.Bson.Serialization;
2221
using MongoDB.Driver.Core.Connections;
23-
using MongoDB.Driver.Core.Misc;
2422

2523
namespace MongoDB.Driver
2624
{
@@ -30,7 +28,7 @@ namespace MongoDB.Driver
3028
#if NET45
3129
[Serializable]
3230
#endif
33-
public class MongoNodeIsRecoveringException : MongoServerException
31+
public class MongoNodeIsRecoveringException : MongoCommandException
3432
{
3533
#region static
3634
// private static methods
@@ -50,19 +48,16 @@ private static string CreateMessage(BsonDocument result)
5048
}
5149
#endregion
5250

53-
// fields
54-
private readonly BsonDocument _result;
55-
5651
// constructors
5752
/// <summary>
5853
/// Initializes a new instance of the <see cref="MongoNodeIsRecoveringException"/> class.
5954
/// </summary>
6055
/// <param name="connectionId">The connection identifier.</param>
56+
/// <param name="command">The command.</param>
6157
/// <param name="result">The result.</param>
62-
public MongoNodeIsRecoveringException(ConnectionId connectionId, BsonDocument result)
63-
: base(connectionId, CreateMessage(result))
58+
public MongoNodeIsRecoveringException(ConnectionId connectionId, BsonDocument command, BsonDocument result)
59+
: base(connectionId, CreateMessage(result), command, result)
6460
{
65-
_result = Ensure.IsNotNull(result, nameof(result));
6661
}
6762

6863
#if NET45
@@ -74,29 +69,6 @@ public MongoNodeIsRecoveringException(ConnectionId connectionId, BsonDocument re
7469
protected MongoNodeIsRecoveringException(SerializationInfo info, StreamingContext context)
7570
: base(info, context)
7671
{
77-
_result = (BsonDocument)info.GetValue("_result", typeof(BsonDocument));
78-
}
79-
#endif
80-
81-
// properties
82-
/// <summary>
83-
/// Gets the result from the server.
84-
/// </summary>
85-
/// <value>
86-
/// The result from the server.
87-
/// </value>
88-
public BsonDocument Result
89-
{
90-
get { return _result; }
91-
}
92-
93-
// methods
94-
#if NET45
95-
/// <inheritdoc/>
96-
public override void GetObjectData(SerializationInfo info, StreamingContext context)
97-
{
98-
base.GetObjectData(info, context);
99-
info.AddValue("_result", _result);
10072
}
10173
#endif
10274
}

src/MongoDB.Driver.Core/MongoNotPrimaryException.cs

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
using System.Runtime.Serialization;
1919
#endif
2020
using MongoDB.Bson;
21-
using MongoDB.Bson.Serialization;
2221
using MongoDB.Driver.Core.Connections;
23-
using MongoDB.Driver.Core.Misc;
2422

2523
namespace MongoDB.Driver
2624
{
@@ -30,21 +28,18 @@ namespace MongoDB.Driver
3028
#if NET45
3129
[Serializable]
3230
#endif
33-
public class MongoNotPrimaryException : MongoServerException
31+
public class MongoNotPrimaryException : MongoCommandException
3432
{
35-
// fields
36-
private readonly BsonDocument _result;
37-
3833
// constructors
3934
/// <summary>
4035
/// Initializes a new instance of the <see cref="MongoNotPrimaryException"/> class.
4136
/// </summary>
4237
/// <param name="connectionId">The connection identifier.</param>
38+
/// <param name="command">The command.</param>
4339
/// <param name="result">The result.</param>
44-
public MongoNotPrimaryException(ConnectionId connectionId, BsonDocument result)
45-
: base(connectionId, "Server returned not master error.")
40+
public MongoNotPrimaryException(ConnectionId connectionId, BsonDocument command, BsonDocument result)
41+
: base(connectionId, "Server returned not master error.", command, result)
4642
{
47-
_result = Ensure.IsNotNull(result, nameof(result));
4843
}
4944

5045
#if NET45
@@ -56,29 +51,6 @@ public MongoNotPrimaryException(ConnectionId connectionId, BsonDocument result)
5651
protected MongoNotPrimaryException(SerializationInfo info, StreamingContext context)
5752
: base(info, context)
5853
{
59-
_result = (BsonDocument)info.GetValue("_result", typeof(BsonDocument));
60-
}
61-
#endif
62-
63-
// properties
64-
/// <summary>
65-
/// Gets the result from the server.
66-
/// </summary>
67-
/// <value>
68-
/// The result from the server.
69-
/// </value>
70-
public BsonDocument Result
71-
{
72-
get { return _result; }
73-
}
74-
75-
// methods
76-
#if NET45
77-
/// <inheritdoc/>
78-
public override void GetObjectData(SerializationInfo info, StreamingContext context)
79-
{
80-
base.GetObjectData(info, context);
81-
info.AddValue("_result", _result);
8254
}
8355
#endif
8456
}

tests/MongoDB.Driver.Core.TestHelpers/CoreExceptionHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static Exception CreateException(Type exceptionType)
5858
var serverId = new ServerId(clusterId, new DnsEndPoint("localhost", 27017));
5959
var connectionId = new ConnectionId(serverId, 1);
6060
var result = new BsonDocument();
61-
return new MongoNodeIsRecoveringException(connectionId, result);
61+
return new MongoNodeIsRecoveringException(connectionId, null, result);
6262
}
6363

6464
case "MongoNotPrimaryException":
@@ -67,7 +67,7 @@ public static Exception CreateException(Type exceptionType)
6767
var serverId = new ServerId(clusterId, new DnsEndPoint("localhost", 27017));
6868
var connectionId = new ConnectionId(serverId, 1);
6969
var result = new BsonDocument();
70-
return new MongoNotPrimaryException(connectionId, result);
70+
return new MongoNotPrimaryException(connectionId, null, result);
7171
}
7272

7373
default:

tests/MongoDB.Driver.Core.Tests/Core/Misc/ExceptionMapperTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public void MapNotPrimaryOrNodeIsRecovering_should_return_expected_result_using_
199199
var response = BsonDocument.Parse("{ ok : 0, code : 0 }");
200200
response["code"] = code;
201201

202-
var result = ExceptionMapper.MapNotPrimaryOrNodeIsRecovering(connectionId, response, "errmsg");
202+
var result = ExceptionMapper.MapNotPrimaryOrNodeIsRecovering(connectionId, null, response, "errmsg");
203203

204204
result?.GetType().Should().Be(expectedExceptionType);
205205
}
@@ -218,7 +218,7 @@ public void MapNotPrimaryOrNodeIsRecovering_should_return_expected_result_using_
218218
var response = BsonDocument.Parse("{ ok : 0, errmsg : '' }");
219219
response["errmsg"] = errmsg;
220220

221-
var result = ExceptionMapper.MapNotPrimaryOrNodeIsRecovering(connectionId, response, "errmsg");
221+
var result = ExceptionMapper.MapNotPrimaryOrNodeIsRecovering(connectionId, null, response, "errmsg");
222222

223223
result?.GetType().Should().Be(expectedExceptionType);
224224
}

tests/MongoDB.Driver.Core.Tests/MongoNodeIsRecoveringExceptionTests.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,18 @@ namespace MongoDB.Driver
3131
public class MongoNodeIsRecoveringExceptionTests
3232
{
3333
private readonly ConnectionId _connectionId = new ConnectionId(new ServerId(new ClusterId(1), new DnsEndPoint("localhost", 27017)), 2).WithServerValue(3);
34+
private readonly BsonDocument _command = new BsonDocument("command", 1);
3435
private readonly BsonDocument _serverResult = new BsonDocument("result", 1);
3536

3637
[Fact]
3738
public void constructor_should_initialize_subject()
3839
{
39-
var result = new MongoNodeIsRecoveringException(_connectionId, _serverResult);
40+
var result = new MongoNodeIsRecoveringException(_connectionId, _command, _serverResult);
4041

4142
result.ConnectionId.Should().BeSameAs(_connectionId);
4243
result.InnerException.Should().BeNull();
4344
result.Message.Should().Be("Server returned node is recovering error (code = -1).");
45+
result.Command.Should().BeSameAs(_command);
4446
result.Result.Should().BeSameAs(_serverResult);
4547
}
4648

@@ -49,11 +51,12 @@ public void constructor_should_initialize_subject_when_result_contains_code()
4951
{
5052
var serverResult = BsonDocument.Parse("{ ok : 0, code : 1234 }");
5153

52-
var result = new MongoNodeIsRecoveringException(_connectionId, serverResult);
54+
var result = new MongoNodeIsRecoveringException(_connectionId, _command, serverResult);
5355

5456
result.ConnectionId.Should().BeSameAs(_connectionId);
5557
result.InnerException.Should().BeNull();
5658
result.Message.Should().Be("Server returned node is recovering error (code = 1234).");
59+
result.Command.Should().BeSameAs(_command);
5760
result.Result.Should().BeSameAs(serverResult);
5861
}
5962

@@ -62,19 +65,20 @@ public void constructor_should_initialize_subject_when_result_contains_code_and_
6265
{
6366
var serverResult = BsonDocument.Parse("{ ok : 0, code : 1234, codeName : 'some name' }");
6467

65-
var result = new MongoNodeIsRecoveringException(_connectionId, serverResult);
68+
var result = new MongoNodeIsRecoveringException(_connectionId, _command, serverResult);
6669

6770
result.ConnectionId.Should().BeSameAs(_connectionId);
6871
result.InnerException.Should().BeNull();
6972
result.Message.Should().Be("Server returned node is recovering error (code = 1234, codeName = \"some name\").");
73+
result.Command.Should().BeSameAs(_command);
7074
result.Result.Should().BeSameAs(serverResult);
7175
}
7276

7377
#if NET45
7478
[Fact]
7579
public void Serialization_should_work()
7680
{
77-
var subject = new MongoNodeIsRecoveringException(_connectionId, _serverResult);
81+
var subject = new MongoNodeIsRecoveringException(_connectionId, _command, _serverResult);
7882

7983
var formatter = new BinaryFormatter();
8084
using (var stream = new MemoryStream())
@@ -86,6 +90,7 @@ public void Serialization_should_work()
8690
rehydrated.ConnectionId.Should().Be(subject.ConnectionId);
8791
rehydrated.InnerException.Should().BeNull();
8892
rehydrated.Message.Should().Be(subject.Message);
93+
rehydrated.Command.Should().Be(subject.Command);
8994
rehydrated.Result.Should().Be(subject.Result);
9095
}
9196
}

0 commit comments

Comments
 (0)