Skip to content

Commit

Permalink
Changes after review, write new tests
Browse files Browse the repository at this point in the history
Signed-off-by: Maciej Lisowski <macieejl00@gmail.com>
  • Loading branch information
MaciejDromin authored and yrodiere committed Dec 21, 2023
1 parent fdea595 commit 2657b1b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,7 @@ public static void run(Application application, Class<? extends QuarkusApplicati
}
}
} catch (Exception e) {
// Here if ConfigurationException is one step up on the chain we want to return it
// because it wraps ConfigValidationException from io.smallrye.config
Throwable rootCause = e.getCause() instanceof ConfigurationException
? e.getCause()
: ExceptionUtil.getRootCause(e);
Throwable rootCause = ExceptionUtil.getRootCause(e);
if (exitCodeHandler == null) {
Logger applicationLogger = Logger.getLogger(Application.class);
if (rootCause instanceof QuarkusBindException) {
Expand Down Expand Up @@ -191,7 +187,7 @@ public static void run(Application application, Class<? extends QuarkusApplicati
}
applicationLogger.warn("You can try to kill it with 'kill -9 <pid>'.");
}
} else if (rootCause instanceof ConfigurationException) {
} else if (ExceptionUtil.isAnyCauseInstanceOf(e, ConfigurationException.class)) {
System.err.println(rootCause.getMessage());
e.printStackTrace();
} else if (rootCause instanceof PreventFurtherStepsException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ public static Throwable getRootCause(Throwable exception) {
return chain.isEmpty() ? null : chain.get(chain.size() - 1);
}

public static <T> boolean isAnyCauseInstanceOf(Throwable exception, Class<T> 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:
* <ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

import org.junit.jupiter.api.Test;

import io.quarkus.runtime.configuration.ConfigurationException;

/**
*
*/
Expand Down Expand Up @@ -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 {
Expand All @@ -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");
}

}

0 comments on commit 2657b1b

Please sign in to comment.