@@ -489,12 +489,17 @@ private Object fetchField(GraphQLFieldDefinition fieldDef, ExecutionContext exec
489489
490490 CompletableFuture <CompletableFuture <Object >> handleCF = engineRunningState .handle (fetchedValue , (result , exception ) -> {
491491 // because we added an artificial CF, we need to unwrap the exception
492- fetchCtx .onCompleted (result , exception );
493- exception = engineRunningState .possibleCancellation (exception );
494-
495- if (exception != null ) {
496- return handleFetchingException (dataFetchingEnvironment .get (), parameters , exception );
492+ Throwable possibleWrappedException = engineRunningState .possibleCancellation (exception );
493+
494+ if (possibleWrappedException != null ) {
495+ CompletableFuture <DataFetcherResult <Object >> handledExceptionResult = handleFetchingException (dataFetchingEnvironment .get (), parameters , possibleWrappedException );
496+ return handledExceptionResult .thenApply ( handledResult -> {
497+ fetchCtx .onExceptionHandled (handledResult );
498+ fetchCtx .onCompleted (result , exception );
499+ return handledResult ;
500+ });
497501 } else {
502+ fetchCtx .onCompleted (result , exception );
498503 // we can simply return the fetched value CF and avoid a allocation
499504 return fetchedValue ;
500505 }
@@ -578,7 +583,7 @@ private void addExtensionsIfPresent(ExecutionContext executionContext, DataFetch
578583 }
579584 }
580585
581- protected <T > CompletableFuture <T > handleFetchingException (
586+ protected <T > CompletableFuture <DataFetcherResult < T > > handleFetchingException (
582587 DataFetchingEnvironment environment ,
583588 ExecutionStrategyParameters parameters ,
584589 Throwable e
@@ -599,10 +604,10 @@ protected <T> CompletableFuture<T> handleFetchingException(
599604 }
600605 }
601606
602- private <T > CompletableFuture <T > asyncHandleException (DataFetcherExceptionHandler handler , DataFetcherExceptionHandlerParameters handlerParameters ) {
607+ private <T > CompletableFuture <DataFetcherResult < T > > asyncHandleException (DataFetcherExceptionHandler handler , DataFetcherExceptionHandlerParameters handlerParameters ) {
603608 //noinspection unchecked
604609 return handler .handleException (handlerParameters ).thenApply (
605- handlerResult -> (T ) DataFetcherResult .newResult ().errors (handlerResult .getErrors ()).build ()
610+ handlerResult -> (DataFetcherResult < T > ) DataFetcherResult .newResult ().errors (handlerResult .getErrors ()).build ()
606611 );
607612 }
608613
0 commit comments