Skip to content

Commit

Permalink
Merge pull request #37543 from MaciejDromin/#36376
Browse files Browse the repository at this point in the history
Fix smallrye ConfigValidationException not being handled properly at Quarkus startup
  • Loading branch information
yrodiere authored Dec 21, 2023
2 parents cf914a6 + 017e57b commit f2f971f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
import io.quarkus.deployment.builditem.GeneratedClassBuildItem;
import io.quarkus.deployment.builditem.MainBytecodeRecorderBuildItem;
import io.quarkus.deployment.builditem.RuntimeConfigSetupCompleteBuildItem;
import io.quarkus.gizmo.CatchBlockCreator;
import io.quarkus.gizmo.ClassCreator;
import io.quarkus.gizmo.ClassOutput;
import io.quarkus.gizmo.MethodCreator;
import io.quarkus.gizmo.TryBlock;
import io.quarkus.runtime.StartupContext;
import io.quarkus.runtime.StartupTask;
import io.quarkus.runtime.configuration.ConfigurationException;

public class RuntimeConfigSetupBuildStep {
private static final String RUNTIME_CONFIG_STARTUP_TASK_CLASS_NAME = "io.quarkus.deployment.steps.RuntimeConfigSetup";
Expand All @@ -37,11 +40,17 @@ void setupRuntimeConfig(
.interfaces(StartupTask.class).build()) {

try (MethodCreator method = clazz.getMethodCreator("deploy", void.class, StartupContext.class)) {
method.invokeVirtualMethod(ofMethod(StartupContext.class, "setCurrentBuildStepName", void.class, String.class),
TryBlock tryBlock = method.tryBlock();
tryBlock.invokeVirtualMethod(
ofMethod(StartupContext.class, "setCurrentBuildStepName", void.class, String.class),
method.getMethodParam(0), method.load("RuntimeConfigSetupBuildStep.setupRuntimeConfig"));

method.invokeStaticMethod(C_CREATE_RUN_TIME_CONFIG);
method.returnValue(null);
tryBlock.invokeStaticMethod(C_CREATE_RUN_TIME_CONFIG);
tryBlock.returnValue(null);

CatchBlockCreator cb = tryBlock.addCatch(RuntimeException.class);
cb.throwException(ConfigurationException.class, "Failed to read configuration properties",
cb.getCaughtException());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,9 @@ 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
&& !StringUtil.isNullOrEmpty(rootCause.getMessage())) {
System.err.println(rootCause.getMessage());
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 f2f971f

Please sign in to comment.