From d3a01c2c66484b0616089718d842087a984ed548 Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Sun, 3 Nov 2024 18:40:19 +0100 Subject: [PATCH] Allow throwing all exceptions during engine init Due to some Java limitation we are required to throw a RuntimeException with our original exception contained within and afterwards unwrap it so we can throw any exception we like. That is dumb. --- .../de/staropensource/engine/base/Engine.java | 68 ++++++++++--------- 1 file changed, 37 insertions(+), 31 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 f0d73c6e..7de413c5 100644 --- a/base/src/main/java/de/staropensource/engine/base/Engine.java +++ b/base/src/main/java/de/staropensource/engine/base/Engine.java @@ -185,36 +185,40 @@ public final class Engine extends SubsystemClass { * @throws IllegalStateException when running in an incompatible environment * @since v1-alpha6 */ - private Engine() throws IllegalStateException { + private Engine() throws RuntimeException { long initTime = Miscellaneous.measureExecutionTime(() -> { - instance = this; - state = EngineState.EARLY_STARTUP; + try { + instance = this; + state = EngineState.EARLY_STARTUP; - new EngineConfiguration(); - EngineConfiguration.getInstance().loadConfiguration(); + new EngineConfiguration(); + EngineConfiguration.getInstance().loadConfiguration(); - LOGGER.info("Initializing engine"); - initializeClasses(); // Initialize classes - if (checkEnvironment()) // Check environment - throw new IllegalStateException("Running in an incompatible environment"); - ensureEnvironment(); // Prepare the environment and ensure safety - populateCrashContent(); // Populate crash content - cacheEvents(); // Cache event listeners - startThreads(); // Start threads + LOGGER.info("Initializing engine"); + initializeClasses(); // Initialize classes + if (checkEnvironment()) // Check environment + throw new IllegalStateException("Running in an incompatible environment"); + ensureEnvironment(); // Prepare the environment and ensure safety + populateCrashContent(); // Populate crash content + cacheEvents(); // Cache event listeners + startThreads(); // Start threads - LOGGER.verb("Completing early initialization stage"); - state = EngineState.STARTUP; + LOGGER.verb("Completing early initialization stage"); + state = EngineState.STARTUP; - // Perform automatic subsystem initialization - if (EngineConfiguration.getInstance().isInitialPerformSubsystemInitialization()) { - collectSubsystems(); // Collect subsystems + // Perform automatic subsystem initialization + if (EngineConfiguration.getInstance().isInitialPerformSubsystemInitialization()) { + collectSubsystems(); // Collect subsystems - // Initialize subsystems - try { - initializeSubsystems(); - } catch (Exception exception) { - LOGGER.error("Subsystem dependency resolution failed"); + // Initialize subsystems + try { + initializeSubsystems(); + } catch (Exception exception) { + LOGGER.error("Subsystem dependency resolution failed"); + } } + } catch (Exception exception) { + throw new RuntimeException(exception); } }); @@ -235,14 +239,16 @@ public final class Engine extends SubsystemClass { 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); + } catch (RuntimeException exception) { + if (exception.getCause() instanceof IllegalStateException) + throw (IllegalStateException) exception.getCause(); + else { + LOGGER.error("Engine initialization failed"); + LOGGER.error(Miscellaneous.getStackTraceHeader(exception.getCause())); + for (String line : Miscellaneous.getStackTraceAsString(exception.getCause(), true).split("\n")) + LOGGER.error(line); + throw new RuntimeException("Engine initialization failed", exception.getCause()); + } } }