Skip to content

Commit 5c81f93

Browse files
committed
Improve error handling of spel expressions
1 parent 3788615 commit 5c81f93

2 files changed

Lines changed: 64 additions & 20 deletions

File tree

src/main/java/eu/openanalytics/containerproxy/spec/expression/SpecExpressionResolver.java

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*/
2121
package eu.openanalytics.containerproxy.spec.expression;
2222

23+
import eu.openanalytics.containerproxy.ContainerProxyException;
2324
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
2425
import org.springframework.context.ApplicationContext;
2526
import org.springframework.context.ConfigurableApplicationContext;
@@ -31,8 +32,11 @@
3132
import org.springframework.context.expression.StandardBeanExpressionResolver;
3233
import org.springframework.core.convert.ConversionService;
3334
import org.springframework.expression.Expression;
35+
import org.springframework.expression.ExpressionException;
3436
import org.springframework.expression.ExpressionParser;
37+
import org.springframework.expression.ParseException;
3538
import org.springframework.expression.ParserContext;
39+
import org.springframework.expression.spel.SpelEvaluationException;
3640
import org.springframework.expression.spel.standard.SpelExpressionParser;
3741
import org.springframework.expression.spel.support.StandardEvaluationContext;
3842
import org.springframework.expression.spel.support.StandardTypeConverter;
@@ -78,27 +82,33 @@ public SpecExpressionResolver(ApplicationContext appContext) {
7882
public <T> T evaluate(String expression, SpecExpressionContext context, Class<T> resType) {
7983
if (expression == null) return null;
8084
if (expression.isEmpty()) return null;
81-
82-
Expression expr = this.expressionParser.parseExpression(expression, this.beanExpressionParserContext);
83-
84-
ConfigurableBeanFactory beanFactory = ((ConfigurableApplicationContext) appContext).getBeanFactory();
85-
86-
StandardEvaluationContext sec = evaluationCache.get(context);
87-
if (sec == null) {
88-
sec = new StandardEvaluationContext();
89-
sec.setRootObject(context);
90-
sec.addPropertyAccessor(new BeanExpressionContextAccessor());
91-
sec.addPropertyAccessor(new BeanFactoryAccessor());
92-
sec.addPropertyAccessor(new MapAccessor());
93-
sec.addPropertyAccessor(new EnvironmentAccessor());
94-
sec.setBeanResolver(new BeanFactoryResolver(appContext));
95-
sec.setTypeLocator(new StandardTypeLocator(beanFactory.getBeanClassLoader()));
96-
ConversionService conversionService = beanFactory.getConversionService();
97-
if (conversionService != null) sec.setTypeConverter(new StandardTypeConverter(conversionService));
98-
evaluationCache.put(context, sec);
85+
86+
try {
87+
Expression expr = this.expressionParser.parseExpression(expression, this.beanExpressionParserContext);
88+
89+
ConfigurableBeanFactory beanFactory = ((ConfigurableApplicationContext) appContext).getBeanFactory();
90+
91+
StandardEvaluationContext sec = evaluationCache.get(context);
92+
if (sec == null) {
93+
sec = new StandardEvaluationContext();
94+
sec.setRootObject(context);
95+
sec.addPropertyAccessor(new BeanExpressionContextAccessor());
96+
sec.addPropertyAccessor(new BeanFactoryAccessor());
97+
sec.addPropertyAccessor(new MapAccessor());
98+
sec.addPropertyAccessor(new EnvironmentAccessor());
99+
sec.setBeanResolver(new BeanFactoryResolver(appContext));
100+
sec.setTypeLocator(new StandardTypeLocator(beanFactory.getBeanClassLoader()));
101+
ConversionService conversionService = beanFactory.getConversionService();
102+
if (conversionService != null) sec.setTypeConverter(new StandardTypeConverter(conversionService));
103+
evaluationCache.put(context, sec);
104+
}
105+
106+
return expr.getValue(sec, resType);
107+
} catch (ExpressionException ex) {
108+
throw new SpelException(ex, expression);
109+
} catch (Throwable ex) {
110+
throw new SpelException(ex, expression);
99111
}
100-
101-
return expr.getValue(sec, resType);
102112
}
103113

104114
public String evaluateToString(String expression, SpecExpressionContext context) {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* ContainerProxy
3+
*
4+
* Copyright (C) 2016-2021 Open Analytics
5+
*
6+
* ===========================================================================
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the Apache License as published by
10+
* The Apache Software Foundation, either version 2 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* Apache License for more details.
17+
*
18+
* You should have received a copy of the Apache License
19+
* along with this program. If not, see <http://www.apache.org/licenses/>
20+
*/
21+
package eu.openanalytics.containerproxy.spec.expression;
22+
23+
import org.springframework.expression.ExpressionException;
24+
25+
public class SpelException extends RuntimeException {
26+
27+
public SpelException(ExpressionException cause, String originalExpression) {
28+
super("Error while resolving expression: \"" + originalExpression + "\", error: " + cause.getMessage());
29+
}
30+
31+
public SpelException(Throwable cause, String originalExpression) {
32+
super("Error while resolving expression: \"" + originalExpression + "\", error: " + cause.getMessage(), cause);
33+
}
34+
}

0 commit comments

Comments
 (0)