Skip to content

Commit

Permalink
generate support package on application failure
Browse files Browse the repository at this point in the history
  • Loading branch information
jixxed committed Aug 20, 2024
1 parent 75d4bc1 commit 819ca5d
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ public void start(final Stage primaryStage) {
});
}catch (Throwable t){
log.error("Failed to initialize the UI", t);
showAlert(t);
String supportFile = SupportService.createSupportPackage();
showAlert(supportFile, t);
}
});
log.debug("setupFleetCarrierWatcher");
Expand Down Expand Up @@ -167,19 +168,27 @@ public void start(final Stage primaryStage) {
ARService.toggle();
}
}

} catch (final Exception ex) {
showAlert(ex);

String supportFile = "" ;
try {
supportFile = SupportService.createSupportPackage();
}catch (Exception e){
log.error("Failed to create support package", e);
}

showAlert(supportFile, ex);
}
}

private void showAlert(final Throwable t) {

private void showAlert(final String supportFile, final Throwable t) {
final Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setResizable(true);
alert.getDialogPane().setPrefSize(800, 800);
Platform.runLater(() -> alert.setResizable(false));
alert.setTitle("Application Error");
alert.setHeaderText("Please contact the developer with the following information");
alert.setHeaderText("Please contact the developer with the following information and include the following generated support file for reproduction purposes:\n" + supportFile);
final StringWriter stringWriter = new StringWriter();
final PrintWriter printWriter = new PrintWriter(stringWriter);
t.printStackTrace(printWriter);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package nl.jixxed.eliteodysseymaterials.service;

import lombok.extern.slf4j.Slf4j;
import nl.jixxed.eliteodysseymaterials.constants.AppConstants;
import nl.jixxed.eliteodysseymaterials.constants.OsConstants;
import nl.jixxed.eliteodysseymaterials.constants.PreferenceConstants;
import nl.jixxed.eliteodysseymaterials.domain.ApplicationState;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

@Slf4j
public class SupportService {


public static String createSupportPackage() {
Path backupFolder = Path.of(OsConstants.CONFIG_DIRECTORY + "/backup");
Path configFolder = Path.of(OsConstants.CONFIG_DIRECTORY);
Path supportFolder = Path.of(OsConstants.CONFIG_DIRECTORY + "/support");
Path journalFolder = Path.of(PreferencesService.getPreference(PreferenceConstants.JOURNAL_FOLDER, OsConstants.DEFAULT_WATCHED_FOLDER));
try {
log.info("Creating support package.");
return zipFolder(configFolder, journalFolder, supportFolder,backupFolder);
} catch (Exception e) {
log.error("Failed to create support package.", e);
}
return "";
}

private static String zipFolder(Path configFolderPath,Path journalFolderPath, Path zipPath, Path backupFolderPath) throws Exception {
File backupFile = new File(zipPath.toFile().getAbsoluteFile() + "/support." + DateTimeFormatter.ofPattern("yyyy-MM-dd_HH.mm.ss.SSS").format(LocalDateTime.now()) + ".zip");
zipPath.toFile().mkdirs();
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(backupFile))) {
Files.walkFileTree(configFolderPath, new SimpleFileVisitor<>() {
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (!file.startsWith(zipPath) &&!file.startsWith(backupFolderPath) && !isExcluded(file.getFileName().toString())) {
zos.putNextEntry(new ZipEntry(configFolderPath.relativize(file).toString()));
log.debug("support: " + file.getFileName());
Files.copy(file, zos);
zos.closeEntry();
}
return FileVisitResult.CONTINUE;
}

private boolean isExcluded(String fileName) {
return fileName.equals("lock") || fileName.equals("material-report.json") || fileName.endsWith(".tmp") || fileName.equals("capi.json");
}
});

Files.walkFileTree(journalFolderPath, new SimpleFileVisitor<>() {
List<String> files = List.of(
AppConstants.CARGO_FILE,
AppConstants.BACKPACK_FILE,
AppConstants.FCMATERIALS_FILE,
AppConstants.STATUS_FILE,
AppConstants.SHIPLOCKER_FILE,
AppConstants.SHIPYARD_FILE,
AppConstants.OUTFITTING_FILE,
AppConstants.MARKET_FILE,
AppConstants.NAVROUTE_FILE,
AppConstants.MODULESINFO_FILE,
ApplicationState.getInstance().getWatchedFile()
);
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (files.contains(file.getFileName().toString())) {
zos.putNextEntry(new ZipEntry("journal/" + journalFolderPath.relativize(file).toString()));
log.debug("support: " + file.getFileName());
Files.copy(file, zos);
zos.closeEntry();
}
return FileVisitResult.CONTINUE;
}
});
}
return backupFile.getAbsolutePath();
}
}

0 comments on commit 819ca5d

Please sign in to comment.