From 90cd24b45e78d00a21934fae1da2d05256fbbb44 Mon Sep 17 00:00:00 2001
From: Florian Angerer <florian.angerer@oracle.com>
Date: Mon, 16 Apr 2018 13:09:51 +0200
Subject: [PATCH] Refine internal error reporting.

---
 .../oracle/truffle/r/runtime/RDeparse.java    |  4 +--
 .../truffle/r/runtime/RInternalError.java     | 32 ++++++++++++++-----
 2 files changed, 25 insertions(+), 11 deletions(-)

diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RDeparse.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RDeparse.java
index 29b6af3ec5..a786065307 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RDeparse.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RDeparse.java
@@ -415,9 +415,7 @@ public class RDeparse {
                     }
                 }
             } catch (AccessDeniedException | FileAlreadyExistsException | IllegalArgumentException e) {
-                fixupSourcesTextInternal();
-            } catch (IOException e) {
-                RInternalError.reportError(e);
+                // do not report because these exceptions are legitimate
                 fixupSourcesTextInternal();
             } catch (Throwable e) {
                 RInternalError.reportError(e);
diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RInternalError.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RInternalError.java
index 7ba302c0e9..fc61fb2ff9 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RInternalError.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RInternalError.java
@@ -45,6 +45,8 @@ import com.oracle.truffle.r.runtime.context.RContext;
  */
 public final class RInternalError extends Error implements TruffleException {
 
+    private static final String FASTR_ERRORS_LOG = "fastr_errors.log";
+
     private static final long serialVersionUID = 80698622974155216L;
 
     private static boolean initializing = false;
@@ -154,20 +156,24 @@ public final class RInternalError extends Error implements TruffleException {
 
     @TruffleBoundary
     public static void reportErrorAndConsoleLog(Throwable throwable, int contextId) {
-        reportError(throwable, contextId);
+        reportErrorDefault(throwable, contextId);
     }
 
     @TruffleBoundary
     public static void reportError(Throwable t) {
-        String message = t instanceof RInternalError && t.getMessage() != null && !t.getMessage().isEmpty() ? t.getMessage() : "internal error: " + t.getClass().getSimpleName();
-        reportError(message, t, 0);
+        reportErrorDefault(t, 0);
+    }
+
+    @TruffleBoundary
+    private static void reportErrorDefault(Throwable t, int contextId) {
+        String errMsg = t instanceof RInternalError && t.getMessage() != null && !t.getMessage().isEmpty() ? t.getMessage() : t.getClass().getSimpleName();
+        reportError(errMsg, t, contextId);
     }
 
-    private static void reportError(String message, Throwable throwable, int contextId) {
+    private static void reportError(String errMsg, Throwable throwable, int contextId) {
         try {
             Throwable t = throwable;
             if (FastROptions.PrintErrorStacktracesToFile.getBooleanValue() || FastROptions.PrintErrorStacktraces.getBooleanValue()) {
-
                 ByteArrayOutputStream out = new ByteArrayOutputStream();
                 t.printStackTrace(new PrintStream(out));
                 String verboseStackTrace;
@@ -185,9 +191,15 @@ public final class RInternalError extends Error implements TruffleException {
                     System.err.println(out.toString());
                     System.err.println(verboseStackTrace);
                 }
+
+                String message = "An internal error occurred: \"" + errMsg + "\"\nPlease report an issue including the commands";
+                if (FastROptions.PrintErrorStacktracesToFile.getBooleanValue()) {
+                    message += " and the error log file '" + getLogFileName(contextId) + "'.";
+                } else {
+                    message += ".";
+                }
                 if (FastROptions.PrintErrorStacktracesToFile.getBooleanValue()) {
-                    String suffix = contextId == 0 ? "" : "-" + Integer.toString(contextId);
-                    Path logfile = Utils.getLogPath("fastr_errors.log" + suffix);
+                    Path logfile = Utils.getLogPath(getLogFileName(contextId));
                     try (BufferedWriter writer = Files.newBufferedWriter(logfile, StandardCharsets.UTF_8, StandardOpenOption.APPEND,
                                     StandardOpenOption.CREATE)) {
                         writer.append(new Date().toString()).append('\n');
@@ -196,7 +208,7 @@ public final class RInternalError extends Error implements TruffleException {
                     } catch (IOException e) {
                         e.printStackTrace();
                     }
-                    System.err.println(message + " (see fastr_errors.log" + suffix + ")");
+                    System.err.println(message);
                     if (RContext.isEmbedded()) {
                         RSuicide.rSuicide("FastR internal error");
                     }
@@ -211,6 +223,10 @@ public final class RInternalError extends Error implements TruffleException {
         }
     }
 
+    private static String getLogFileName(int contextId) {
+        return contextId == 0 ? FASTR_ERRORS_LOG : FASTR_ERRORS_LOG + "-" + Integer.toString(contextId);
+    }
+
     @Override
     public Node getLocation() {
         return null;
-- 
GitLab