From 2f0bc19e7c6a9456644da20e493c4cbd5870b642 Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Mon, 14 Oct 2024 15:48:54 +0200 Subject: [PATCH] Update/Fix engine init exception handling + errors --- .../de/staropensource/engine/base/Engine.java | 25 ++++++++++++------- .../logging/PlainLoggingAdapter.java | 2 +- .../engine/base/logging/InitLogger.java | 2 +- .../staropensource/engine/testapp/Main.java | 15 ++++++++--- 4 files changed, 29 insertions(+), 15 deletions(-) diff --git a/base/src/main/java/de/staropensource/engine/base/Engine.java b/base/src/main/java/de/staropensource/engine/base/Engine.java index 98c992dd..3ab8eade 100644 --- a/base/src/main/java/de/staropensource/engine/base/Engine.java +++ b/base/src/main/java/de/staropensource/engine/base/Engine.java @@ -186,12 +186,8 @@ public final class Engine extends SubsystemClass { * @since v1-alpha6 */ private Engine() throws IllegalStateException { - if (instance == null) - instance = this; - else - return; - long initTime = Miscellaneous.measureExecutionTime(() -> { + instance = this; state = EngineState.EARLY_STARTUP; new EngineConfiguration(); @@ -217,7 +213,7 @@ public final class Engine extends SubsystemClass { try { initializeSubsystems(); } catch (Exception exception) { - logger.crash("Subsystem dependency resolution failed", exception); + logger.error("Subsystem dependency resolution failed"); } } }); @@ -232,11 +228,22 @@ public final class Engine extends SubsystemClass { * Engine, if it isn't already. * * @throws IllegalStateException when running in an incompatible environment + * @throws RuntimeException on engine initialization failure * @since v1-alpha6 */ - public static void initialize() throws IllegalStateException { - if (instance == null) - instance = new Engine(); + public static void initialize() throws IllegalStateException, RuntimeException { + try { + if (instance == null) + new Engine(); + } catch (IllegalStateException exception) { + throw exception; + } catch (Exception exception) { + logger.error("Engine initialization failed"); + logger.error(Miscellaneous.getStackTraceHeader(exception)); + for (String line : Miscellaneous.getStackTraceAsString(exception, true).split("\n")) + logger.error(line); + throw new RuntimeException("Engine initialization failed", exception); + } } /** diff --git a/base/src/main/java/de/staropensource/engine/base/implementation/logging/PlainLoggingAdapter.java b/base/src/main/java/de/staropensource/engine/base/implementation/logging/PlainLoggingAdapter.java index 1d954876..7a5f6c8c 100644 --- a/base/src/main/java/de/staropensource/engine/base/implementation/logging/PlainLoggingAdapter.java +++ b/base/src/main/java/de/staropensource/engine/base/implementation/logging/PlainLoggingAdapter.java @@ -59,7 +59,7 @@ public class PlainLoggingAdapter implements LoggingAdapter { public void print(@NotNull LogLevel level, @NotNull Class issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message, @NotNull String format) { format = new EmptyShortcodeConverter(format, true).getClean(); if (level == LogLevel.ERROR || level == LogLevel.CRASH) - if (EngineConfiguration.getInstance().isLoggerForceStandardOutput()) + if (EngineConfiguration.getInstance() != null && EngineConfiguration.getInstance().isLoggerForceStandardOutput()) System.out.println(format); else System.err.println(format); diff --git a/base/src/main/java/de/staropensource/engine/base/logging/InitLogger.java b/base/src/main/java/de/staropensource/engine/base/logging/InitLogger.java index 5f4edd4c..f3ac9738 100644 --- a/base/src/main/java/de/staropensource/engine/base/logging/InitLogger.java +++ b/base/src/main/java/de/staropensource/engine/base/logging/InitLogger.java @@ -58,7 +58,7 @@ public final class InitLogger { */ private static synchronized void log(@NotNull LogLevel level, @NotNull Class issuerClass, @NotNull String issuerOrigin, @Nullable String issuerMetadata, @NotNull String message) { // Dismiss if level is not allowed - if (level.compareTo(EngineConfiguration.getInstance().getLoggerLevel()) < 0) + if ((EngineConfiguration.getInstance() != null && level.compareTo(EngineConfiguration.getInstance().getLoggerLevel()) < 0) || level.compareTo(LogLevel.INFORMATIONAL) < 0) return; // Invoke LoggingAdapter#prePlaceholder diff --git a/testapp/src/main/java/de/staropensource/engine/testapp/Main.java b/testapp/src/main/java/de/staropensource/engine/testapp/Main.java index eaf5a85d..55bea013 100644 --- a/testapp/src/main/java/de/staropensource/engine/testapp/Main.java +++ b/testapp/src/main/java/de/staropensource/engine/testapp/Main.java @@ -32,7 +32,6 @@ import de.staropensource.engine.windowing.implementable.Window; import de.staropensource.engine.windowing.type.input.Key; import de.staropensource.engine.windowing.type.input.KeyState; import lombok.Getter; -import lombok.SneakyThrows; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -100,7 +99,6 @@ public final class Main { * @see #main(String[]) * @since v1-alpha0 */ - @SneakyThrows public void run() { try { // Specify subsystems to load @@ -112,7 +110,11 @@ public final class Main { ); // Initialize sos!engine - Engine.initialize(); + try { + Engine.initialize(); + } catch (Exception exception) { + return; + } // Register events EventHelper.registerEvent(InputEvent.class, new EventListenerCode() { @@ -166,7 +168,12 @@ public final class Main { logger.crash(message.toString()); } catch (Exception exception) { - logger.crash("The main thread threw an exception", exception); + System.err.println("Caught throwable in main thread:"); + System.err.println(Miscellaneous.getStackTraceHeader(exception)); + System.err.println(Miscellaneous.getStackTraceAsString(exception, true)); + + // Halt JVM + Runtime.getRuntime().halt(255); } }