Skip to content

Commit daa7cbd

Browse files
authored
Improve error message clarity in MongoCommandException. (#1789)
JAVA-5905
1 parent cb0c38f commit daa7cbd

File tree

8 files changed

+17
-16
lines changed

8 files changed

+17
-16
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ private static String message(
8888
@Nullable final Map<Integer, WriteError> writeErrors,
8989
@Nullable final ClientBulkWriteResult partialResult,
9090
final ServerAddress serverAddress) {
91-
return "Client-level bulk write operation error on server " + serverAddress + "."
91+
return "Client-level bulk write operation error on MongoDB server " + serverAddress + "."
9292
+ (error == null ? "" : " Top-level error: " + error + ".")
9393
+ (writeErrors == null || writeErrors.isEmpty() ? "" : " Write errors: " + writeErrors + ".")
9494
+ (writeConcernErrors == null || writeConcernErrors.isEmpty() ? "" : " Write concern errors: " + writeConcernErrors + ".")

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class MongoBulkWriteException extends MongoServerException {
5353
public MongoBulkWriteException(final BulkWriteResult writeResult, final List<BulkWriteError> writeErrors,
5454
@Nullable final WriteConcernError writeConcernError, final ServerAddress serverAddress,
5555
final Set<String> errorLabels) {
56-
super("Bulk write operation error on server " + serverAddress + ". "
56+
super("Bulk write operation error on MongoDB server " + serverAddress + ". "
5757
+ (writeErrors.isEmpty() ? "" : "Write errors: " + writeErrors + ". ")
5858
+ (writeConcernError == null ? "" : "Write concern error: " + writeConcernError + ". "), serverAddress);
5959
this.writeResult = writeResult;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class MongoCommandException extends MongoServerException {
4848
*/
4949
public MongoCommandException(final BsonDocument response, final ServerAddress address) {
5050
super(extractErrorCode(response), extractErrorCodeName(response),
51-
format("Command failed with error %s: '%s' on server %s. The full response is %s", extractErrorCodeAndName(response),
51+
format("Command execution failed on MongoDB server with error %s: '%s' on server %s. The full response is %s", extractErrorCodeAndName(response),
5252
extractErrorMessage(response), address, getResponseAsJson(response)), address);
5353
this.response = response;
5454
addLabels(extractErrorLabelsAsBson(response));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public MongoWriteException(final WriteError error, final ServerAddress serverAdd
5050
* @since 5.0
5151
*/
5252
public MongoWriteException(final WriteError error, final ServerAddress serverAddress, final Collection<String> errorLabels) {
53-
super(error.getCode(), "Write operation error on server " + serverAddress + ". Write error: " + error + ".", serverAddress);
53+
super(error.getCode(), "Write operation error on MongoDB server " + serverAddress + ". Write error: " + error + ".", serverAddress);
5454
this.error = error;
5555
addLabels(errorLabels);
5656
}

driver-core/src/test/unit/com/mongodb/MongoCommandExceptionSpecification.groovy

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,12 @@ class MongoCommandExceptionSpecification extends Specification {
5656
new MongoCommandException(new BsonDocument('ok', BsonBoolean.FALSE).append('code', new BsonInt32(26))
5757
.append('codeName', new BsonString('TimeoutError')).append('errmsg', new BsonString('the error message')),
5858
new ServerAddress())
59-
.getMessage() == 'Command failed with error 26 (TimeoutError): \'the error message\' on server 127.0.0.1:27017. ' +
60-
'The full response is {"ok": false, "code": 26, "codeName": "TimeoutError", "errmsg": "the error message"}'
59+
.getMessage() == 'Command execution failed on MongoDB server with error 26 (TimeoutError): \'the error message\' ' +
60+
'on server 127.0.0.1:27017. The full response is {"ok": false, "code": 26, "codeName": "TimeoutError", ' +
61+
'"errmsg": "the error message"}'
6162
new MongoCommandException(new BsonDocument('ok', BsonBoolean.FALSE).append('code', new BsonInt32(26))
6263
.append('errmsg', new BsonString('the error message')), new ServerAddress())
63-
.getMessage() == 'Command failed with error 26: \'the error message\' on server 127.0.0.1:27017. ' +
64-
'The full response is {"ok": false, "code": 26, "errmsg": "the error message"}'
64+
.getMessage() == 'Command execution failed on MongoDB server with error 26: \'the error message\' ' +
65+
'on server 127.0.0.1:27017. The full response is {"ok": false, "code": 26, "errmsg": "the error message"}'
6566
}
6667
}

driver-core/src/test/unit/com/mongodb/MongoWriteExceptionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public void testExceptionProperties() {
3232
WriteError writeError = new WriteError(11000, "Duplicate key", new BsonDocument("x", new BsonInt32(1)));
3333
MongoWriteException e = new MongoWriteException(writeError, new ServerAddress("host1"), Collections.emptySet());
3434

35-
assertEquals("Write operation error on server host1:27017. Write error: WriteError{code=11000, message='Duplicate key', "
35+
assertEquals("Write operation error on MongoDB server host1:27017. Write error: WriteError{code=11000, message='Duplicate key', "
3636
+ "details={\"x\": 1}}.",
3737
e.getMessage());
3838
assertEquals(writeError.getCode(), e.getCode());

driver-legacy/src/main/com/mongodb/BulkWriteException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class BulkWriteException extends MongoServerException {
4646
*/
4747
BulkWriteException(final BulkWriteResult writeResult, final List<BulkWriteError> writeErrors,
4848
@Nullable final WriteConcernError writeConcernError, final ServerAddress serverAddress) {
49-
super("Bulk write operation error on server " + serverAddress + ". "
49+
super("Bulk write operation error on MongoDB server " + serverAddress + ". "
5050
+ (writeErrors.isEmpty() ? "" : "Write errors: " + writeErrors + ". ")
5151
+ (writeConcernError == null ? "" : "Write concern error: " + writeConcernError + ". "), serverAddress);
5252
this.writeResult = writeResult;

driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ public void test3p2AuthFailsWithoutCachedToken() {
406406
MongoClientSettings clientSettings = createSettings(callback);
407407
try (MongoClient mongoClient = createMongoClient(clientSettings)) {
408408
assertCause(MongoCommandException.class,
409-
"Command failed with error 18 (AuthenticationFailed):",
409+
"Command execution failed on MongoDB server with error 18 (AuthenticationFailed):",
410410
() -> performFind(mongoClient));
411411
}
412412
}
@@ -424,7 +424,7 @@ public void test3p3UnexpectedErrorDoesNotClearCache() {
424424
try (MongoClient mongoClient = createMongoClient(clientSettings)) {
425425
failCommand(20, 1, "saslStart");
426426
assertCause(MongoCommandException.class,
427-
"Command failed with error 20",
427+
"Command execution failed on MongoDB server with error 20",
428428
() -> performFind(mongoClient));
429429

430430
assertEquals(Arrays.asList(
@@ -471,7 +471,7 @@ public void test4p2ReadCommandsFailIfReauthenticationFails() {
471471
performFind(mongoClient);
472472
failCommand(391, 1, "find");
473473
assertCause(MongoCommandException.class,
474-
"Command failed with error 18",
474+
"Command execution failed on MongoDB server with error 18",
475475
() -> performFind(mongoClient));
476476
}
477477
assertEquals(2, wrappedCallback.invocations.get());
@@ -492,7 +492,7 @@ public void test4p3WriteCommandsFailIfReauthenticationFails() {
492492
performInsert(mongoClient);
493493
failCommand(391, 1, "insert");
494494
assertCause(MongoCommandException.class,
495-
"Command failed with error 18",
495+
"Command execution failed on MongoDB server with error 18",
496496
() -> performInsert(mongoClient));
497497
}
498498
assertEquals(2, wrappedCallback.invocations.get());
@@ -740,7 +740,7 @@ public void testh3p2NoSpecAuthIfNoCachedToken() {
740740
TestCommandListener commandListener = new TestCommandListener(listener);
741741
assertFindFails(createHumanSettings(createHumanCallback(), commandListener),
742742
MongoCommandException.class,
743-
"Command failed with error 18");
743+
"Command execution failed on MongoDB server with error 18");
744744
assertEquals(Arrays.asList(
745745
"isMaster started",
746746
"isMaster succeeded",
@@ -833,7 +833,7 @@ public void testh4p4Fails() {
833833
assertEquals(1, callback1.getInvocations());
834834
failCommand(391, 1, "find");
835835
assertCause(MongoCommandException.class,
836-
"Command failed with error 18",
836+
"Command execution failed on MongoDB server with error 18",
837837
() -> performFind(mongoClient));
838838
assertEquals(3, callback1.getInvocations());
839839
}

0 commit comments

Comments
 (0)