|
| 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.ClientWriteModel; |
| 20 | +import com.mongodb.client.result.bulk.ClientBulkWriteResult; |
| 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 java.util.Collections.emptyList; |
| 29 | +import static java.util.Collections.emptyMap; |
| 30 | +import static java.util.Collections.unmodifiableList; |
| 31 | +import static java.util.Collections.unmodifiableMap; |
| 32 | +import static java.util.Optional.ofNullable; |
| 33 | + |
| 34 | +/** |
| 35 | + * The result of an unsuccessful or partially unsuccessful client-level bulk write operation. |
| 36 | + * |
| 37 | + * @see ClientBulkWriteResult |
| 38 | + * @since 5.3 |
| 39 | + * @serial exclude |
| 40 | + */ |
| 41 | +public final class ClientBulkWriteException extends MongoServerException { |
| 42 | + private static final long serialVersionUID = 1; |
| 43 | + |
| 44 | + @Nullable |
| 45 | + private final MongoException error; |
| 46 | + private final List<WriteConcernError> writeConcernErrors; |
| 47 | + private final Map<Long, WriteError> writeErrors; |
| 48 | + @Nullable |
| 49 | + private final ClientBulkWriteResult partialResult; |
| 50 | + |
| 51 | + private ClientBulkWriteException( |
| 52 | + final String message, |
| 53 | + @Nullable final MongoException error, |
| 54 | + @Nullable final List<WriteConcernError> writeConcernErrors, |
| 55 | + @Nullable final Map<Long, WriteError> writeErrors, |
| 56 | + @Nullable final ClientBulkWriteResult partialResult, |
| 57 | + final ServerAddress serverAddress) { |
| 58 | + super(message, serverAddress); |
| 59 | + // BULK-TODO Should ClientBulkWriteException.getCode be the same as error.getCode, |
| 60 | + // and getErrorLabels/hasErrorLabel contain the same labels as error.getErrorLabels? |
| 61 | + this.error = error; |
| 62 | + this.writeConcernErrors = writeConcernErrors == null ? emptyList() : unmodifiableList(writeConcernErrors); |
| 63 | + this.writeErrors = writeErrors == null ? emptyMap() : unmodifiableMap(writeErrors); |
| 64 | + this.partialResult = partialResult; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Creates a {@link ClientBulkWriteException}. |
| 69 | + * At least one of {@code error}, {@code writeConcernErrors}, {@code writeErrors}, {@code partialResult} |
| 70 | + * must be non-{@code null} or non-empty. |
| 71 | + * |
| 72 | + * @param error The {@linkplain #getError() top-level error}. |
| 73 | + * @param writeConcernErrors The {@linkplain #getWriteConcernErrors() write concern errors}. |
| 74 | + * @param writeErrors The {@linkplain #getWriteErrors() write errors}. |
| 75 | + * @param partialResult The {@linkplain #getPartialResult() partial result}. |
| 76 | + * @param serverAddress The {@linkplain MongoServerException#getServerAddress() server address}. |
| 77 | + * @return The requested {@link ClientBulkWriteException}. |
| 78 | + */ |
| 79 | + public static ClientBulkWriteException create( |
| 80 | + @Nullable final MongoException error, |
| 81 | + @Nullable final List<WriteConcernError> writeConcernErrors, |
| 82 | + @Nullable final Map<Long, WriteError> writeErrors, |
| 83 | + @Nullable final ClientBulkWriteResult partialResult, |
| 84 | + final ServerAddress serverAddress) { |
| 85 | + isTrueArgument("At least one of `writeConcernErrors`, `writeErrors`, `partialResult` must be non-null or non-empty", |
| 86 | + !(writeConcernErrors == null || writeConcernErrors.isEmpty()) |
| 87 | + || !(writeErrors == null || writeErrors.isEmpty()) |
| 88 | + || partialResult != null); |
| 89 | + String message = "Client-level bulk write operation error on server " + serverAddress + "." |
| 90 | + + (error == null ? "" : " Top-level error: " + error + ".") |
| 91 | + + (writeErrors == null || writeErrors.isEmpty() ? "" : " Write errors: " + writeErrors + ".") |
| 92 | + + (writeConcernErrors == null || writeConcernErrors.isEmpty() ? "" : " Write concern errors: " + writeConcernErrors + ".") |
| 93 | + + (partialResult == null ? "" : " Partial result: " + partialResult + "."); |
| 94 | + return new ClientBulkWriteException(message, error, writeConcernErrors, writeErrors, partialResult, serverAddress); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * The top-level error. That is an error that is neither a {@linkplain #getWriteConcernErrors() write concern error}, |
| 99 | + * nor is an {@linkplain #getWriteErrors() error of an individual write operation}. |
| 100 | + * |
| 101 | + * @return The top-level error. {@linkplain Optional#isPresent() Present} only if a top-level error occurred. |
| 102 | + */ |
| 103 | + public Optional<MongoException> getError() { |
| 104 | + return ofNullable(error); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * The {@link WriteConcernError}s that occurred while executing the client-level bulk write operation. |
| 109 | + * <p> |
| 110 | + * There are no guarantees on mutability of the {@link List} returned.</p> |
| 111 | + * |
| 112 | + * @return The {@link WriteConcernError}s. |
| 113 | + */ |
| 114 | + public List<WriteConcernError> getWriteConcernErrors() { |
| 115 | + return writeConcernErrors; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * The indexed {@link WriteError}s. |
| 120 | + * The {@linkplain Map#keySet() keys} are the indexes of the corresponding {@link ClientWriteModel}s |
| 121 | + * in the corresponding client-level bulk write operation. |
| 122 | + * <p> |
| 123 | + * There are no guarantees on mutability or iteration order of the {@link Map} returned.</p> |
| 124 | + * |
| 125 | + * @return The indexed {@link WriteError}s. |
| 126 | + * @see ClientBulkWriteResult#getInsertResults() |
| 127 | + * @see ClientBulkWriteResult#getUpdateResults() |
| 128 | + * @see ClientBulkWriteResult#getDeleteResults() |
| 129 | + */ |
| 130 | + public Map<Long, WriteError> getWriteErrors() { |
| 131 | + return writeErrors; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * The result of the successful part of a client-level bulk write operation. |
| 136 | + * |
| 137 | + * @return The successful partial result. {@linkplain Optional#isPresent() Present} only if at least one |
| 138 | + * {@linkplain ClientWriteModel individual write operation} succeed. |
| 139 | + */ |
| 140 | + public Optional<ClientBulkWriteResult> getPartialResult() { |
| 141 | + return ofNullable(partialResult); |
| 142 | + } |
| 143 | +} |
0 commit comments