|
| 1 | +/* |
| 2 | + * Copyright 2008-present MongoDB, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.mongodb; |
| 17 | + |
| 18 | +import com.mongodb.bulk.WriteConcernError; |
| 19 | +import com.mongodb.client.model.bulk.ClientBulkWriteResult; |
| 20 | +import com.mongodb.client.model.bulk.ClientNamespacedWriteModel; |
| 21 | +import com.mongodb.lang.Nullable; |
| 22 | + |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Optional; |
| 26 | + |
| 27 | +import static com.mongodb.assertions.Assertions.isTrueArgument; |
| 28 | +import static com.mongodb.assertions.Assertions.notNull; |
| 29 | +import static com.mongodb.internal.operation.ClientBulkWriteOperation.Exceptions.serverAddressFromException; |
| 30 | +import static java.util.Collections.emptyList; |
| 31 | +import static java.util.Collections.emptyMap; |
| 32 | +import static java.util.Collections.unmodifiableList; |
| 33 | +import static java.util.Collections.unmodifiableMap; |
| 34 | +import static java.util.Optional.ofNullable; |
| 35 | + |
| 36 | +/** |
| 37 | + * The result of an unsuccessful or partially unsuccessful client-level bulk write operation. |
| 38 | + * 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 #getCause() top-level error}. |
| 40 | + * |
| 41 | + * @see ClientBulkWriteResult |
| 42 | + * @since 5.3 |
| 43 | + * @serial exclude |
| 44 | + */ |
| 45 | +public final class ClientBulkWriteException extends MongoServerException { |
| 46 | + private static final long serialVersionUID = 1; |
| 47 | + |
| 48 | + private final List<WriteConcernError> writeConcernErrors; |
| 49 | + private final Map<Integer, WriteError> writeErrors; |
| 50 | + @Nullable |
| 51 | + private final ClientBulkWriteResult partialResult; |
| 52 | + |
| 53 | + /** |
| 54 | + * Constructs a new instance. |
| 55 | + * |
| 56 | + * @param error The {@linkplain #getCause() top-level error}. |
| 57 | + * @param writeConcernErrors The {@linkplain #getWriteConcernErrors() write concern errors}. |
| 58 | + * @param writeErrors The {@linkplain #getWriteErrors() write errors}. |
| 59 | + * @param partialResult The {@linkplain #getPartialResult() partial result}. |
| 60 | + * @param serverAddress The {@linkplain MongoServerException#getServerAddress() server address}. |
| 61 | + * If {@code error} is a {@link MongoServerException} or a {@link MongoSocketException}, then {@code serverAddress} |
| 62 | + * must be equal to the {@link ServerAddress} they bear. |
| 63 | + */ |
| 64 | + public ClientBulkWriteException( |
| 65 | + @Nullable final MongoException error, |
| 66 | + @Nullable final List<WriteConcernError> writeConcernErrors, |
| 67 | + @Nullable final Map<Integer, WriteError> writeErrors, |
| 68 | + @Nullable final ClientBulkWriteResult partialResult, |
| 69 | + final ServerAddress serverAddress) { |
| 70 | + super( |
| 71 | + message( |
| 72 | + error, writeConcernErrors, writeErrors, partialResult, |
| 73 | + notNull("serverAddress", serverAddress)), |
| 74 | + validateServerAddress(error, serverAddress)); |
| 75 | + initCause(error); |
| 76 | + isTrueArgument("At least one of `writeConcernErrors`, `writeErrors`, `partialResult` must be non-null or non-empty", |
| 77 | + !(writeConcernErrors == null || writeConcernErrors.isEmpty()) |
| 78 | + || !(writeErrors == null || writeErrors.isEmpty()) |
| 79 | + || partialResult != null); |
| 80 | + this.writeConcernErrors = writeConcernErrors == null ? emptyList() : unmodifiableList(writeConcernErrors); |
| 81 | + this.writeErrors = writeErrors == null ? emptyMap() : unmodifiableMap(writeErrors); |
| 82 | + this.partialResult = partialResult; |
| 83 | + } |
| 84 | + |
| 85 | + private static String message( |
| 86 | + @Nullable final MongoException error, |
| 87 | + @Nullable final List<WriteConcernError> writeConcernErrors, |
| 88 | + @Nullable final Map<Integer, WriteError> writeErrors, |
| 89 | + @Nullable final ClientBulkWriteResult partialResult, |
| 90 | + final ServerAddress serverAddress) { |
| 91 | + return "Client-level bulk write operation error on server " + serverAddress + "." |
| 92 | + + (error == null ? "" : " Top-level error: " + error + ".") |
| 93 | + + (writeErrors == null || writeErrors.isEmpty() ? "" : " Write errors: " + writeErrors + ".") |
| 94 | + + (writeConcernErrors == null || writeConcernErrors.isEmpty() ? "" : " Write concern errors: " + writeConcernErrors + ".") |
| 95 | + + (partialResult == null ? "" : " Partial result: " + partialResult + "."); |
| 96 | + } |
| 97 | + |
| 98 | + private static ServerAddress validateServerAddress(@Nullable final MongoException error, final ServerAddress serverAddress) { |
| 99 | + serverAddressFromException(error).ifPresent(serverAddressFromError -> |
| 100 | + isTrueArgument("`serverAddress` must be equal to that of the `error`", serverAddressFromError.equals(serverAddress))); |
| 101 | + return error instanceof MongoServerException |
| 102 | + ? ((MongoServerException) error).getServerAddress() |
| 103 | + : serverAddress; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * The top-level error. That is an error that is neither a {@linkplain #getWriteConcernErrors() write concern error}, |
| 108 | + * nor is an {@linkplain #getWriteErrors() error of an individual write operation}. |
| 109 | + * |
| 110 | + * @return The top-level error. Non-{@code null} only if a top-level error occurred. |
| 111 | + */ |
| 112 | + @Override |
| 113 | + @Nullable |
| 114 | + public MongoException getCause() { |
| 115 | + return (MongoException) super.getCause(); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * The {@link WriteConcernError}s that occurred while executing the client-level bulk write operation. |
| 120 | + * <p> |
| 121 | + * There are no guarantees on mutability of the {@link List} returned.</p> |
| 122 | + * |
| 123 | + * @return The {@link WriteConcernError}s. |
| 124 | + */ |
| 125 | + public List<WriteConcernError> getWriteConcernErrors() { |
| 126 | + return writeConcernErrors; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * The indexed {@link WriteError}s. |
| 131 | + * The {@linkplain Map#keySet() keys} are the indexes of the corresponding {@link ClientNamespacedWriteModel}s |
| 132 | + * in the corresponding client-level bulk write operation. |
| 133 | + * <p> |
| 134 | + * There are no guarantees on mutability or iteration order of the {@link Map} returned.</p> |
| 135 | + * |
| 136 | + * @return The indexed {@link WriteError}s. |
| 137 | + * @see ClientBulkWriteResult.VerboseResults#getInsertResults() |
| 138 | + * @see ClientBulkWriteResult.VerboseResults#getUpdateResults() |
| 139 | + * @see ClientBulkWriteResult.VerboseResults#getDeleteResults() |
| 140 | + */ |
| 141 | + public Map<Integer, WriteError> getWriteErrors() { |
| 142 | + return writeErrors; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * The result of the part of a client-level bulk write operation that is known to be successful. |
| 147 | + * |
| 148 | + * @return The successful partial result. {@linkplain Optional#isPresent() Present} only if the client received a response indicating success |
| 149 | + * of at least one {@linkplain ClientNamespacedWriteModel individual write operation}. |
| 150 | + */ |
| 151 | + public Optional<ClientBulkWriteResult> getPartialResult() { |
| 152 | + return ofNullable(partialResult); |
| 153 | + } |
| 154 | +} |
0 commit comments