26
26
import java .util .concurrent .Executor ;
27
27
28
28
import graphql .GraphQLContext ;
29
+ import io .micrometer .context .ContextSnapshot ;
29
30
import reactor .core .publisher .Mono ;
30
31
31
32
import org .springframework .core .CoroutinesUtils ;
@@ -110,32 +111,22 @@ protected Object doInvoke(GraphQLContext graphQLContext, Object... argValues) {
110
111
Object result ;
111
112
if (this .invokeAsync ) {
112
113
Callable <Object > callable = () -> method .invoke (getBean (), argValues );
113
- result = adaptCallable (graphQLContext , callable );
114
+ result = adaptCallable (graphQLContext , callable , method , argValues );
114
115
}
115
116
else {
116
117
result = method .invoke (getBean (), argValues );
117
118
if (this .hasCallableReturnValue && result != null ) {
118
- result = adaptCallable (graphQLContext , (Callable <?>) result );
119
+ result = adaptCallable (graphQLContext , (Callable <?>) result , method , argValues );
119
120
}
120
121
}
121
122
122
123
return result ;
123
124
}
124
125
catch (IllegalArgumentException ex ) {
125
- assertTargetBean (method , getBean (), argValues );
126
- String text = (ex .getMessage () != null ) ? ex .getMessage () : "Illegal argument" ;
127
- return Mono .error (new IllegalStateException (formatInvokeError (text , argValues ), ex ));
126
+ return Mono .error (processIllegalArgumentException (argValues , ex , method ));
128
127
}
129
128
catch (InvocationTargetException ex ) {
130
- // Unwrap for DataFetcherExceptionResolvers ...
131
- Throwable targetException = ex .getTargetException ();
132
- if (targetException instanceof Error || targetException instanceof Exception ) {
133
- return Mono .error (targetException );
134
- }
135
- else {
136
- return Mono .error (new IllegalStateException (
137
- formatInvokeError ("Invocation failure" , argValues ), targetException ));
138
- }
129
+ return Mono .error (processInvocationTargetException (argValues , ex ));
139
130
}
140
131
catch (Throwable ex ) {
141
132
return Mono .error (ex );
@@ -155,16 +146,46 @@ private static Object invokeSuspendingFunction(Object bean, Method method, Objec
155
146
return result ;
156
147
}
157
148
158
- private CompletableFuture <?> adaptCallable (GraphQLContext graphQLContext , Callable <?> result ) {
159
- return CompletableFuture .supplyAsync (() -> {
149
+ @ SuppressWarnings ("DataFlowIssue" )
150
+ private CompletableFuture <?> adaptCallable (
151
+ GraphQLContext graphQLContext , Callable <?> result , Method method , Object [] argValues ) {
152
+
153
+ CompletableFuture <Object > future = new CompletableFuture <>();
154
+ this .executor .execute (() -> {
160
155
try {
161
- return ContextSnapshotFactoryHelper .captureFrom (graphQLContext ).wrap (result ).call ();
156
+ ContextSnapshot snapshot = ContextSnapshotFactoryHelper .captureFrom (graphQLContext );
157
+ Object value = snapshot .wrap ((Callable <?>) result ).call ();
158
+ future .complete (value );
159
+ }
160
+ catch (IllegalArgumentException ex ) {
161
+ future .completeExceptionally (processIllegalArgumentException (argValues , ex , method ));
162
+ }
163
+ catch (InvocationTargetException ex ) {
164
+ future .completeExceptionally (processInvocationTargetException (argValues , ex ));
162
165
}
163
166
catch (Exception ex ) {
164
- String msg = "Failure in Callable returned from " + getBridgedMethod ().toGenericString ();
165
- throw new IllegalStateException (msg , ex );
167
+ future .completeExceptionally (ex );
166
168
}
167
- }, this .executor );
169
+ });
170
+ return future ;
171
+ }
172
+
173
+ private IllegalStateException processIllegalArgumentException (
174
+ Object [] argValues , IllegalArgumentException ex , Method method ) {
175
+
176
+ assertTargetBean (method , getBean (), argValues );
177
+ String text = (ex .getMessage () != null ) ? ex .getMessage () : "Illegal argument" ;
178
+ return new IllegalStateException (formatInvokeError (text , argValues ), ex );
179
+ }
180
+
181
+ private Throwable processInvocationTargetException (Object [] argValues , InvocationTargetException ex ) {
182
+ // Unwrap for DataFetcherExceptionResolvers ...
183
+ Throwable targetException = ex .getTargetException ();
184
+ if (targetException instanceof Error || targetException instanceof Exception ) {
185
+ return targetException ;
186
+ }
187
+ String message = formatInvokeError ("Invocation failure" , argValues );
188
+ return new IllegalStateException (message , targetException );
168
189
}
169
190
170
191
/**
0 commit comments