From 9c9e70294397336666f4179967c3037e213af8df Mon Sep 17 00:00:00 2001 From: Maciej Lisowski Date: Tue, 19 Dec 2023 16:40:52 +0100 Subject: [PATCH] Changes after review, write new tests Signed-off-by: Maciej Lisowski --- .../runtime/ApplicationLifecycleManager.java | 8 ++--- .../quarkus/runtime/util/ExceptionUtil.java | 11 +++++++ .../runtime/util/ExceptionUtilTest.java | 29 +++++++++++++++++++ 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/core/runtime/src/main/java/io/quarkus/runtime/ApplicationLifecycleManager.java b/core/runtime/src/main/java/io/quarkus/runtime/ApplicationLifecycleManager.java index 981b7c8497bdf..4c7ed149e1737 100644 --- a/core/runtime/src/main/java/io/quarkus/runtime/ApplicationLifecycleManager.java +++ b/core/runtime/src/main/java/io/quarkus/runtime/ApplicationLifecycleManager.java @@ -156,11 +156,7 @@ public static void run(Application application, Class'."); } - } else if (rootCause instanceof ConfigurationException) { + } else if (ExceptionUtil.isAnyCauseInstanceOf(e, ConfigurationException.class)) { System.err.println(rootCause.getMessage()); e.printStackTrace(); } else if (rootCause instanceof PreventFurtherStepsException diff --git a/core/runtime/src/main/java/io/quarkus/runtime/util/ExceptionUtil.java b/core/runtime/src/main/java/io/quarkus/runtime/util/ExceptionUtil.java index 99fbf4fdce04c..b27f844b3cf06 100644 --- a/core/runtime/src/main/java/io/quarkus/runtime/util/ExceptionUtil.java +++ b/core/runtime/src/main/java/io/quarkus/runtime/util/ExceptionUtil.java @@ -86,6 +86,17 @@ public static Throwable getRootCause(Throwable exception) { return chain.isEmpty() ? null : chain.get(chain.size() - 1); } + public static boolean isAnyCauseInstanceOf(Throwable exception, Class classToCheck) { + Throwable curr = exception; + do { + if (classToCheck.isInstance(curr)) { + return true; + } + curr = curr.getCause(); + } while (curr != null); + return false; + } + /** * Creates and returns a new {@link Throwable} which has the following characteristics: *
    diff --git a/core/runtime/src/test/java/io/quarkus/runtime/util/ExceptionUtilTest.java b/core/runtime/src/test/java/io/quarkus/runtime/util/ExceptionUtilTest.java index c85696e57a8f0..e432bb0fa281f 100644 --- a/core/runtime/src/test/java/io/quarkus/runtime/util/ExceptionUtilTest.java +++ b/core/runtime/src/test/java/io/quarkus/runtime/util/ExceptionUtilTest.java @@ -13,6 +13,8 @@ import org.junit.jupiter.api.Test; +import io.quarkus.runtime.configuration.ConfigurationException; + /** * */ @@ -59,6 +61,11 @@ public void testGetRootCause() { assertEquals(NullPointerException.class, ExceptionUtil.getRootCause(new NullPointerException()).getClass()); } + @Test + public void testIsAnyCauseInstanceOf() { + assertTrue(ExceptionUtil.isAnyCauseInstanceOf(generateConfigurationException(), ConfigurationException.class)); + } + private Throwable generateException() { try { try { @@ -79,4 +86,26 @@ private Throwable generateException() { } throw new RuntimeException("Should not reach here"); } + + private Throwable generateConfigurationException() { + try { + try { + Integer.parseInt("23.23232"); + } catch (NumberFormatException nfe) { + throw new ConfigurationException("Incorrect param", nfe); + } + } catch (ConfigurationException ce) { + try { + throw new IOException("Request processing failed", ce); + } catch (IOException e) { + try { + throw new IOError(e); + } catch (IOError ie) { + return new RuntimeException("Unexpected exception", ie); + } + } + } + throw new RuntimeException("Should not reach here"); + } + }