Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix smallrye ConfigValidationException not being handled properly at Quarkus startup #37543

Merged
merged 1 commit into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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");
}

}