groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From emil...@apache.org
Subject [groovy] branch master updated: save source context in uncaught exception error message
Date Fri, 10 Jan 2020 17:14:32 GMT
This is an automated email from the ASF dual-hosted git repository.

emilles pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new 137850c  save source context in uncaught exception error message
137850c is described below

commit 137850cbeb0a694fafa42a06637bbd6974bcca8b
Author: Eric Milles <eric.milles@thomsonreuters.com>
AuthorDate: Fri Jan 10 11:01:58 2020 -0600

    save source context in uncaught exception error message
---
 .../codehaus/groovy/control/CompilationUnit.java   | 39 ++++++++++------------
 .../groovy/control/messages/ExceptionMessage.java  | 28 ++++++----------
 2 files changed, 27 insertions(+), 40 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/control/CompilationUnit.java b/src/main/java/org/codehaus/groovy/control/CompilationUnit.java
index 053cad2..eb915ef 100644
--- a/src/main/java/org/codehaus/groovy/control/CompilationUnit.java
+++ b/src/main/java/org/codehaus/groovy/control/CompilationUnit.java
@@ -930,10 +930,23 @@ public class CompilationUnit extends ProcessingUnit {
                 } catch (GroovyBugError e) {
                     unit.changeBugText(e, context);
                     throw e;
-                } catch (NoClassDefFoundError | Exception e) {
-                    // effort to get more logging in case a dependency of a class is loaded
-                    // although it shouldn't have
-                    unit.convertUncaughtExceptionToCompilationError(e);
+                } catch (Exception | LinkageError e) {
+                    ErrorCollector errorCollector = null;
+                    // check for a nested compilation exception
+                    for (Throwable t = e.getCause(); t != e && t != null; t = t.getCause())
{
+                        if (t instanceof MultipleCompilationErrorsException) {
+                            errorCollector = ((MultipleCompilationErrorsException) t).getErrorCollector();
+                            break;
+                        }
+                    }
+
+                    if (errorCollector != null) {
+                        unit.getErrorCollector().addCollectorContents(errorCollector);
+                    } else if (context != null) {
+                        unit.getErrorCollector().addException(e instanceof Exception ? (Exception)
e : new RuntimeException(e), context);
+                    } else {
+                        unit.getErrorCollector().addError(new ExceptionMessage(e instanceof
Exception ? (Exception) e : new RuntimeException(e), unit.debug, unit));
+                    }
                 }
             }
             unit.getErrorCollector().failIfErrors();
@@ -1035,24 +1048,6 @@ public class CompilationUnit extends ProcessingUnit {
         return sorted;
     }
 
-    private void convertUncaughtExceptionToCompilationError(final Throwable e) {
-        ErrorCollector nestedCollector = null;
-        // check the exception for a nested compilation exception
-        for (Throwable next = e.getCause(); next != e && next != null; next = next.getCause())
{
-            if (!(next instanceof MultipleCompilationErrorsException)) continue;
-            MultipleCompilationErrorsException mcee = (MultipleCompilationErrorsException)
next;
-            nestedCollector = mcee.collector;
-            break;
-        }
-
-        if (nestedCollector != null) {
-            getErrorCollector().addCollectorContents(nestedCollector);
-        } else {
-            Exception err = e instanceof Exception?((Exception)e):new RuntimeException(e);
-            getErrorCollector().addError(new ExceptionMessage(err, debug, this));
-        }
-    }
-
     private void changeBugText(final GroovyBugError e, final SourceUnit context) {
         e.setBugText("exception in phase '" + getPhaseDescription() + "' in source unit '"
+ (context != null ? context.getName() : "?") + "' " + e.getBugText());
     }
diff --git a/src/main/java/org/codehaus/groovy/control/messages/ExceptionMessage.java b/src/main/java/org/codehaus/groovy/control/messages/ExceptionMessage.java
index 57c92fb..cd7621f 100644
--- a/src/main/java/org/codehaus/groovy/control/messages/ExceptionMessage.java
+++ b/src/main/java/org/codehaus/groovy/control/messages/ExceptionMessage.java
@@ -22,36 +22,34 @@ import org.codehaus.groovy.control.Janitor;
 import org.codehaus.groovy.control.ProcessingUnit;
 
 import java.io.PrintWriter;
+import java.util.Objects;
 
 /**
  * A class for error messages produced by the parser system.
  */
 public class ExceptionMessage extends Message {
-    protected boolean verbose = true;
 
-    private Exception cause = null;   // The exception source of the message, if any
-    ProcessingUnit owner = null;
+    private final Exception cause;
+    protected final boolean debug;
+    protected final ProcessingUnit owner;
 
-    public ExceptionMessage(Exception cause, boolean v, ProcessingUnit owner) {
-        this.verbose = v;
-        this.cause = cause;
+    public ExceptionMessage(final Exception cause, final boolean debug, final ProcessingUnit
owner) {
+        this.cause = Objects.requireNonNull(cause);
+        this.debug = debug;
         this.owner = owner;
     }
 
     /**
      * Returns the underlying Exception.
      */
-
     public Exception getCause() {
-        return this.cause;
+        return cause;
     }
 
-
     /**
      * Writes out a nicely formatted summary of the exception.
      */
-
-    public void write(PrintWriter output, Janitor janitor) {
+    public void write(final PrintWriter output, final Janitor janitor) {
         String description = "General error during " + owner.getPhaseDescription() + ": ";
 
         String message = cause.getMessage();
@@ -62,12 +60,6 @@ public class ExceptionMessage extends Message {
         }
         output.println();
 
-        //if (verbose) {
-        cause.printStackTrace(output);
-        //}
+        /*if (debug)*/ cause.printStackTrace(output);
     }
-
 }
-
-
-


Mime
View raw message