22
33
44import org .graalvm .polyglot .PolyglotException ;
5+ import org .mozilla .javascript .EcmaError ;
56import org .openstreetmap .josm .plugins .scripting .graalvm .GraalVMFacadeFactory ;
67
8+ import javax .script .ScriptException ;
79import javax .swing .*;
810import javax .validation .constraints .NotNull ;
911import javax .validation .constraints .Null ;
2022public 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 ) {
0 commit comments