Skip to content

Commit 647b634

Browse files
committed
Merge branch 'scripts'
This topic branch brings fixes required by the script editor (so that errors are shown in the script editor, not in the console that might be hidden from the user by the operating system). Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2 parents a8d620a + 1364e3c commit 647b634

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

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

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

3434
import java.io.FileReader;
35+
import java.io.IOException;
36+
import java.io.PrintWriter;
3537
import java.io.Reader;
3638
import java.io.Writer;
3739

@@ -146,7 +148,13 @@ public void run() {
146148
engine.put(ScriptEngine.FILENAME, path);
147149
final ScriptContext scriptContext = engine.getContext();
148150
if (output != null) scriptContext.setWriter(output);
149-
if (error != null) scriptContext.setErrorWriter(error);
151+
final PrintWriter errorPrinter;
152+
if (error != null) {
153+
scriptContext.setErrorWriter(error);
154+
errorPrinter = new PrintWriter(error);
155+
} else {
156+
errorPrinter = null;
157+
}
150158

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

174186
// populate output values
@@ -180,6 +192,20 @@ public void run() {
180192
final Object typed = ConversionUtils.convert(decoded, item.getType());
181193
setOutput(name, typed);
182194
}
195+
196+
if (output != null) try {
197+
output.flush();
198+
} catch (IOException e) {
199+
if (error == null) {
200+
log.error(e);
201+
} else {
202+
e.printStackTrace(errorPrinter);
203+
}
204+
}
205+
206+
if (errorPrinter != null) {
207+
errorPrinter.flush();
208+
}
183209
}
184210

185211
// -- Contextual methods --

0 commit comments

Comments
 (0)