25
25
import java .util .Optional ;
26
26
27
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 ;
28
30
import static java .util .Collections .emptyList ;
29
31
import static java .util .Collections .emptyMap ;
30
32
import static java .util .Collections .unmodifiableList ;
@@ -56,16 +58,23 @@ public final class ClientBulkWriteException extends MongoServerException {
56
58
* @param writeErrors The {@linkplain #getWriteErrors() write errors}.
57
59
* @param partialResult The {@linkplain #getPartialResult() partial result}.
58
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.
59
63
*/
60
64
public ClientBulkWriteException (
61
65
@ Nullable final MongoException error ,
62
66
@ Nullable final List <WriteConcernError > writeConcernErrors ,
63
67
@ Nullable final Map <Long , WriteError > writeErrors ,
64
68
@ Nullable final ClientBulkWriteResult partialResult ,
65
69
final ServerAddress serverAddress ) {
66
- super (message (error , writeConcernErrors , writeErrors , partialResult , serverAddress ), serverAddress );
70
+ super (
71
+ message (
72
+ error , writeConcernErrors , writeErrors , partialResult ,
73
+ notNull ("serverAddress" , serverAddress )),
74
+ validateServerAddress (error , serverAddress ));
67
75
// BULK-TODO Should ClientBulkWriteException.getCode be the same as error.getCode,
68
76
// and getErrorLabels/hasErrorLabel contain the same labels as error.getErrorLabels?
77
+ // TRANSIENT_TRANSACTION_ERROR_LABEL, UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL, RETRYABLE_WRITE_ERROR_LABEL, NO_WRITES_PERFORMED_ERROR_LABEL
69
78
isTrueArgument ("At least one of `writeConcernErrors`, `writeErrors`, `partialResult` must be non-null or non-empty" ,
70
79
!(writeConcernErrors == null || writeConcernErrors .isEmpty ())
71
80
|| !(writeErrors == null || writeErrors .isEmpty ())
@@ -89,6 +98,14 @@ private static String message(
89
98
+ (partialResult == null ? "" : " Partial result: " + partialResult + "." );
90
99
}
91
100
101
+ private static ServerAddress validateServerAddress (@ Nullable final MongoException error , final ServerAddress serverAddress ) {
102
+ serverAddressFromException (error ).ifPresent (serverAddressFromError ->
103
+ isTrueArgument ("`serverAddress` must be equal to that of the `error`" , serverAddressFromError .equals (serverAddress )));
104
+ return error instanceof MongoServerException
105
+ ? ((MongoServerException ) error ).getServerAddress ()
106
+ : serverAddress ;
107
+ }
108
+
92
109
/**
93
110
* The top-level error. That is an error that is neither a {@linkplain #getWriteConcernErrors() write concern error},
94
111
* nor is an {@linkplain #getWriteErrors() error of an individual write operation}.
0 commit comments