Skip to content

Commit 06841ed

Browse files
committed
improve formating and displaying of script errors
1 parent 2884b5b commit 06841ed

File tree

4 files changed

+172
-23
lines changed

4 files changed

+172
-23
lines changed

src/main/java/org/openstreetmap/josm/plugins/scripting/ui/console/ErrorOutputPanel.java

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33

44
import org.graalvm.polyglot.PolyglotException;
5+
import org.mozilla.javascript.EcmaError;
56
import org.openstreetmap.josm.plugins.scripting.graalvm.GraalVMFacadeFactory;
67

8+
import javax.script.ScriptException;
79
import javax.swing.*;
810
import javax.validation.constraints.NotNull;
911
import javax.validation.constraints.Null;
@@ -20,7 +22,6 @@
2022
public class ErrorOutputPanel extends JPanel {
2123

2224
private JTextPane paneOutput;
23-
private JScrollPane editorScrollPane;
2425

2526
public ErrorOutputPanel() {
2627
build();
@@ -30,7 +31,7 @@ protected void build() {
3031
setLayout(new BorderLayout());
3132
paneOutput = new JTextPane();
3233
paneOutput.setEditable(false);
33-
editorScrollPane = new JScrollPane(paneOutput);
34+
JScrollPane editorScrollPane = new JScrollPane(paneOutput);
3435
editorScrollPane.setVerticalScrollBarPolicy(
3536
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
3637
editorScrollPane.setHorizontalScrollBarPolicy(
@@ -40,6 +41,17 @@ protected void build() {
4041

4142
protected void displayPolyglotException(PolyglotException exception) {
4243
paneOutput.setText(formatPolyglotException(exception));
44+
paneOutput.setCaretPosition(0);
45+
}
46+
47+
protected void displayMozillaEcmaError(EcmaError exception) {
48+
paneOutput.setText(exception.getMessage());
49+
paneOutput.setCaretPosition(0);
50+
}
51+
52+
protected void displayScriptException(ScriptException exception) {
53+
paneOutput.setText(exception.getMessage());
54+
paneOutput.setCaretPosition(0);
4355
}
4456

4557
protected void displayGeneralException(Throwable exception) {
@@ -49,39 +61,50 @@ protected void displayGeneralException(Throwable exception) {
4961
final var writer = new StringWriter();
5062
exception.printStackTrace(new PrintWriter(writer));
5163
builder.append(writer.getBuffer());
52-
paneOutput.setText(builder.toString()); }
53-
64+
paneOutput.setText(builder.toString());
65+
paneOutput.setCaretPosition(0);
66+
}
5467

5568
/**
56-
* Displays an exception
69+
* Displays an exception. If <code>exception</code> is null, an empty
70+
* string is displayed.
5771
*
5872
* @param exception the exception
5973
*/
60-
public void displayException(@NotNull Throwable exception) {
74+
public void displayException(@Null Throwable exception) {
75+
if (exception == null) {
76+
paneOutput.setText("");
77+
paneOutput.setCaretPosition(0);
78+
return;
79+
}
6180
if (GraalVMFacadeFactory.isGraalVMPresent()) {
62-
final var polyglotException = lookupPolyglotException(exception);
81+
final var polyglotException = lookupCauseByExceptionType(exception, PolyglotException.class);
6382
if (polyglotException != null) {
64-
displayPolyglotException(polyglotException);
83+
displayPolyglotException((PolyglotException) polyglotException);
6584
return;
6685
}
6786
}
87+
final var mozillaEcmaError = lookupCauseByExceptionType(exception, EcmaError.class);
88+
if (mozillaEcmaError != null) {
89+
displayMozillaEcmaError((EcmaError) mozillaEcmaError);
90+
return;
91+
}
92+
final var scriptException = lookupCauseByExceptionType(exception, ScriptException.class);
93+
if (scriptException != null) {
94+
displayScriptException((ScriptException) scriptException);
95+
return;
96+
}
6897
displayGeneralException(exception);
69-
// scroll to top
70-
paneOutput.setCaretPosition(0);
7198
}
7299

73-
protected @Null PolyglotException lookupPolyglotException(Throwable t) {
100+
protected @Null Throwable lookupCauseByExceptionType(Throwable t, Class<? extends Throwable> clazz) {
74101
while(t != null) {
75-
if (t instanceof PolyglotException) {
102+
if (clazz.isInstance(t)) {
76103
break;
77104
}
78105
t = t.getCause();
79106
}
80-
if (t == null) {
81-
return null;
82-
} else {
83-
return (PolyglotException) t;
84-
}
107+
return t;
85108
}
86109

87110
protected String formatPolyglotException(@NotNull final PolyglotException exception) {

src/main/java/org/openstreetmap/josm/plugins/scripting/ui/console/ScriptingConsolePanel.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ protected JPanel buildOutputTabPanel() {
5656
tabPane.addTab(tr("Console"), log = new ScriptLogPanel());
5757
tabPane.setToolTipTextAt(0, tr("Displays script output"));
5858
tabPane.addTab(tr("Errors"), errorOutput);
59+
tabPane.setIconAt(1, ImageProvider.get(
60+
"circle-check-solid",
61+
ImageProvider.ImageSizes.SMALLICON));
5962
tabPane.setToolTipTextAt(1, tr("Displays scripting errors"));
6063

6164
ErrorModel.getInstance().addPropertyChangeListener(
@@ -207,6 +210,7 @@ public RunScriptAction(ScriptEditorModel model) {
207210

208211
@Override
209212
public void actionPerformed(ActionEvent e) {
213+
ErrorModel.getInstance().clearError();
210214
final String source = editor.getScript();
211215
switch(model.getScriptEngineDescriptor().getEngineType()) {
212216
case EMBEDDED:
@@ -274,16 +278,19 @@ public void propertyChange(PropertyChangeEvent event) {
274278
if (! ErrorModel.PROP_ERROR.equals(event.getPropertyName())) {
275279
return;
276280
}
277-
//TODO(gubaer): set an icon depending on status instead of background colors
278281
if (event.getNewValue() != null) {
279-
outputTabs.setBackgroundAt(1, Color.RED);
282+
outputTabs.setIconAt(1, ImageProvider.get("bug-solid",
283+
ImageProvider.ImageSizes.SMALLICON));
280284
} else {
281-
outputTabs.setBackgroundAt(1, outputTabs.getBackgroundAt(0));
285+
outputTabs.setIconAt(1, ImageProvider.get("circle-check-solid",
286+
ImageProvider.ImageSizes.SMALLICON));
282287
}
283-
if (! (event.getNewValue() instanceof Throwable)) {
284-
return;
288+
if (event.getNewValue() == null) {
289+
// clear the panel
290+
outputPanel.displayException(null);
291+
} if (event.getNewValue() instanceof Throwable) {
292+
outputPanel.displayException((Throwable) event.getNewValue());
285293
}
286-
outputPanel.displayException((Throwable) event.getNewValue());
287294
}
288295
}
289296
}
Lines changed: 62 additions & 0 deletions
Loading
Lines changed: 57 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)