1
1
/*
2
- * Copyright 2002-2023 the original author or authors.
2
+ * Copyright 2002-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -87,23 +87,13 @@ protected Object doInvoke(GraphQLContext graphQLContext, Object... argValues) {
87
87
return invokeSuspendingFunction (getBean (), method , argValues );
88
88
}
89
89
Object result = method .invoke (getBean (), argValues );
90
- return handleReturnValue (graphQLContext , result );
90
+ return handleReturnValue (graphQLContext , result , method , argValues );
91
91
}
92
92
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 ));
96
94
}
97
95
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 ));
107
97
}
108
98
catch (Throwable ex ) {
109
99
return Mono .error (ex );
@@ -124,24 +114,51 @@ private static Object invokeSuspendingFunction(Object bean, Method method, Objec
124
114
}
125
115
126
116
@ 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
+
129
121
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 ;
141
140
}
142
141
return result ;
143
142
}
144
143
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
+
145
162
/**
146
163
* Use this method to resolve the arguments asynchronously. This is only
147
164
* useful when at least one of the values is a {@link Mono}
0 commit comments