Skip to content

Commit 3672152

Browse files
authored
Replace ClientBulkWriteException.getError with getCause (#1568)
JAVA-4586
1 parent 44b0e5c commit 3672152

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
/**
3737
* The result of an unsuccessful or partially unsuccessful client-level bulk write operation.
3838
* Note that the {@linkplain #getCode() code} and {@linkplain #getErrorLabels() labels} from this exception are not useful.
39-
* An application should use those from the {@linkplain #getError() top-level error}.
39+
* An application should use those from the {@linkplain #getCause() top-level error}.
4040
*
4141
* @see ClientBulkWriteResult
4242
* @since 5.3
@@ -45,8 +45,6 @@
4545
public final class ClientBulkWriteException extends MongoServerException {
4646
private static final long serialVersionUID = 1;
4747

48-
@Nullable
49-
private final MongoException error;
5048
private final List<WriteConcernError> writeConcernErrors;
5149
private final Map<Integer, WriteError> writeErrors;
5250
@Nullable
@@ -55,7 +53,7 @@ public final class ClientBulkWriteException extends MongoServerException {
5553
/**
5654
* Constructs a new instance.
5755
*
58-
* @param error The {@linkplain #getError() top-level error}.
56+
* @param error The {@linkplain #getCause() top-level error}.
5957
* @param writeConcernErrors The {@linkplain #getWriteConcernErrors() write concern errors}.
6058
* @param writeErrors The {@linkplain #getWriteErrors() write errors}.
6159
* @param partialResult The {@linkplain #getPartialResult() partial result}.
@@ -74,11 +72,11 @@ public ClientBulkWriteException(
7472
error, writeConcernErrors, writeErrors, partialResult,
7573
notNull("serverAddress", serverAddress)),
7674
validateServerAddress(error, serverAddress));
75+
initCause(error);
7776
isTrueArgument("At least one of `writeConcernErrors`, `writeErrors`, `partialResult` must be non-null or non-empty",
7877
!(writeConcernErrors == null || writeConcernErrors.isEmpty())
7978
|| !(writeErrors == null || writeErrors.isEmpty())
8079
|| partialResult != null);
81-
this.error = error;
8280
this.writeConcernErrors = writeConcernErrors == null ? emptyList() : unmodifiableList(writeConcernErrors);
8381
this.writeErrors = writeErrors == null ? emptyMap() : unmodifiableMap(writeErrors);
8482
this.partialResult = partialResult;
@@ -109,10 +107,12 @@ private static ServerAddress validateServerAddress(@Nullable final MongoExceptio
109107
* The top-level error. That is an error that is neither a {@linkplain #getWriteConcernErrors() write concern error},
110108
* nor is an {@linkplain #getWriteErrors() error of an individual write operation}.
111109
*
112-
* @return The top-level error. {@linkplain Optional#isPresent() Present} only if a top-level error occurred.
110+
* @return The top-level error. Non-{@code null} only if a top-level error occurred.
113111
*/
114-
public Optional<MongoException> getError() {
115-
return ofNullable(error);
112+
@Override
113+
@Nullable
114+
public MongoException getCause() {
115+
return (MongoException) super.getCause();
116116
}
117117

118118
/**

driver-core/src/main/com/mongodb/internal/operation/OperationHelper.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ static void setNonTailableCursorMaxTimeSupplier(final TimeoutMode timeoutMode, f
215215
public static MongoException unwrap(final MongoException exception) {
216216
MongoException result = exception;
217217
if (exception instanceof ClientBulkWriteException) {
218-
result = ((ClientBulkWriteException) exception).getError().orElse(exception);
218+
MongoException topLevelError = ((ClientBulkWriteException) exception).getCause();
219+
result = topLevelError == null ? exception : topLevelError;
219220
}
220221
return result;
221222
}

driver-sync/src/test/functional/com/mongodb/client/AbstractClientSideOperationsTimeoutProseTest.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@
9999
import static org.junit.jupiter.api.Assertions.assertNull;
100100
import static org.junit.jupiter.api.Assertions.assertThrows;
101101
import static org.junit.jupiter.api.Assertions.assertTrue;
102-
import static org.junit.jupiter.api.Assertions.fail;
103102
import static org.junit.jupiter.api.Assumptions.assumeFalse;
104103
import static org.junit.jupiter.api.Assumptions.assumeTrue;
105104

@@ -736,8 +735,8 @@ protected void test11MultiBatchBulkWrites() throws InterruptedException {
736735
new Document("a", join("", nCopies(maxBsonObjectSize - 500, "b"))));
737736
MongoException topLevelError = assertThrows(ClientBulkWriteException.class, () ->
738737
client.bulkWrite(nCopies(maxMessageSizeBytes / maxBsonObjectSize + 1, model)))
739-
.getError()
740-
.<AssertionError>orElseThrow(() -> fail("Expected a top-level error"));
738+
.getCause();
739+
assertNotNull(topLevelError);
741740
assertInstanceOf(MongoOperationTimeoutException.class, topLevelError);
742741
assertEquals(2, commandListener.getCommandStartedEvents("bulkWrite").size());
743742
}

0 commit comments

Comments
 (0)