Skip to content

Commit 028ff9b

Browse files
committed
Unwrap InvocationTargetException in SpEL's FunctionReference
FunctionReference in the Spring Expression Language (SpEL) currently does not unwrap an InvocationTargetException; however, ConstructorReference and MethodReference do. For example, currently one may encounter an exception like the following, where the 'null' comes from the fact that an InvocationTargetException doesn't always have a message. SpelEvaluationException: EL1023E: A problem occurred whilst attempting to invoke the function 'formatObjectVarargs': 'null' To address that, and to align with the behavior of ConstructorReference and MethodReference, this commit modifies FunctionReference so that it unwraps the InvocationTargetException to use its cause for the exception message, resulting in an exception message like the following. SpelEvaluationException: EL1023E: A problem occurred whilst attempting to invoke the function 'formatObjectVarargs': 'Format specifier '%s'' Closes gh-33174
1 parent e2ce811 commit 028ff9b

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

spring-expression/src/main/java/org/springframework/expression/spel/ast/FunctionReference.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.lang.invoke.MethodHandle;
2020
import java.lang.invoke.MethodType;
21+
import java.lang.reflect.InvocationTargetException;
2122
import java.lang.reflect.Method;
2223
import java.lang.reflect.Modifier;
2324
import java.util.StringJoiner;
@@ -145,8 +146,10 @@ private TypedValue executeFunctionViaMethod(ExpressionState state, Method method
145146
return new TypedValue(result, new TypeDescriptor(new MethodParameter(method, -1)).narrow(result));
146147
}
147148
catch (Exception ex) {
148-
throw new SpelEvaluationException(getStartPosition(), ex, SpelMessage.EXCEPTION_DURING_FUNCTION_CALL,
149-
this.name, ex.getMessage());
149+
Throwable cause = ((ex instanceof InvocationTargetException ite && ite.getCause() != null) ?
150+
ite.getCause() : ex);
151+
throw new SpelEvaluationException(getStartPosition(), cause, SpelMessage.EXCEPTION_DURING_FUNCTION_CALL,
152+
this.name, cause.getMessage());
150153
}
151154
finally {
152155
if (compilable) {

0 commit comments

Comments
 (0)