Skip to content

Commit

Permalink
Retire error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
crschnick committed Mar 6, 2024
1 parent b40d03d commit 8b24eb1
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 221 deletions.
123 changes: 9 additions & 114 deletions pdxu-app/src/main/java/com/crschnick/pdxu/app/core/ErrorHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,14 @@
import com.crschnick.pdxu.app.gui.dialog.GuiErrorReporter;
import com.crschnick.pdxu.app.util.SupportedOs;
import com.crschnick.pdxu.app.util.ThreadHelper;
import io.sentry.Attachment;
import io.sentry.Sentry;
import io.sentry.SentryEvent;
import io.sentry.UserFeedback;
import io.sentry.protocol.SentryId;
import javafx.application.Platform;
import org.apache.commons.io.FileUtils;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ErrorHandler {

Expand Down Expand Up @@ -110,7 +98,7 @@ public static void setPlatformShutdown() {

public static void registerThread(Thread thread) {
thread.setUncaughtExceptionHandler((t, e) -> {
handleException(e, "An uncaught exception was thrown", null);
handleException(e, "An uncaught exception was thrown");
});
}

Expand Down Expand Up @@ -142,26 +130,25 @@ private static void unpreparedStartup(Throwable ex) {
}

public static void handleTerminalException(Throwable ex) {
handleException(ex, null, null, true);
handleException(ex, null, true);
}

public static void handleException(Throwable ex) {
handleException(ex, "An error occured", null);
handleException(ex, "An error occured");
}

public static void handleException(Throwable ex, String msg, Path attachFile) {
handleException(ex, msg, attachFile, false);
public static void handleException(Throwable ex, String msg) {
handleException(ex, msg, false);
}

private static CountDownLatch showErrorReporter(Throwable ex, Path attachFile, boolean terminal) {
private static CountDownLatch showErrorReporter(Throwable ex, boolean terminal) {
CountDownLatch latch = new CountDownLatch(1);
Runnable run = () -> {
boolean show = (PdxuInstallation.getInstance() == null ||
PdxuInstallation.getInstance().isProduction()) && !errorReporterShowing;
if (show) {
errorReporterShowing = true;
boolean shouldSendDiagnostics = GuiErrorReporter.showException(ex, terminal);
reportError(ex, shouldSendDiagnostics, attachFile, terminal);
GuiErrorReporter.showException(ex, terminal);
errorReporterShowing = false;
}

Expand All @@ -175,7 +162,7 @@ private static CountDownLatch showErrorReporter(Throwable ex, Path attachFile, b
return latch;
}

private static void handleException(Throwable ex, String msg, Path attachFile, boolean terminal) {
private static void handleException(Throwable ex, String msg, boolean terminal) {
if (ex == null) {
return;
}
Expand All @@ -189,7 +176,7 @@ private static void handleException(Throwable ex, String msg, Path attachFile, b
}

if (!platformShutdown) {
var latch = showErrorReporter(ex, attachFile, terminal);
var latch = showErrorReporter(ex, terminal);
if (terminal) {
try {
latch.await();
Expand Down Expand Up @@ -218,96 +205,4 @@ private static void handleException(Throwable ex, String msg, Path attachFile, b
System.exit(1);
}
}

public static void reportError(Throwable t, boolean diag, Path attachFile, boolean terminal) {
if (diag) {
AtomicReference<SentryId> id = new AtomicReference<>();
Sentry.withScope(scope -> {
LogManager.getInstance().getLogFile().ifPresent(l -> {
scope.addAttachment(new Attachment(l.toString()));
});
scope.setTag("diagnoticsData", "true");
Sentry.setExtra("terminal", String.valueOf(terminal));
id.set(Sentry.captureException(t));
});
if (attachFile != null) {
addAttachment(id.get(), attachFile);
}
} else {
Sentry.withScope(scope -> {
scope.setTag("diagnoticsData", "false");
Sentry.setExtra("terminal", String.valueOf(terminal));
Sentry.captureException(t);
});
}
}

private static void addAttachment(SentryId id, Path attachFile) {
if (!Files.exists(attachFile)) {
return;
}

try {
var bytes = Files.readAllBytes(attachFile);
var out = new ByteArrayOutputStream();
var zipName = "pdxu-report-" + new Random().nextInt(Integer.MAX_VALUE) + ".zip";
try (var zipOut = new ZipOutputStream(out)) {
zipOut.putNextEntry(new ZipEntry(attachFile.getFileName().toString()));
zipOut.write(bytes);
}

for (var part : splitInPartsIfNeeded(zipName, out.toByteArray())) {
Sentry.withScope(scope -> {
scope.setTag("id", id.toString());
scope.addAttachment(new Attachment(part.toString()));
Sentry.captureMessage("Attachment");
});
}
} catch (IOException e) {
e.printStackTrace();
}
}

private static List<Path> splitInPartsIfNeeded(String prefix, byte[] bytes) throws IOException {
var length = 9_000_000;
if (bytes.length <= length) {
Files.write(FileUtils.getTempDirectory().toPath().resolve(prefix), bytes);
return List.of(FileUtils.getTempDirectory().toPath().resolve(prefix));
}

List<Path> files = new ArrayList<>();
for (int i = 0; i < Math.ceil((double) bytes.length / length); i++) {
var file = FileUtils.getTempDirectory().toPath().resolve(prefix + ".part" + i);
try (var out = Files.newOutputStream(file)) {
out.write(bytes, i * length, Math.min(length, bytes.length - (i * length)));
}
files.add(file);
}
return files;
}

public static void reportIssue(Path attachFile) {
Runnable run = () -> {
var r = GuiErrorReporter.showIssueDialog();
r.ifPresent(msg -> {
AtomicReference<SentryId> id = new AtomicReference<>();
Sentry.withScope(scope -> {
LogManager.getInstance().getLogFile().ifPresent(l -> {
scope.addAttachment(new Attachment(l.toString()));
});

id.set(Sentry.captureMessage("User Issue Report"));
Sentry.captureUserFeedback(new UserFeedback(id.get(), null, null, msg));
});
if (attachFile != null) {
addAttachment(id.get(), attachFile);
}
});
};
if (Platform.isFxApplicationThread()) {
run.run();
} else {
Platform.runLater(run);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,17 +180,15 @@ public void check() {
hoi4.getValue() == null && stellaris.getValue() == null &&
ck2.getValue() == null && vic2.getValue() == null && vic3.getValue() == null;
if (hasNoValidInstallation) {
var res = GuiErrorReporter.showSimpleErrorMessage("""
GuiErrorReporter.showSimpleErrorMessage("""
Welcome to the Pdx-Unlimiter!
The automatic game detection did not detect any supported Paradox game.
To get started, you can set the installation directories of games manually in the settings menu.
Note that you can't do anything useful with the Pdx-Unlimiter until at least one installation is set.
""");
if (res) {
Platform.runLater(GuiSettings::showSettings);
}
Platform.runLater(GuiSettings::showSettings);
}

// Disable irony if needed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,6 @@ protected boolean isValid(GameDist newValue) {
return false;
} catch (Exception e) {
showInstallErrorMessage(e.getClass().getSimpleName() + ": " + e.getMessage());
ErrorHandler.reportError(e, false, null, false);
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,6 @@ private static MenuBar createMenuBar() {
});
help.getItems().add(guide);

MenuItem is = new MenuItem(PdxuI18n.get("REPORT_ISSUE"));
is.setOnAction((a) -> {
ErrorHandler.reportIssue(null);
});
help.getItems().add(is);

MenuItem discord = new MenuItem(PdxuI18n.get("DISCORD"));
discord.setOnAction((a) -> {
Hyperlinks.open(Hyperlinks.DISCORD);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.crschnick.pdxu.app.gui;

import com.crschnick.pdxu.app.core.ErrorHandler;
import com.crschnick.pdxu.app.core.PdxuInstallation;
import com.crschnick.pdxu.app.core.SavegameManagerState;
import com.crschnick.pdxu.app.core.TaskExecutor;
Expand All @@ -10,7 +9,10 @@
import com.crschnick.pdxu.app.installation.GameInstallation;
import com.crschnick.pdxu.app.lang.LanguageManager;
import com.crschnick.pdxu.app.lang.PdxuI18n;
import com.crschnick.pdxu.app.savegame.*;
import com.crschnick.pdxu.app.savegame.SavegameActions;
import com.crschnick.pdxu.app.savegame.SavegameBranches;
import com.crschnick.pdxu.app.savegame.SavegameContext;
import com.crschnick.pdxu.app.savegame.SavegameEntry;
import com.crschnick.pdxu.app.util.SupportedOs;
import com.crschnick.pdxu.app.util.integration.ConverterSupport;
import com.crschnick.pdxu.app.util.integration.Eu4SeHelper;
Expand Down Expand Up @@ -162,17 +164,6 @@ private static <T, I extends SavegameInfo<T>> HBox createButtonBar(SavegameEntry
GuiTooltips.install(export, PdxuI18n.get("EXPORT_SAVEGAME", SavegameContext.getContext(e).getGame().getTranslatedFullName()));
staticButtons.getChildren().add(export);
}
{
Button report = new JFXButton(null, new FontIcon());
report.setGraphic(new FontIcon());
report.setOnMouseClicked((m) -> {
ErrorHandler.reportIssue(SavegameContext.getContext(e).getStorage().getSavegameFile(e));
});
report.getStyleClass().add("report-button");
report.setAccessibleText("Report");
GuiTooltips.install(report, PdxuI18n.get("REPORT_SAVEGAME_ISSUE"));
staticButtons.getChildren().add(report);
}
{
Button copy = new JFXButton(null, new FontIcon());
copy.setGraphic(new FontIcon());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public static void addStylesheets(Scene scene) {
.map(p -> p.toUri().toString())
.forEach(s -> scene.getStylesheets().add(s));
} catch (IOException e) {
ErrorHandler.handleException(e, "Pdx-Unlimiter installation files could not be found at " + PdxuInstallation.getInstance().getResourceDir() + ". Were they deleted somehow?", null);
ErrorHandler.handleException(e, "Pdx-Unlimiter installation files could not be found at " + PdxuInstallation.getInstance().getResourceDir() + ". Were they deleted somehow?");
}
}

Expand Down
Loading

0 comments on commit 8b24eb1

Please sign in to comment.