Skip to content

Commit 270f03c

Browse files
committed
Handling command responses that are missing "ok" and/or "errmsg" fields.
JAVA-1811
1 parent 8d77e54 commit 270f03c

File tree

4 files changed

+74
-12
lines changed

4 files changed

+74
-12
lines changed

driver-core/src/main/com/mongodb/MongoCommandException.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,10 @@ private static String getResponseAsJson(final BsonDocument commandResponse) {
8585
}
8686

8787
private static int extractErrorCode(final BsonDocument response) {
88-
if (response.containsKey("code")) {
89-
return ((BsonInt32) response.get("code")).getValue();
90-
} else {
91-
return -1;
92-
}
88+
return response.getNumber("code", new BsonInt32(-1)).intValue();
9389
}
9490

9591
private static String extractErrorMessage(final BsonDocument response) {
96-
if (response.containsKey("errmsg")) {
97-
return ((BsonString) response.get("errmsg")).getValue();
98-
} else {
99-
return null;
100-
}
92+
return response.getString("errmsg", new BsonString("")).getValue();
10193
}
10294
}

driver-core/src/main/com/mongodb/connection/ProtocolHelper.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ private static WriteConcernResult createWriteResult(final BsonDocument result) {
5858

5959
static boolean isCommandOk(final BsonDocument response) {
6060
BsonValue okValue = response.get("ok");
61-
if (okValue.isBoolean()) {
61+
if (okValue == null) {
62+
return false;
63+
} else if (okValue.isBoolean()) {
6264
return okValue.asBoolean().getValue();
6365
} else if (okValue.isNumber()) {
6466
return okValue.asNumber().intValue() == 1;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2015 MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mongodb
18+
19+
import org.bson.BsonBoolean
20+
import org.bson.BsonDocument
21+
import org.bson.BsonInt32
22+
import org.bson.BsonString
23+
import spock.lang.Specification
24+
25+
class MongoCommandExceptionSpecification extends Specification {
26+
27+
def 'should extract error message'() {
28+
expect:
29+
new MongoCommandException(new BsonDocument('ok', BsonBoolean.FALSE).append('errmsg', new BsonString('the error message')),
30+
new ServerAddress())
31+
.getErrorMessage() == 'the error message'
32+
new MongoCommandException(new BsonDocument('ok', BsonBoolean.FALSE),
33+
new ServerAddress())
34+
.getErrorMessage() == ''
35+
}
36+
37+
def 'should extract error code'() {
38+
expect:
39+
new MongoCommandException(new BsonDocument('ok', BsonBoolean.FALSE).append('code', new BsonInt32(26)),
40+
new ServerAddress())
41+
.getErrorCode() == 26
42+
new MongoCommandException(new BsonDocument('ok', BsonBoolean.FALSE),
43+
new ServerAddress())
44+
.getErrorCode() == -1
45+
}
46+
47+
}

driver-core/src/test/unit/com/mongodb/connection/ProtocolHelperSpecification.groovy

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2014-2015 MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package com.mongodb.connection
218

319
import com.mongodb.MongoCommandException
@@ -25,6 +41,11 @@ import static com.mongodb.connection.ProtocolHelper.isCommandOk
2541

2642
class ProtocolHelperSpecification extends Specification {
2743

44+
def 'isCommandOk should be false if ok field is missing'() {
45+
expect:
46+
!isCommandOk(new BsonDocument())
47+
}
48+
2849
def 'isCommandOk should be false for numbers that are 0'() {
2950
expect:
3051
!isCommandOk(new BsonDocument('ok', new BsonInt32(0)))
@@ -158,4 +179,4 @@ class ProtocolHelperSpecification extends Specification {
158179
then:
159180
thrown(WriteConcernException)
160181
}
161-
}
182+
}

0 commit comments

Comments
 (0)