diff --git a/driver-core/src/main/com/mongodb/ClientBulkWriteException.java b/driver-core/src/main/com/mongodb/ClientBulkWriteException.java index d4e25858eb0..fcc23b45f71 100644 --- a/driver-core/src/main/com/mongodb/ClientBulkWriteException.java +++ b/driver-core/src/main/com/mongodb/ClientBulkWriteException.java @@ -36,7 +36,7 @@ /** * The result of an unsuccessful or partially unsuccessful client-level bulk write operation. * Note that the {@linkplain #getCode() code} and {@linkplain #getErrorLabels() labels} from this exception are not useful. - * An application should use those from the {@linkplain #getError() top-level error}. + * An application should use those from the {@linkplain #getCause() top-level error}. * * @see ClientBulkWriteResult * @since 5.3 @@ -45,8 +45,6 @@ public final class ClientBulkWriteException extends MongoServerException { private static final long serialVersionUID = 1; - @Nullable - private final MongoException error; private final List writeConcernErrors; private final Map writeErrors; @Nullable @@ -55,7 +53,7 @@ public final class ClientBulkWriteException extends MongoServerException { /** * Constructs a new instance. * - * @param error The {@linkplain #getError() top-level error}. + * @param error The {@linkplain #getCause() top-level error}. * @param writeConcernErrors The {@linkplain #getWriteConcernErrors() write concern errors}. * @param writeErrors The {@linkplain #getWriteErrors() write errors}. * @param partialResult The {@linkplain #getPartialResult() partial result}. @@ -74,11 +72,11 @@ public ClientBulkWriteException( error, writeConcernErrors, writeErrors, partialResult, notNull("serverAddress", serverAddress)), validateServerAddress(error, serverAddress)); + initCause(error); isTrueArgument("At least one of `writeConcernErrors`, `writeErrors`, `partialResult` must be non-null or non-empty", !(writeConcernErrors == null || writeConcernErrors.isEmpty()) || !(writeErrors == null || writeErrors.isEmpty()) || partialResult != null); - this.error = error; this.writeConcernErrors = writeConcernErrors == null ? emptyList() : unmodifiableList(writeConcernErrors); this.writeErrors = writeErrors == null ? emptyMap() : unmodifiableMap(writeErrors); this.partialResult = partialResult; @@ -109,10 +107,12 @@ private static ServerAddress validateServerAddress(@Nullable final MongoExceptio * The top-level error. That is an error that is neither a {@linkplain #getWriteConcernErrors() write concern error}, * nor is an {@linkplain #getWriteErrors() error of an individual write operation}. * - * @return The top-level error. {@linkplain Optional#isPresent() Present} only if a top-level error occurred. + * @return The top-level error. Non-{@code null} only if a top-level error occurred. */ - public Optional getError() { - return ofNullable(error); + @Override + @Nullable + public MongoException getCause() { + return (MongoException) super.getCause(); } /** diff --git a/driver-core/src/main/com/mongodb/internal/operation/OperationHelper.java b/driver-core/src/main/com/mongodb/internal/operation/OperationHelper.java index 91d3433dc6f..b7a4997e639 100644 --- a/driver-core/src/main/com/mongodb/internal/operation/OperationHelper.java +++ b/driver-core/src/main/com/mongodb/internal/operation/OperationHelper.java @@ -215,7 +215,8 @@ static void setNonTailableCursorMaxTimeSupplier(final TimeoutMode timeoutMode, f public static MongoException unwrap(final MongoException exception) { MongoException result = exception; if (exception instanceof ClientBulkWriteException) { - result = ((ClientBulkWriteException) exception).getError().orElse(exception); + MongoException topLevelError = ((ClientBulkWriteException) exception).getCause(); + result = topLevelError == null ? exception : topLevelError; } return result; } diff --git a/driver-sync/src/test/functional/com/mongodb/client/AbstractClientSideOperationsTimeoutProseTest.java b/driver-sync/src/test/functional/com/mongodb/client/AbstractClientSideOperationsTimeoutProseTest.java index 6319b9a4721..8eb47aa0a6c 100644 --- a/driver-sync/src/test/functional/com/mongodb/client/AbstractClientSideOperationsTimeoutProseTest.java +++ b/driver-sync/src/test/functional/com/mongodb/client/AbstractClientSideOperationsTimeoutProseTest.java @@ -99,7 +99,6 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assumptions.assumeFalse; import static org.junit.jupiter.api.Assumptions.assumeTrue; @@ -736,8 +735,8 @@ protected void test11MultiBatchBulkWrites() throws InterruptedException { new Document("a", join("", nCopies(maxBsonObjectSize - 500, "b")))); MongoException topLevelError = assertThrows(ClientBulkWriteException.class, () -> client.bulkWrite(nCopies(maxMessageSizeBytes / maxBsonObjectSize + 1, model))) - .getError() - .orElseThrow(() -> fail("Expected a top-level error")); + .getCause(); + assertNotNull(topLevelError); assertInstanceOf(MongoOperationTimeoutException.class, topLevelError); assertEquals(2, commandListener.getCommandStartedEvents("bulkWrite").size()); }