Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace ClientBulkWriteException.getError with getCause #1568

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions driver-core/src/main/com/mongodb/ClientBulkWriteException.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -45,8 +45,6 @@
public final class ClientBulkWriteException extends MongoServerException {
private static final long serialVersionUID = 1;

@Nullable
private final MongoException error;
private final List<WriteConcernError> writeConcernErrors;
private final Map<Integer, WriteError> writeErrors;
@Nullable
Expand All @@ -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}.
Expand All @@ -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;
Expand Down Expand Up @@ -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<MongoException> getError() {
return ofNullable(error);
@Override
@Nullable
public MongoException getCause() {
return (MongoException) super.getCause();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()
.<AssertionError>orElseThrow(() -> fail("Expected a top-level error"));
.getCause();
assertNotNull(topLevelError);
assertInstanceOf(MongoOperationTimeoutException.class, topLevelError);
assertEquals(2, commandListener.getCommandStartedEvents("bulkWrite").size());
}
Expand Down