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 ;
@@ -58,14 +60,20 @@ public final class ClientBulkWriteException extends MongoServerException {
58
60
* @param writeErrors The {@linkplain #getWriteErrors() write errors}.
59
61
* @param partialResult The {@linkplain #getPartialResult() partial result}.
60
62
* @param serverAddress The {@linkplain MongoServerException#getServerAddress() server address}.
63
+ * If {@code error} is a {@link MongoServerException} or a {@link MongoSocketException}, then {@code serverAddress}
64
+ * must be equal to the {@link ServerAddress} they bear.
61
65
*/
62
66
public ClientBulkWriteException (
63
67
@ Nullable final MongoException error ,
64
68
@ Nullable final List <WriteConcernError > writeConcernErrors ,
65
69
@ Nullable final Map <Integer , WriteError > writeErrors ,
66
70
@ Nullable final ClientBulkWriteResult partialResult ,
67
71
final ServerAddress serverAddress ) {
68
- super (message (error , writeConcernErrors , writeErrors , partialResult , serverAddress ), serverAddress );
72
+ super (
73
+ message (
74
+ error , writeConcernErrors , writeErrors , partialResult ,
75
+ notNull ("serverAddress" , serverAddress )),
76
+ validateServerAddress (error , serverAddress ));
69
77
isTrueArgument ("At least one of `writeConcernErrors`, `writeErrors`, `partialResult` must be non-null or non-empty" ,
70
78
!(writeConcernErrors == null || writeConcernErrors .isEmpty ())
71
79
|| !(writeErrors == null || writeErrors .isEmpty ())
@@ -89,6 +97,14 @@ private static String message(
89
97
+ (partialResult == null ? "" : " Partial result: " + partialResult + "." );
90
98
}
91
99
100
+ private static ServerAddress validateServerAddress (@ Nullable final MongoException error , final ServerAddress serverAddress ) {
101
+ serverAddressFromException (error ).ifPresent (serverAddressFromError ->
102
+ isTrueArgument ("`serverAddress` must be equal to that of the `error`" , serverAddressFromError .equals (serverAddress )));
103
+ return error instanceof MongoServerException
104
+ ? ((MongoServerException ) error ).getServerAddress ()
105
+ : serverAddress ;
106
+ }
107
+
92
108
/**
93
109
* The top-level error. That is an error that is neither a {@linkplain #getWriteConcernErrors() write concern error},
94
110
* nor is an {@linkplain #getWriteErrors() error of an individual write operation}.
0 commit comments