Skip to content

Commit

Permalink
Changes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
MaciejDromin committed Dec 6, 2023
1 parent e7f4062 commit 48ebc43
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import io.quarkus.gizmo.ClassTransformer;
import io.quarkus.gizmo.FieldCreator;
import io.quarkus.gizmo.FieldDescriptor;
import io.quarkus.gizmo.IfThenElse;
import io.quarkus.gizmo.MethodCreator;
import io.quarkus.gizmo.MethodDescriptor;
import io.quarkus.gizmo.ResultHandle;
Expand Down Expand Up @@ -307,26 +308,36 @@ void build(List<StaticBytecodeRecorderBuildItem> staticInitTasks,
CatchBlockCreator preventFurtherStepsBlock = tryBlock.addCatch(PreventFurtherStepsException.class);
preventFurtherStepsBlock.invokeVirtualMethod(ofMethod(StartupContext.class, "close", void.class), startupContext);

CatchBlockCreator catchConfigurationException = tryBlock.addCatch(ConfigurationException.class);

// Handle Configuration Exception thrown at startup
handleThrownException(launchMode, catchConfigurationException);

catchConfigurationException.invokeStaticMethod(ofMethod(ApplicationStateNotification.class,
"notifyStartupFailed", void.class, Throwable.class),
catchConfigurationException.getCaughtException());
catchConfigurationException.invokeVirtualMethod(ofMethod(StartupContext.class,
"close", void.class), startupContext);
catchConfigurationException.throwException(catchConfigurationException.getCaughtException());
mv.returnValue(null);

cb = tryBlock.addCatch(Throwable.class);

// Handle any exception that may be thrown at startup
handleThrownException(launchMode, cb);
// an exception was thrown before logging was actually setup, we simply dump everything to the console
// we don't do this for dev mode, as on startup failure dev mode sets up its own logging
if (launchMode.getLaunchMode() != LaunchMode.DEVELOPMENT) {
ResultHandle delayedHandler = cb
.readStaticField(
FieldDescriptor.of(InitialConfigurator.class, "DELAYED_HANDLER", QuarkusDelayedHandler.class));
ResultHandle isActivated = cb.invokeVirtualMethod(
ofMethod(QuarkusDelayedHandler.class, "isActivated", boolean.class),
delayedHandler);
BytecodeCreator isActivatedFalse = cb.ifNonZero(isActivated).falseBranch();
ResultHandle handlersArray = isActivatedFalse.newArray(Handler.class, 1);
isActivatedFalse.writeArrayValue(handlersArray, 0,
isActivatedFalse.newInstance(ofConstructor(ConsoleHandler.class)));
isActivatedFalse.invokeVirtualMethod(
ofMethod(QuarkusDelayedHandler.class, "setHandlers", Handler[].class, Handler[].class),
delayedHandler, handlersArray);
isActivatedFalse.breakScope();
}

cb.invokeVirtualMethod(ofMethod(StartupContext.class, "close", void.class), startupContext);
cb.throwException(RuntimeException.class, "Failed to start quarkus", cb.getCaughtException());

ResultHandle caughtException = cb.getCaughtException();

IfThenElse ifThenElse = cb.ifThenElse(cb.instanceOf(caughtException, ConfigurationException.class));
BytecodeCreator ifThen = ifThenElse.then();
ifThen.throwException(caughtException);
BytecodeCreator elseThen = ifThenElse.elseThen();
elseThen.throwException(RuntimeException.class, "Failed to start quarkus", cb.getCaughtException());
mv.returnValue(null);

// Application class: stop method
Expand All @@ -346,27 +357,6 @@ void build(List<StaticBytecodeRecorderBuildItem> staticInitTasks,
file.close();
}

// an exception was thrown before logging was actually setup, we simply dump everything to the console
// we don't do this for dev mode, as on startup failure dev mode sets up its own logging
private void handleThrownException(LaunchModeBuildItem launchMode, CatchBlockCreator cb) {
if (launchMode.getLaunchMode() != LaunchMode.DEVELOPMENT) {
ResultHandle delayedHandler = cb
.readStaticField(
FieldDescriptor.of(InitialConfigurator.class, "DELAYED_HANDLER", QuarkusDelayedHandler.class));
ResultHandle isActivated = cb.invokeVirtualMethod(
ofMethod(QuarkusDelayedHandler.class, "isActivated", boolean.class),
delayedHandler);
BytecodeCreator isActivatedFalse = cb.ifNonZero(isActivated).falseBranch();
ResultHandle handlersArray = isActivatedFalse.newArray(Handler.class, 1);
isActivatedFalse.writeArrayValue(handlersArray, 0,
isActivatedFalse.newInstance(ofConstructor(ConsoleHandler.class)));
isActivatedFalse.invokeVirtualMethod(
ofMethod(QuarkusDelayedHandler.class, "setHandlers", Handler[].class, Handler[].class),
delayedHandler, handlersArray);
isActivatedFalse.breakScope();
}
}

@BuildStep
public MainClassBuildItem mainClassBuildStep(BuildProducer<GeneratedClassBuildItem> generatedClass,
BuildProducer<BytecodeTransformerBuildItem> transformedClass,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void setupRuntimeConfig(
tryBlock.invokeStaticMethod(C_CREATE_RUN_TIME_CONFIG);
tryBlock.returnValue(null);

CatchBlockCreator cb = tryBlock.addCatch(Throwable.class);
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 @@ -190,8 +190,8 @@ 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) {
applicationLogger.errorv(e, rootCause.getMessage());
ensureConsoleLogsDrained();
System.err.println(rootCause.getMessage());
e.printStackTrace();
} else if (rootCause instanceof PreventFurtherStepsException
&& !StringUtil.isNullOrEmpty(rootCause.getMessage())) {
System.err.println(rootCause.getMessage());
Expand Down

0 comments on commit 48ebc43

Please sign in to comment.