18
18
*/
19
19
package org .neo4j .driver .internal .async ;
20
20
21
+ import java .util .Arrays ;
21
22
import java .util .EnumSet ;
23
+ import java .util .concurrent .CompletableFuture ;
22
24
import java .util .concurrent .CompletionException ;
23
25
import java .util .concurrent .CompletionStage ;
24
26
import java .util .function .BiFunction ;
@@ -210,7 +212,10 @@ public CompletionStage<ResultCursor> runAsync( Query query )
210
212
CompletionStage <AsyncResultCursor > cursorStage =
211
213
protocol .runInUnmanagedTransaction ( connection , query , this , fetchSize ).asyncResult ();
212
214
resultCursors .add ( cursorStage );
213
- return cursorStage .thenApply ( cursor -> cursor );
215
+ return cursorStage .thenCompose (
216
+ cursor -> cursor .runError ()
217
+ .map ( Futures ::<ResultCursor >failedFuture )
218
+ .orElseGet ( () -> CompletableFuture .completedFuture ( cursor ) ) );
214
219
}
215
220
216
221
public CompletionStage <RxResultCursor > runRx (Query query )
@@ -229,7 +234,29 @@ public boolean isOpen()
229
234
230
235
public void markTerminated ( Throwable cause )
231
236
{
232
- state = StateHolder .terminatedWith ( cause );
237
+ if ( state .value == State .TERMINATED )
238
+ {
239
+ if ( state .causeOfTermination != null )
240
+ {
241
+ addSuppressedWhenNotCaptured ( state .causeOfTermination , cause );
242
+ }
243
+ }
244
+ else
245
+ {
246
+ state = StateHolder .terminatedWith ( cause );
247
+ }
248
+ }
249
+
250
+ private void addSuppressedWhenNotCaptured ( Throwable currentCause , Throwable newCause )
251
+ {
252
+ if ( currentCause != newCause )
253
+ {
254
+ boolean noneMatch = Arrays .stream ( currentCause .getSuppressed () ).noneMatch ( suppressed -> suppressed == newCause );
255
+ if ( noneMatch )
256
+ {
257
+ currentCause .addSuppressed ( newCause );
258
+ }
259
+ }
233
260
}
234
261
235
262
public Connection connection ()
0 commit comments