Skip to content

Commit 2cb7e09

Browse files
committed
addressing change made in server 2.4rc0.
1 parent 3e1f52f commit 2cb7e09

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

MongoDB.Driver/CommandResults/CommandResult.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ public CommandResult(IMongoCommand command, BsonDocument response)
5050
}
5151

5252
// public properties
53+
/// <summary>
54+
/// Gets the code.
55+
/// </summary>
56+
public int? Code
57+
{
58+
get { return _response.GetValue("code", null).AsNullableInt32; }
59+
}
60+
5361
/// <summary>
5462
/// Gets the command.
5563
/// </summary>

MongoDB.Driver/Communication/Security/SaslAuthenticationProtocol.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,24 @@ public void Authenticate(MongoConnection connection, MongoCredential credential)
7070

7171
while (true)
7272
{
73-
var result = connection.RunCommand(credential.Source, QueryFlags.SlaveOk, command, true);
74-
var code = result.Response["code"].AsInt32;
75-
if (code != 0)
73+
CommandResult result;
74+
try
7675
{
77-
HandleError(result, code);
76+
result = connection.RunCommand(credential.Source, QueryFlags.SlaveOk, command, true);
7877
}
78+
catch (MongoCommandException ex)
79+
{
80+
var message = "Unknown error occured during authentication.";
81+
var code = ex.CommandResult.Code;
82+
var errmsg = ex.CommandResult.ErrorMessage;
83+
if(code.HasValue && errmsg != null)
84+
{
85+
message = string.Format("Error: {0} - {1}", code, errmsg);
86+
}
87+
88+
throw new MongoSecurityException(message, ex);
89+
}
90+
7991
if (result.Response["done"].AsBoolean)
8092
{
8193
break;
@@ -104,11 +116,5 @@ public bool CanUse(MongoCredential credential)
104116
{
105117
return _mechanism.CanUse(credential);
106118
}
107-
108-
// private methods
109-
private void HandleError(CommandResult result, int code)
110-
{
111-
throw new MongoSecurityException(string.Format("Error: {0} - {1}", code, result.Response["errmsg"].AsString));
112-
}
113119
}
114120
}

0 commit comments

Comments
 (0)