From a70687aaec5d8358eba0521c16f56aa332e0c2c5 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Mon, 30 Oct 2017 21:50:14 -0700 Subject: [PATCH] Improve error reporting for the new TCP netconsole. (#700) Fixes #695. --- .../edu/wpi/first/wpilibj/DriverStation.java | 51 +++++++++++++++---- .../java/edu/wpi/first/wpilibj/RobotBase.java | 32 +++++++----- 2 files changed, 59 insertions(+), 24 deletions(-) diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/DriverStation.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/DriverStation.java index be2870c3ab..ca8c3c91a2 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/DriverStation.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/DriverStation.java @@ -154,7 +154,7 @@ public class DriverStation implements RobotState.Interface { } /** - * Report error to Driver Station. Also prints error to System.err Optionally appends Stack trace + * Report error to Driver Station. Optionally appends Stack trace * to error message. * * @param printTrace If true, append stack trace to error string @@ -164,7 +164,17 @@ public class DriverStation implements RobotState.Interface { } /** - * Report warning to Driver Station. Also prints error to System.err Optionally appends Stack + * Report error to Driver Station. Appends provided stack trace + * to error message. + * + * @param stackTrace The stack trace to append + */ + public static void reportError(String error, StackTraceElement[] stackTrace) { + reportErrorImpl(true, 1, error, stackTrace); + } + + /** + * Report warning to Driver Station. Optionally appends Stack * trace to warning message. * * @param printTrace If true, append stack trace to warning string @@ -173,27 +183,46 @@ public class DriverStation implements RobotState.Interface { reportErrorImpl(false, 1, error, printTrace); } + /** + * Report warning to Driver Station. Appends provided stack + * trace to warning message. + * + * @param stackTrace The stack trace to append + */ + public static void reportWarning(String error, StackTraceElement[] stackTrace) { + reportErrorImpl(false, 1, error, stackTrace); + } + private static void reportErrorImpl(boolean isError, int code, String error, boolean printTrace) { - StackTraceElement[] traces = Thread.currentThread().getStackTrace(); + reportErrorImpl(isError, code, error, printTrace, Thread.currentThread().getStackTrace(), 3); + } + + private static void reportErrorImpl(boolean isError, int code, String error, + StackTraceElement[] stackTrace) { + reportErrorImpl(isError, code, error, true, stackTrace, 0); + } + + private static void reportErrorImpl(boolean isError, int code, String error, + boolean printTrace, StackTraceElement[] stackTrace, int stackTraceFirst) { String locString; - if (traces.length > 3) { - locString = traces[3].toString(); + if (stackTrace.length >= stackTraceFirst + 1) { + locString = stackTrace[stackTraceFirst].toString(); } else { locString = ""; } boolean haveLoc = false; - String traceString = " at "; - for (int i = 3; i < traces.length; i++) { - String loc = traces[i].toString(); - traceString += loc + "\n"; + String traceString = ""; + for (int i = stackTraceFirst; i < stackTrace.length; i++) { + String loc = stackTrace[i].toString(); + traceString += "\tat " + loc + "\n"; // get first user function - if (!haveLoc && !loc.startsWith("edu.wpi.first.wpilibj")) { + if (!haveLoc && !loc.startsWith("edu.wpi.first")) { locString = loc; haveLoc = true; } } - HAL.sendError(isError, code, false, error, locString, printTrace ? traceString : "", true); + HAL.sendError(isError, code, false, error, locString, printTrace ? traceString : "", false); } /** diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/RobotBase.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/RobotBase.java index a5fa8a342c..54cb02d84d 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/RobotBase.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/RobotBase.java @@ -11,7 +11,6 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.net.URL; -import java.util.Arrays; import java.util.Enumeration; import java.util.jar.Manifest; //import org.opencv.core.Core; @@ -223,10 +222,14 @@ public abstract class RobotBase { try { robot = (RobotBase) Class.forName(robotName).newInstance(); } catch (Throwable throwable) { - DriverStation.reportError("ERROR Unhandled exception instantiating robot " + robotName + " " - + throwable.toString() + " at " + Arrays.toString(throwable.getStackTrace()), false); - System.err.println("WARNING: Robots don't quit!"); - System.err.println("ERROR: Could not instantiate robot " + robotName + "!"); + Throwable cause = throwable.getCause(); + if (cause != null) { + throwable = cause; + } + DriverStation.reportError("Unhandled exception instantiating robot " + robotName + " " + + throwable.toString(), throwable.getStackTrace()); + DriverStation.reportWarning("Robots should not quit, but yours did!", false); + DriverStation.reportError("Could not instantiate robot " + robotName + "!", false); System.exit(1); return; } @@ -253,19 +256,22 @@ public abstract class RobotBase { try { robot.startCompetition(); } catch (Throwable throwable) { - DriverStation.reportError( - "ERROR Unhandled exception: " + throwable.toString() + " at " - + Arrays.toString(throwable.getStackTrace()), false); + Throwable cause = throwable.getCause(); + if (cause != null) { + throwable = cause; + } + DriverStation.reportError("Unhandled exception: " + throwable.toString(), + throwable.getStackTrace()); errorOnExit = true; } finally { // startCompetition never returns unless exception occurs.... - System.err.println("WARNING: Robots don't quit!"); + DriverStation.reportWarning("Robots should not quit, but yours did!", false); if (errorOnExit) { - System.err - .println("---> The startCompetition() method (or methods called by it) should have " - + "handled the exception above."); + DriverStation.reportError( + "The startCompetition() method (or methods called by it) should have " + + "handled the exception above.", false); } else { - System.err.println("---> Unexpected return from startCompetition() method."); + DriverStation.reportError("Unexpected return from startCompetition() method.", false); } } System.exit(1);