Skip to content

Commit 807d02f

Browse files
committed
JAVA-2108: Check for both err and errmsg fields in WriteConcernException
Necessary since one of these can come from either a getlasterror command, which puts the error in the err field, or a normal command like createIndexes, which puts the error in the errmsg field.
1 parent a3d6b8e commit 807d02f

File tree

3 files changed

+71
-3
lines changed

3 files changed

+71
-3
lines changed

src/main/com/mongodb/WriteConcernException.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,13 @@ public ServerAddress getServerAddress() {
5757
* @return the error message
5858
*/
5959
public String getErrorMessage() {
60-
return commandResult.getString("err");
60+
if (commandResult.containsField("err")) {
61+
return commandResult.getString("err");
62+
} else if (commandResult.containsField("errmsg")) {
63+
return commandResult.getString("errmsg");
64+
} else {
65+
return null;
66+
}
6167
}
6268

6369
/**

src/test/com/mongodb/DBCollectionTest.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ public void testEnsureIndexExceptions(){
456456
collection.ensureIndex(new BasicDBObject("x", 1), new BasicDBObject("unique", true));
457457
}
458458

459-
@Test(expected = DuplicateKeyException.class)
459+
@Test
460460
public void testCreateIndexExceptions(){
461461
collection.insert(new BasicDBObject("x", 1));
462462
collection.insert(new BasicDBObject("x", 1));
@@ -468,7 +468,12 @@ public void testCreateIndexExceptions(){
468468
fail("Trying to create an existing index should not fail.");
469469
}
470470

471-
collection.createIndex(new BasicDBObject("x", 1), new BasicDBObject("unique", true));
471+
try {
472+
collection.createIndex(new BasicDBObject("x", 1), new BasicDBObject("unique", true));
473+
fail("Should throw an exception creating unique index when there are duplicate keys");
474+
} catch (DuplicateKeyException e) {
475+
assertNotNull("missing error message", e.getErrorMessage());
476+
}
472477
}
473478

474479
@Test
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2016 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+
18+
package com.mongodb;
19+
20+
import com.mongodb.util.TestCase;
21+
import org.codehaus.groovy.tools.shell.Command;
22+
import org.junit.Test;
23+
24+
import java.net.UnknownHostException;
25+
26+
import static junit.framework.TestCase.assertEquals;
27+
28+
public class DuplicateKeyExceptionTest extends TestCase {
29+
30+
@Test
31+
public void shouldGetErrorMessageFromErrField() throws UnknownHostException {
32+
// given
33+
String err = "This is an err";
34+
CommandResult result = new CommandResult(new ServerAddress());
35+
result.put("err", err);
36+
37+
// when
38+
DuplicateKeyException e = new DuplicateKeyException(result);
39+
40+
// then
41+
assertEquals(err, e.getErrorMessage());
42+
}
43+
44+
@Test
45+
public void shouldGetErrorMessageFromErrMsgField() throws UnknownHostException {
46+
// given
47+
String err = "This is an err";
48+
CommandResult result = new CommandResult(new ServerAddress());
49+
result.put("errmsg", err);
50+
51+
// when
52+
DuplicateKeyException e = new DuplicateKeyException(result);
53+
54+
// then
55+
assertEquals(err, e.getErrorMessage());
56+
}
57+
}

0 commit comments

Comments
 (0)