11/*
2- * Copyright 2002-2023 the original author or authors.
2+ * Copyright 2002-2024 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
@@ -87,23 +87,13 @@ protected Object doInvoke(GraphQLContext graphQLContext, Object... argValues) {
8787 return invokeSuspendingFunction (getBean (), method , argValues );
8888 }
8989 Object result = method .invoke (getBean (), argValues );
90- return handleReturnValue (graphQLContext , result );
90+ return handleReturnValue (graphQLContext , result , method , argValues );
9191 }
9292 catch (IllegalArgumentException ex ) {
93- assertTargetBean (method , getBean (), argValues );
94- String text = (ex .getMessage () != null ) ? ex .getMessage () : "Illegal argument" ;
95- return Mono .error (new IllegalStateException (formatInvokeError (text , argValues ), ex ));
93+ return Mono .error (processIllegalArgumentException (argValues , ex , method ));
9694 }
9795 catch (InvocationTargetException ex ) {
98- // Unwrap for DataFetcherExceptionResolvers ...
99- Throwable targetException = ex .getTargetException ();
100- if (targetException instanceof Error || targetException instanceof Exception ) {
101- return Mono .error (targetException );
102- }
103- else {
104- return Mono .error (new IllegalStateException (
105- formatInvokeError ("Invocation failure" , argValues ), targetException ));
106- }
96+ return Mono .error (processInvocationTargetException (argValues , ex ));
10797 }
10898 catch (Throwable ex ) {
10999 return Mono .error (ex );
@@ -124,24 +114,51 @@ private static Object invokeSuspendingFunction(Object bean, Method method, Objec
124114 }
125115
126116 @ Nullable
127- @ SuppressWarnings ("deprecation" )
128- private Object handleReturnValue (GraphQLContext graphQLContext , @ Nullable Object result ) {
117+ @ SuppressWarnings ({"deprecation" , "DataFlowIssue" })
118+ private Object handleReturnValue (
119+ GraphQLContext graphQLContext , @ Nullable Object result , Method method , Object [] argValues ) {
120+
129121 if (this .hasCallableReturnValue && result != null ) {
130- return CompletableFuture .supplyAsync (
131- () -> {
132- try {
133- return ContextSnapshot .captureFrom (graphQLContext ).wrap ((Callable <?>) result ).call ();
134- }
135- catch (Exception ex ) {
136- throw new IllegalStateException (
137- "Failure in Callable returned from " + getBridgedMethod ().toGenericString (), ex );
138- }
139- },
140- this .executor );
122+ CompletableFuture <Object > future = new CompletableFuture <>();
123+ this .executor .execute (() -> {
124+ try {
125+ ContextSnapshot snapshot = ContextSnapshot .captureFrom (graphQLContext );
126+ Object value = snapshot .wrap ((Callable <?>) result ).call ();
127+ future .complete (value );
128+ }
129+ catch (IllegalArgumentException ex ) {
130+ future .completeExceptionally (processIllegalArgumentException (argValues , ex , method ));
131+ }
132+ catch (InvocationTargetException ex ) {
133+ future .completeExceptionally (processInvocationTargetException (argValues , ex ));
134+ }
135+ catch (Exception ex ) {
136+ future .completeExceptionally (ex );
137+ }
138+ });
139+ return future ;
141140 }
142141 return result ;
143142 }
144143
144+ private IllegalStateException processIllegalArgumentException (
145+ Object [] argValues , IllegalArgumentException ex , Method method ) {
146+
147+ assertTargetBean (method , getBean (), argValues );
148+ String text = (ex .getMessage () != null ) ? ex .getMessage () : "Illegal argument" ;
149+ return new IllegalStateException (formatInvokeError (text , argValues ), ex );
150+ }
151+
152+ private Throwable processInvocationTargetException (Object [] argValues , InvocationTargetException ex ) {
153+ // Unwrap for DataFetcherExceptionResolvers ...
154+ Throwable targetException = ex .getTargetException ();
155+ if (targetException instanceof Error || targetException instanceof Exception ) {
156+ return targetException ;
157+ }
158+ String message = formatInvokeError ("Invocation failure" , argValues );
159+ return new IllegalStateException (message , targetException );
160+ }
161+
145162 /**
146163 * Use this method to resolve the arguments asynchronously. This is only
147164 * useful when at least one of the values is a {@link Mono}
0 commit comments