mirror of
https://github.com/HMCL-dev/HMCL.git
synced 2025-09-18 16:26:05 -04:00
Collect uncaught RuntimeException thrown in task execution
This commit is contained in:
parent
e2787af044
commit
c9b6f2f62f
@ -22,6 +22,7 @@ import javafx.application.Platform;
|
|||||||
import javafx.stage.Stage;
|
import javafx.stage.Stage;
|
||||||
import org.jackhuang.hmcl.setting.ConfigHolder;
|
import org.jackhuang.hmcl.setting.ConfigHolder;
|
||||||
import org.jackhuang.hmcl.task.Schedulers;
|
import org.jackhuang.hmcl.task.Schedulers;
|
||||||
|
import org.jackhuang.hmcl.task.TaskExecutor;
|
||||||
import org.jackhuang.hmcl.ui.Controllers;
|
import org.jackhuang.hmcl.ui.Controllers;
|
||||||
import org.jackhuang.hmcl.upgrade.UpdateChecker;
|
import org.jackhuang.hmcl.upgrade.UpdateChecker;
|
||||||
import org.jackhuang.hmcl.util.CrashReporter;
|
import org.jackhuang.hmcl.util.CrashReporter;
|
||||||
@ -77,6 +78,7 @@ public final class Launcher extends Application {
|
|||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Thread.setDefaultUncaughtExceptionHandler(CRASH_REPORTER);
|
Thread.setDefaultUncaughtExceptionHandler(CRASH_REPORTER);
|
||||||
|
TaskExecutor.setUncaughtExceptionHandler(new CrashReporter(false));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
LOG.info("*** " + Metadata.TITLE + " ***");
|
LOG.info("*** " + Metadata.TITLE + " ***");
|
||||||
@ -142,5 +144,5 @@ public final class Launcher extends Application {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final CrashReporter CRASH_REPORTER = new CrashReporter();
|
public static final CrashReporter CRASH_REPORTER = new CrashReporter(true);
|
||||||
}
|
}
|
||||||
|
@ -86,6 +86,12 @@ public class CrashReporter implements Thread.UncaughtExceptionHandler {
|
|||||||
|
|
||||||
private static Set<String> CAUGHT_EXCEPTIONS = newSetFromMap(new ConcurrentHashMap<>());
|
private static Set<String> CAUGHT_EXCEPTIONS = newSetFromMap(new ConcurrentHashMap<>());
|
||||||
|
|
||||||
|
private final boolean showCrashWindow;
|
||||||
|
|
||||||
|
public CrashReporter(boolean showCrashWindow) {
|
||||||
|
this.showCrashWindow = showCrashWindow;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void uncaughtException(Thread t, Throwable e) {
|
public void uncaughtException(Thread t, Throwable e) {
|
||||||
LOG.log(Level.SEVERE, "Uncaught exception in thread " + t.getName(), 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);
|
LOG.log(Level.SEVERE, text);
|
||||||
|
|
||||||
if (checkThrowable(e)) {
|
if (checkThrowable(e)) {
|
||||||
|
if (showCrashWindow) {
|
||||||
Platform.runLater(() -> new CrashWindow(text).show());
|
Platform.runLater(() -> new CrashWindow(text).show());
|
||||||
|
}
|
||||||
if (!UpdateChecker.isOutdated() && IntegrityChecker.isSelfVerified()) {
|
if (!UpdateChecker.isOutdated() && IntegrityChecker.isSelfVerified()) {
|
||||||
reportToServer(text);
|
reportToServer(text);
|
||||||
}
|
}
|
||||||
|
@ -62,6 +62,13 @@ public final class TaskExecutor {
|
|||||||
if (!success) {
|
if (!success) {
|
||||||
// We log exception stacktrace because some of exceptions occurred because of bugs.
|
// We log exception stacktrace because some of exceptions occurred because of bugs.
|
||||||
Logging.LOG.log(Level.WARNING, "An exception occurred in task execution", exception);
|
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));
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user