Collect uncaught RuntimeException thrown in task execution

This commit is contained in:
huanghongxun 2019-08-24 11:49:53 +08:00
parent e2787af044
commit c9b6f2f62f
3 changed files with 25 additions and 2 deletions

View File

@ -22,6 +22,7 @@ import javafx.application.Platform;
import javafx.stage.Stage;
import org.jackhuang.hmcl.setting.ConfigHolder;
import org.jackhuang.hmcl.task.Schedulers;
import org.jackhuang.hmcl.task.TaskExecutor;
import org.jackhuang.hmcl.ui.Controllers;
import org.jackhuang.hmcl.upgrade.UpdateChecker;
import org.jackhuang.hmcl.util.CrashReporter;
@ -77,6 +78,7 @@ public final class Launcher extends Application {
public static void main(String[] args) {
Thread.setDefaultUncaughtExceptionHandler(CRASH_REPORTER);
TaskExecutor.setUncaughtExceptionHandler(new CrashReporter(false));
try {
LOG.info("*** " + Metadata.TITLE + " ***");
@ -142,5 +144,5 @@ public final class Launcher extends Application {
return result;
}
public static final CrashReporter CRASH_REPORTER = new CrashReporter();
public static final CrashReporter CRASH_REPORTER = new CrashReporter(true);
}

View File

@ -86,6 +86,12 @@ public class CrashReporter implements Thread.UncaughtExceptionHandler {
private static Set<String> CAUGHT_EXCEPTIONS = newSetFromMap(new ConcurrentHashMap<>());
private final boolean showCrashWindow;
public CrashReporter(boolean showCrashWindow) {
this.showCrashWindow = showCrashWindow;
}
@Override
public void uncaughtException(Thread t, Throwable e) {
LOG.log(Level.SEVERE, "Uncaught exception in thread " + t.getName(), e);
@ -116,7 +122,9 @@ public class CrashReporter implements Thread.UncaughtExceptionHandler {
LOG.log(Level.SEVERE, text);
if (checkThrowable(e)) {
Platform.runLater(() -> new CrashWindow(text).show());
if (showCrashWindow) {
Platform.runLater(() -> new CrashWindow(text).show());
}
if (!UpdateChecker.isOutdated() && IntegrityChecker.isSelfVerified()) {
reportToServer(text);
}

View File

@ -62,6 +62,13 @@ public final class TaskExecutor {
if (!success) {
// We log exception stacktrace because some of exceptions occurred because of bugs.
Logging.LOG.log(Level.WARNING, "An exception occurred in task execution", exception);
Throwable resolvedException = resolveException(exception);
if (resolvedException instanceof RuntimeException) {
// Track uncaught RuntimeException which are thrown mostly by our mistake
if (uncaughtExceptionHandler != null)
uncaughtExceptionHandler.uncaughtException(Thread.currentThread(), resolvedException);
}
}
taskListeners.forEach(it -> it.onStop(success, this));
@ -253,4 +260,10 @@ public final class TaskExecutor {
}
};
}
private static Thread.UncaughtExceptionHandler uncaughtExceptionHandler = null;
public static void setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler uncaughtExceptionHandler) {
TaskExecutor.uncaughtExceptionHandler = uncaughtExceptionHandler;
}
}