Skip to content

Commit beeb1b6

Browse files
committed
Normalize single write handling of Write Concern errors
JAVA-6111
1 parent 81fc5e6 commit beeb1b6

4 files changed

Lines changed: 61 additions & 22 deletions

File tree

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,21 @@ public MongoServerException(final int code, @Nullable final String errorCodeName
7171
this.serverAddress = serverAddress;
7272
}
7373

74+
/**
75+
* Construct a new instance.
76+
*
77+
* @param code the error code from the server
78+
* @param message the message from the server
79+
* @param t the throwable cause
80+
* @param serverAddress the address of the server
81+
* @since 5.7
82+
*/
83+
public MongoServerException(final int code, final String message, final Throwable t, final ServerAddress serverAddress) {
84+
super(code, message, t);
85+
this.errorCodeName = null;
86+
this.serverAddress = serverAddress;
87+
}
88+
7489
/**
7590
* Gets the address of the server.
7691
*

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,24 @@ public MongoWriteConcernException(final WriteConcernError writeConcernError, @Nu
8282
addLabels(errorLabels);
8383
}
8484

85+
/**
86+
* Construct an instance.
87+
*
88+
* @param writeConcernError the non-null write concern error
89+
* @param writeConcernResult the write result
90+
* @param serverAddress the non-null server address
91+
* @param errorLabels the server errorLabels
92+
* @param t the throwable cause
93+
* @since 5.7
94+
*/
95+
public MongoWriteConcernException(final WriteConcernError writeConcernError, @Nullable final WriteConcernResult writeConcernResult,
96+
final ServerAddress serverAddress, final Collection<String> errorLabels, final Throwable t) {
97+
super(writeConcernError.getCode(), writeConcernError.getMessage(), t, serverAddress);
98+
this.writeConcernResult = writeConcernResult;
99+
this.writeConcernError = notNull("writeConcernError", writeConcernError);
100+
addLabels(errorLabels);
101+
}
102+
85103

86104
/**
87105
* Gets the write concern error.

driver-reactive-streams/src/main/com/mongodb/reactivestreams/client/internal/MongoOperationPublisher.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.mongodb.MongoBulkWriteException;
2020
import com.mongodb.MongoClientException;
2121
import com.mongodb.MongoException;
22+
import com.mongodb.MongoInternalException;
2223
import com.mongodb.MongoNamespace;
2324
import com.mongodb.MongoWriteConcernException;
2425
import com.mongodb.MongoWriteException;
@@ -534,20 +535,8 @@ private Mono<BulkWriteResult> createSingleWriteRequestMono(
534535
MongoException exception;
535536
WriteConcernError writeConcernError = e.getWriteConcernError();
536537
if (e.getWriteErrors().isEmpty() && writeConcernError != null) {
537-
WriteConcernResult writeConcernResult;
538-
if (type == WriteRequest.Type.INSERT) {
539-
writeConcernResult = WriteConcernResult.acknowledged(e.getWriteResult().getInsertedCount(), false, null);
540-
} else if (type == WriteRequest.Type.DELETE) {
541-
writeConcernResult = WriteConcernResult.acknowledged(e.getWriteResult().getDeletedCount(), false, null);
542-
} else {
543-
writeConcernResult = WriteConcernResult
544-
.acknowledged(e.getWriteResult().getMatchedCount() + e.getWriteResult().getUpserts().size(),
545-
e.getWriteResult().getMatchedCount() > 0,
546-
e.getWriteResult().getUpserts().isEmpty()
547-
? null : e.getWriteResult().getUpserts().get(0).getId());
548-
}
549-
exception = new MongoWriteConcernException(writeConcernError, writeConcernResult, e.getServerAddress(),
550-
e.getErrorLabels());
538+
exception = new MongoWriteConcernException(writeConcernError, translateBulkWriteResult(type, e.getWriteResult()),
539+
e.getServerAddress(), e.getErrorLabels(), e);
551540
} else if (!e.getWriteErrors().isEmpty()) {
552541
exception = new MongoWriteException(new WriteError(e.getWriteErrors().get(0)), e.getServerAddress(),
553542
e.getErrorLabels());
@@ -560,6 +549,23 @@ private Mono<BulkWriteResult> createSingleWriteRequestMono(
560549
});
561550
}
562551

552+
private WriteConcernResult translateBulkWriteResult(final WriteRequest.Type type, final BulkWriteResult writeResult) {
553+
switch (type) {
554+
case INSERT:
555+
return WriteConcernResult.acknowledged(writeResult.getInsertedCount(), false, null);
556+
case DELETE:
557+
return WriteConcernResult.acknowledged(writeResult.getDeletedCount(), false, null);
558+
case UPDATE:
559+
case REPLACE:
560+
return WriteConcernResult.acknowledged(writeResult.getMatchedCount() + writeResult.getUpserts().size(),
561+
writeResult.getMatchedCount() > 0,
562+
writeResult.getUpserts().isEmpty()
563+
? null : writeResult.getUpserts().get(0).getId());
564+
default:
565+
throw new MongoInternalException("Unhandled write request type: " + type);
566+
}
567+
}
568+
563569
private OperationExecutor getExecutor(final TimeoutSettings timeoutSettings) {
564570
return executor.withTimeoutSettings(timeoutSettings);
565571
}

driver-sync/src/main/com/mongodb/client/internal/MongoCollectionImpl.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,17 +1113,17 @@ private BulkWriteResult executeSingleWriteRequest(@Nullable final ClientSession
11131113
final WriteOperation<BulkWriteResult> writeOperation,
11141114
final WriteRequest.Type type) {
11151115
try {
1116-
return getExecutor(timeoutSettings)
1117-
.execute(writeOperation, readConcern, clientSession);
1116+
return getExecutor(timeoutSettings).execute(writeOperation, readConcern, clientSession);
11181117
} catch (MongoBulkWriteException e) {
1119-
if (e.getWriteErrors().isEmpty()) {
1120-
throw new MongoWriteConcernException(e.getWriteConcernError(),
1121-
translateBulkWriteResult(type, e.getWriteResult()),
1122-
e.getServerAddress(), e.getErrorLabels());
1123-
} else {
1118+
if (e.getWriteConcernError() != null) {
1119+
throw new MongoWriteConcernException(e.getWriteConcernError(), translateBulkWriteResult(type, e.getWriteResult()),
1120+
e.getServerAddress(), e.getErrorLabels(), e);
1121+
} else if (!e.getWriteErrors().isEmpty()) {
11241122
throw new MongoWriteException(new WriteError(e.getWriteErrors().get(0)), e.getServerAddress(), e.getErrorLabels());
1123+
} else {
1124+
throw new MongoWriteException(new WriteError(-1, "Unknown write error", new BsonDocument()),
1125+
e.getServerAddress(), e.getErrorLabels());
11251126
}
1126-
11271127
}
11281128
}
11291129

0 commit comments

Comments
 (0)