Skip to content

Commit a29c37d

Browse files
committed
If stderr is provided, ScriptModules should print errors there
This is needed for the script editor that wants to show errors in its error screen. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 7d3ebeb commit a29c37d

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/main/java/org/scijava/script/ScriptModule.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
package org.scijava.script;
3333

3434
import java.io.FileReader;
35+
import java.io.PrintWriter;
3536
import java.io.Reader;
3637
import java.io.Writer;
3738

@@ -146,7 +147,13 @@ public void run() {
146147
engine.put(ScriptEngine.FILENAME, path);
147148
final ScriptContext scriptContext = engine.getContext();
148149
if (output != null) scriptContext.setWriter(output);
149-
if (error != null) scriptContext.setErrorWriter(error);
150+
final PrintWriter errorPrinter;
151+
if (error != null) {
152+
scriptContext.setErrorWriter(error);
153+
errorPrinter = new PrintWriter(error);
154+
} else {
155+
errorPrinter = null;
156+
}
150157

151158
// populate bindings with the input values
152159
for (final ModuleItem<?> item : getInfo().inputs()) {
@@ -164,11 +171,15 @@ public void run() {
164171
setOutput(RETURN_VALUE, language.decode(returnValue));
165172
setResolved(RETURN_VALUE, true);
166173
}
167-
catch (final ScriptException e) {
168-
log.error(e.getCause());
169-
}
170-
catch (final Throwable e) {
171-
log.error(e);
174+
catch (Throwable e) {
175+
while (e instanceof ScriptException && e.getCause() != null) {
176+
e = e.getCause();
177+
}
178+
if (error == null) {
179+
log.error(e);
180+
} else {
181+
e.printStackTrace(errorPrinter);
182+
}
172183
}
173184

174185
// populate output values

0 commit comments

Comments
 (0)