Skip to content

Commit 58e4820

Browse files
authored
Remove deprecated methods in WriteConcernError class (#1257)
JAVA-5153
1 parent 1da4c2f commit 58e4820

File tree

10 files changed

+63
-105
lines changed

10 files changed

+63
-105
lines changed

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ public class MongoBulkWriteException extends MongoServerException {
5050
* @param errorLabels any server errorLabels
5151
* @since 4.1
5252
*/
53-
@SuppressWarnings("deprecation")
5453
public MongoBulkWriteException(final BulkWriteResult writeResult, final List<BulkWriteError> writeErrors,
5554
@Nullable final WriteConcernError writeConcernError, final ServerAddress serverAddress,
5655
final Set<String> errorLabels) {
@@ -63,10 +62,6 @@ public MongoBulkWriteException(final BulkWriteResult writeResult, final List<Bul
6362
this.serverAddress = serverAddress;
6463

6564
addLabels(errorLabels);
66-
67-
if (writeConcernError != null) {
68-
addLabels(writeConcernError.getErrorLabels());
69-
}
7065
}
7166

7267
/**

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

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import com.mongodb.bulk.WriteConcernError;
2020
import com.mongodb.lang.Nullable;
2121

22+
import java.util.Collection;
23+
import java.util.Collections;
24+
2225
import static com.mongodb.assertions.Assertions.notNull;
2326

2427
/**
@@ -40,9 +43,11 @@ public class MongoWriteConcernException extends MongoServerException {
4043
*
4144
* @param writeConcernError the non-null write concern error
4245
* @param serverAddress the non-null server address
46+
* @deprecated Prefer {@link MongoWriteConcernException(WriteConcernError, WriteConcernResult, ServerAddress, Collection)}
4347
*/
48+
@Deprecated
4449
public MongoWriteConcernException(final WriteConcernError writeConcernError, final ServerAddress serverAddress) {
45-
this(writeConcernError, null, serverAddress);
50+
this(writeConcernError, null, serverAddress, Collections.emptySet());
4651
}
4752

4853
/**
@@ -52,24 +57,31 @@ public MongoWriteConcernException(final WriteConcernError writeConcernError, fin
5257
* @param writeConcernResult the write result
5358
* @param serverAddress the non-null server address
5459
* @since 3.2
60+
* @deprecated Prefer {@link MongoWriteConcernException(WriteConcernError, WriteConcernResult, ServerAddress, Collection)}
5561
*/
56-
@SuppressWarnings("deprecation")
62+
@Deprecated
5763
public MongoWriteConcernException(final WriteConcernError writeConcernError, @Nullable final WriteConcernResult writeConcernResult,
5864
final ServerAddress serverAddress) {
65+
this(writeConcernError, writeConcernResult, serverAddress, Collections.emptySet());
66+
}
67+
68+
/**
69+
* Construct an instance.
70+
*
71+
* @param writeConcernError the non-null write concern error
72+
* @param writeConcernResult the write result
73+
* @param serverAddress the non-null server address
74+
* @param errorLabels the server errorLabels
75+
* @since 5.0
76+
*/
77+
public MongoWriteConcernException(final WriteConcernError writeConcernError, @Nullable final WriteConcernResult writeConcernResult,
78+
final ServerAddress serverAddress, final Collection<String> errorLabels) {
5979
super(writeConcernError.getCode(), writeConcernError.getMessage(), serverAddress);
6080
this.writeConcernResult = writeConcernResult;
6181
this.writeConcernError = notNull("writeConcernError", writeConcernError);
62-
for (final String errorLabel : writeConcernError.getErrorLabels()) {
63-
super.addLabel(errorLabel);
64-
}
82+
addLabels(errorLabels);
6583
}
6684

67-
@Override
68-
@SuppressWarnings("deprecation")
69-
public void addLabel(final String errorLabel) {
70-
writeConcernError.addLabel(errorLabel);
71-
super.addLabel(errorLabel);
72-
}
7385

7486
/**
7587
* Gets the write concern error.

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package com.mongodb;
1818

19+
import java.util.Collection;
20+
import java.util.Collections;
21+
1922
/**
2023
* An exception indicating the failure of a write operation.
2124
*
@@ -32,10 +35,24 @@ public class MongoWriteException extends MongoServerException {
3235
* Construct an instance
3336
* @param error the error
3437
* @param serverAddress the server address
38+
* @deprecated Prefer {@link MongoWriteException(WriteError, ServerAddress, Collection)}
3539
*/
40+
@Deprecated
3641
public MongoWriteException(final WriteError error, final ServerAddress serverAddress) {
42+
this(error, serverAddress, Collections.emptySet());
43+
}
44+
45+
/**
46+
* Construct an instance
47+
* @param error the error
48+
* @param serverAddress the server address
49+
* @param errorLabels the server errorLabels
50+
* @since 5.0
51+
*/
52+
public MongoWriteException(final WriteError error, final ServerAddress serverAddress, final Collection<String> errorLabels) {
3753
super(error.getCode(), "Write operation error on server " + serverAddress + ". Write error: " + error + ".", serverAddress);
3854
this.error = error;
55+
addLabels(errorLabels);
3956
}
4057

4158
/**

driver-core/src/main/com/mongodb/bulk/WriteConcernError.java

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,8 @@
1616

1717
package com.mongodb.bulk;
1818

19-
import com.mongodb.lang.NonNull;
2019
import org.bson.BsonDocument;
2120

22-
import java.util.Collections;
23-
import java.util.Set;
24-
2521
import static com.mongodb.assertions.Assertions.notNull;
2622

2723
/**
@@ -35,8 +31,6 @@ public class WriteConcernError {
3531
private final String codeName;
3632
private final String message;
3733
private final BsonDocument details;
38-
private final Set<String> errorLabels;
39-
4034

4135
/**
4236
* Constructs a new instance.
@@ -47,28 +41,10 @@ public class WriteConcernError {
4741
* @param details any details
4842
*/
4943
public WriteConcernError(final int code, final String codeName, final String message, final BsonDocument details) {
50-
this(code, codeName, message, details, Collections.emptySet());
51-
}
52-
53-
/**
54-
* Constructs a new instance.
55-
*
56-
* @param code the error code
57-
* @param codeName the error code name
58-
* @param message the error message
59-
* @param details any details
60-
* @param errorLabels any error labels
61-
* @since 4.1
62-
* @deprecated Prefer using error labels included in the top level response document
63-
*/
64-
@Deprecated
65-
public WriteConcernError(final int code, final String codeName, final String message, final BsonDocument details,
66-
final Set<String> errorLabels) {
6744
this.code = code;
6845
this.codeName = notNull("codeName", codeName);
6946
this.message = notNull("message", message);
7047
this.details = notNull("details", details);
71-
this.errorLabels = notNull("errorLabels", errorLabels);
7248
}
7349

7450
/**
@@ -109,33 +85,6 @@ public BsonDocument getDetails() {
10985
return details;
11086
}
11187

112-
/**
113-
* Adds the given error label to the exception.
114-
*
115-
* @param errorLabel the non-null error label to add to the exception
116-
*
117-
* @since 4.1
118-
* @deprecated Prefer using error labels included in the top level response document
119-
*/
120-
@Deprecated
121-
public void addLabel(final String errorLabel) {
122-
notNull("errorLabel", errorLabel);
123-
errorLabels.add(errorLabel);
124-
}
125-
126-
/**
127-
* Gets the set of error labels associated with this exception.
128-
*
129-
* @return the error labels, which may not be null but may be empty
130-
* @since 4.1
131-
* @deprecated Prefer using error labels included in the top level response document
132-
*/
133-
@NonNull
134-
@Deprecated
135-
public Set<String> getErrorLabels() {
136-
return Collections.unmodifiableSet(errorLabels);
137-
}
138-
13988
@Override
14089
public boolean equals(final Object o) {
14190
if (this == o) {
@@ -159,9 +108,6 @@ public boolean equals(final Object o) {
159108
if (!message.equals(that.message)) {
160109
return false;
161110
}
162-
if (!errorLabels.equals(that.errorLabels)) {
163-
return false;
164-
}
165111

166112
return true;
167113
}
@@ -172,7 +118,6 @@ public int hashCode() {
172118
result = 31 * result + codeName.hashCode();
173119
result = 31 * result + message.hashCode();
174120
result = 31 * result + details.hashCode();
175-
result = 31 * result + errorLabels.hashCode();
176121
return result;
177122
}
178123

@@ -183,7 +128,6 @@ public String toString() {
183128
+ ", codeName='" + codeName + '\''
184129
+ ", message='" + message + '\''
185130
+ ", details=" + details
186-
+ ", errorLabels=" + errorLabels
187131
+ '}';
188132
}
189133
}

driver-core/src/main/com/mongodb/internal/connection/BulkWriteBatchCombiner.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,8 @@ private void mergeWriteConcernError(@Nullable final WriteConcernError writeConce
179179
if (writeConcernError != null) {
180180
if (writeConcernErrors.isEmpty()) {
181181
writeConcernErrors.add(writeConcernError);
182-
errorLabels.addAll(writeConcernError.getErrorLabels());
183182
} else if (!writeConcernError.equals(writeConcernErrors.get(writeConcernErrors.size() - 1))) {
184183
writeConcernErrors.add(writeConcernError);
185-
errorLabels.addAll(writeConcernError.getErrorLabels());
186184
}
187185
}
188186
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import org.bson.BsonDocument;
2626
import org.bson.BsonInt32;
2727

28+
import java.util.stream.Collectors;
29+
2830
import static com.mongodb.internal.operation.AsyncOperationHelper.CommandWriteTransformerAsync;
2931
import static com.mongodb.internal.operation.SyncOperationHelper.CommandWriteTransformer;
3032
import static com.mongodb.internal.operation.WriteConcernHelper.createWriteConcernError;
@@ -43,12 +45,10 @@ static <T> CommandWriteTransformerAsync<BsonDocument, T> asyncTransformer() {
4345
@Nullable
4446
private static <T> T transformDocument(final BsonDocument result, final ServerAddress serverAddress) {
4547
if (hasWriteConcernError(result)) {
46-
MongoWriteConcernException writeConcernException = new MongoWriteConcernException(
48+
throw new MongoWriteConcernException(
4749
createWriteConcernError(result.getDocument("writeConcernError")),
48-
createWriteConcernResult(result.getDocument("lastErrorObject", new BsonDocument())), serverAddress);
49-
result.getArray("errorLabels", new BsonArray()).stream().map(i -> i.asString().getValue())
50-
.forEach(writeConcernException::addLabel);
51-
throw writeConcernException;
50+
createWriteConcernResult(result.getDocument("lastErrorObject", new BsonDocument())), serverAddress,
51+
result.getArray("errorLabels", new BsonArray()).stream().map(i -> i.asString().getValue()).collect(Collectors.toSet()));
5252
}
5353

5454
if (!result.isDocument("value")) {

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,18 @@ public static boolean hasWriteConcernError(final BsonDocument result) {
5858
}
5959

6060
public static MongoWriteConcernException createWriteConcernException(final BsonDocument result, final ServerAddress serverAddress) {
61-
MongoWriteConcernException writeConcernException = new MongoWriteConcernException(
61+
return new MongoWriteConcernException(
6262
createWriteConcernError(result.getDocument("writeConcernError")),
63-
WriteConcernResult.acknowledged(0, false, null), serverAddress);
64-
result.getArray("errorLabels", new BsonArray()).stream().map(i -> i.asString().getValue())
65-
.forEach(writeConcernException::addLabel);
66-
return writeConcernException;
63+
WriteConcernResult.acknowledged(0, false, null), serverAddress,
64+
result.getArray("errorLabels", new BsonArray()).stream().map(i -> i.asString().getValue())
65+
.collect(Collectors.toSet()));
6766
}
6867

69-
@SuppressWarnings("deprecation")
7068
public static WriteConcernError createWriteConcernError(final BsonDocument writeConcernErrorDocument) {
7169
return new WriteConcernError(writeConcernErrorDocument.getNumber("code").intValue(),
7270
writeConcernErrorDocument.getString("codeName", new BsonString("")).getValue(),
7371
writeConcernErrorDocument.getString("errmsg").getValue(),
74-
writeConcernErrorDocument.getDocument("errInfo", new BsonDocument()),
75-
writeConcernErrorDocument.getArray("errorLabels", new BsonArray()).stream().map(i -> i.asString().getValue())
76-
.collect(Collectors.toSet()));
72+
writeConcernErrorDocument.getDocument("errInfo", new BsonDocument()));
7773
}
7874

7975
private WriteConcernHelper() {

driver-core/src/test/unit/com/mongodb/MongoWriteExceptionTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,16 @@
2121
import org.bson.BsonInt32;
2222
import org.junit.Test;
2323

24+
import java.util.Collections;
25+
2426
import static org.junit.jupiter.api.Assertions.assertEquals;
2527

2628
public class MongoWriteExceptionTest {
2729

2830
@Test
2931
public void testExceptionProperties() {
3032
WriteError writeError = new WriteError(11000, "Duplicate key", new BsonDocument("x", new BsonInt32(1)));
31-
MongoWriteException e = new MongoWriteException(writeError, new ServerAddress("host1"));
33+
MongoWriteException e = new MongoWriteException(writeError, new ServerAddress("host1"), Collections.emptySet());
3234

3335
assertEquals("Write operation error on server host1:27017. Write error: WriteError{code=11000, message='Duplicate key', "
3436
+ "details={\"x\": 1}}.",

driver-reactive-streams/src/main/com/mongodb/reactivestreams/client/internal/MongoOperationPublisher.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -468,17 +468,16 @@ private Mono<BulkWriteResult> createSingleWriteRequestMono(
468468
e.getWriteResult().getUpserts().isEmpty()
469469
? null : e.getWriteResult().getUpserts().get(0).getId());
470470
}
471-
exception = new MongoWriteConcernException(writeConcernError, writeConcernResult, e.getServerAddress());
471+
exception = new MongoWriteConcernException(writeConcernError, writeConcernResult, e.getServerAddress(),
472+
e.getErrorLabels());
472473
} else if (!e.getWriteErrors().isEmpty()) {
473-
exception = new MongoWriteException(new WriteError(e.getWriteErrors().get(0)), e.getServerAddress());
474+
exception = new MongoWriteException(new WriteError(e.getWriteErrors().get(0)), e.getServerAddress(),
475+
e.getErrorLabels());
474476
} else {
475477
exception = new MongoWriteException(new WriteError(-1, "Unknown write error", new BsonDocument()),
476-
e.getServerAddress());
478+
e.getServerAddress(), e.getErrorLabels());
477479
}
478480

479-
for (final String errorLabel : e.getErrorLabels()) {
480-
exception.addLabel(errorLabel);
481-
}
482481
return exception;
483482
});
484483
}

driver-sync/src/main/com/mongodb/client/internal/MongoCollectionImpl.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import com.mongodb.AutoEncryptionSettings;
2020
import com.mongodb.MongoBulkWriteException;
21-
import com.mongodb.MongoException;
2221
import com.mongodb.MongoInternalException;
2322
import com.mongodb.MongoNamespace;
2423
import com.mongodb.MongoWriteConcernException;
@@ -49,11 +48,11 @@
4948
import com.mongodb.client.model.FindOneAndUpdateOptions;
5049
import com.mongodb.client.model.IndexModel;
5150
import com.mongodb.client.model.IndexOptions;
52-
import com.mongodb.client.model.SearchIndexModel;
5351
import com.mongodb.client.model.InsertManyOptions;
5452
import com.mongodb.client.model.InsertOneOptions;
5553
import com.mongodb.client.model.RenameCollectionOptions;
5654
import com.mongodb.client.model.ReplaceOptions;
55+
import com.mongodb.client.model.SearchIndexModel;
5756
import com.mongodb.client.model.UpdateOptions;
5857
import com.mongodb.client.model.WriteModel;
5958
import com.mongodb.client.result.DeleteResult;
@@ -1084,18 +1083,14 @@ private BulkWriteResult executeSingleWriteRequest(@Nullable final ClientSession
10841083
try {
10851084
return executor.execute(writeOperation, readConcern, clientSession);
10861085
} catch (MongoBulkWriteException e) {
1087-
MongoException exception;
10881086
if (e.getWriteErrors().isEmpty()) {
1089-
exception = new MongoWriteConcernException(e.getWriteConcernError(),
1087+
throw new MongoWriteConcernException(e.getWriteConcernError(),
10901088
translateBulkWriteResult(type, e.getWriteResult()),
1091-
e.getServerAddress());
1089+
e.getServerAddress(), e.getErrorLabels());
10921090
} else {
1093-
exception = new MongoWriteException(new WriteError(e.getWriteErrors().get(0)), e.getServerAddress());
1091+
throw new MongoWriteException(new WriteError(e.getWriteErrors().get(0)), e.getServerAddress(), e.getErrorLabels());
10941092
}
1095-
for (final String errorLabel : e.getErrorLabels()) {
1096-
exception.addLabel(errorLabel);
1097-
}
1098-
throw exception;
1093+
10991094
}
11001095
}
11011096

0 commit comments

Comments
 (0)