Skip to content

Commit 1d59c5f

Browse files
committed
StandardScriptEvaluator uses same eval exception exposure as ScriptTemplateView
Issue: SPR-13557
1 parent 92dc51f commit 1d59c5f

File tree

6 files changed

+89
-56
lines changed

6 files changed

+89
-56
lines changed

spring-context/src/main/java/org/springframework/scripting/bsh/BshScriptEvaluator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2015 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.
@@ -79,10 +79,10 @@ public Object evaluate(ScriptSource script, Map<String, Object> arguments) {
7979
return interpreter.eval(new StringReader(script.getScriptAsString()));
8080
}
8181
catch (IOException ex) {
82-
throw new ScriptCompilationException(script, "Cannot access script", ex);
82+
throw new ScriptCompilationException(script, "Cannot access BeanShell script", ex);
8383
}
8484
catch (EvalError ex) {
85-
throw new ScriptCompilationException(script, "Evaluation failure", ex);
85+
throw new ScriptCompilationException(script, ex);
8686
}
8787
}
8888

spring-context/src/main/java/org/springframework/scripting/groovy/GroovyScriptEvaluator.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
1+
/*
2+
* Copyright 2002-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package org.springframework.scripting.groovy;
218

319
import java.io.IOException;
420
import java.util.Map;
521

622
import groovy.lang.Binding;
23+
import groovy.lang.GroovyRuntimeException;
724
import groovy.lang.GroovyShell;
8-
import org.codehaus.groovy.control.CompilationFailedException;
925

1026
import org.springframework.beans.factory.BeanClassLoaderAware;
1127
import org.springframework.scripting.ScriptCompilationException;
@@ -65,10 +81,10 @@ public Object evaluate(ScriptSource script, Map<String, Object> arguments) {
6581
}
6682
}
6783
catch (IOException ex) {
68-
throw new ScriptCompilationException(script, "Cannot access script", ex);
84+
throw new ScriptCompilationException(script, "Cannot access Groovy script", ex);
6985
}
70-
catch (CompilationFailedException ex) {
71-
throw new ScriptCompilationException(script, "Evaluation failure", ex);
86+
catch (GroovyRuntimeException ex) {
87+
throw new ScriptCompilationException(script, ex);
7288
}
7389
}
7490

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2002-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.scripting.support;
18+
19+
import javax.script.ScriptException;
20+
21+
/**
22+
* Exception decorating a {@link javax.script.ScriptException} coming out of
23+
* JSR-223 script evaluation, i.e. a {@link javax.script.ScriptEngine#eval}
24+
* call or {@link javax.script.Invocable#invokeMethod} /
25+
* {@link javax.script.Invocable#invokeFunction} call.
26+
*
27+
* <p>This exception does not print the Java stacktrace, since the JSR-223
28+
* {@link ScriptException} results in a rather convoluted text output.
29+
* From that perspective, this exception is primarily a decorator for a
30+
* {@link ScriptException} root cause passed into an outer exception.
31+
*
32+
* @author Juergen Hoeller
33+
* @author Sebastien Deleuze
34+
* @since 4.2.2
35+
*/
36+
@SuppressWarnings("serial")
37+
public class StandardScriptEvalException extends RuntimeException {
38+
39+
private final ScriptException scriptException;
40+
41+
42+
/**
43+
* Construct a new script eval exception with the specified original exception.
44+
*/
45+
public StandardScriptEvalException(ScriptException ex) {
46+
super(ex.getMessage());
47+
this.scriptException = ex;
48+
}
49+
50+
51+
public final ScriptException getScriptException() {
52+
return this.scriptException;
53+
}
54+
55+
@Override
56+
public synchronized Throwable fillInStackTrace() {
57+
return this;
58+
}
59+
60+
}

spring-context/src/main/java/org/springframework/scripting/support/StandardScriptEvaluator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,10 @@ public Object evaluate(ScriptSource script, Map<String, Object> argumentBindings
133133
}
134134
}
135135
catch (IOException ex) {
136-
throw new ScriptCompilationException(script, "Cannot access script", ex);
136+
throw new ScriptCompilationException(script, "Cannot access script for ScriptEngine", ex);
137137
}
138138
catch (ScriptException ex) {
139-
throw new ScriptCompilationException(script, "Evaluation failure", ex);
139+
throw new ScriptCompilationException(script, new StandardScriptEvalException(ex));
140140
}
141141
}
142142

spring-webmvc/src/main/java/org/springframework/web/servlet/view/script/ScriptRenderException.java

Lines changed: 0 additions & 45 deletions
This file was deleted.

spring-webmvc/src/main/java/org/springframework/web/servlet/view/script/ScriptTemplateView.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import javax.script.Invocable;
3030
import javax.script.ScriptEngine;
3131
import javax.script.ScriptEngineManager;
32+
import javax.script.ScriptException;
3233
import javax.servlet.ServletException;
3334
import javax.servlet.http.HttpServletRequest;
3435
import javax.servlet.http.HttpServletResponse;
@@ -42,6 +43,7 @@
4243
import org.springframework.core.io.DefaultResourceLoader;
4344
import org.springframework.core.io.Resource;
4445
import org.springframework.core.io.ResourceLoader;
46+
import org.springframework.scripting.support.StandardScriptEvalException;
4547
import org.springframework.scripting.support.StandardScriptUtils;
4648
import org.springframework.util.Assert;
4749
import org.springframework.util.FileCopyUtils;
@@ -357,8 +359,8 @@ protected void renderMergedOutputModel(Map<String, Object> model, HttpServletReq
357359

358360
response.getWriter().write(String.valueOf(html));
359361
}
360-
catch (Exception ex) {
361-
throw new ServletException("Failed to render script template", new ScriptRenderException(ex.getMessage()));
362+
catch (ScriptException ex) {
363+
throw new ServletException("Failed to render script template", new StandardScriptEvalException(ex));
362364
}
363365
}
364366

0 commit comments

Comments
 (0)