Skip to content

Commit

Permalink
Merge branch '3.2.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkinsona committed Apr 19, 2024
2 parents 58073e0 + 2e114b6 commit 3725d70
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.GenericTypeResolver;
import org.springframework.core.NativeDetector;
import org.springframework.core.OrderComparator;
import org.springframework.core.OrderComparator.OrderSourceProvider;
import org.springframework.core.Ordered;
Expand Down Expand Up @@ -848,7 +849,16 @@ private void reportFailure(Collection<SpringBootExceptionReporter> exceptionRepo
// Continue with normal handling of the original failure
}
if (logger.isErrorEnabled()) {
logger.error("Application run failed", failure);
if (NativeDetector.inNativeImage()) {
// Depending on how early the failure was, logging may not work in a
// native image so we output the stack trace directly to System.out
// instead.
System.out.println("Application run failed");
failure.printStackTrace(System.out);
}
else {
logger.error("Application run failed", failure);
}
registerLoggedException(failure);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import org.springframework.boot.context.event.ApplicationStartingEvent;
import org.springframework.boot.context.event.SpringApplicationEvent;
import org.springframework.boot.convert.ApplicationConversionService;
import org.springframework.boot.testsupport.classpath.ForkedClassPath;
import org.springframework.boot.testsupport.system.CapturedOutput;
import org.springframework.boot.testsupport.system.OutputCaptureExtension;
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
Expand Down Expand Up @@ -753,6 +754,53 @@ void failureInReadyEventListenerCloseApplicationContext(CapturedOutput output) {
assertThat(output).contains("Application run failed");
}

@Test
void failureOnTheJvmLogsApplicationRunFailed(CapturedOutput output) {
SpringApplication application = new SpringApplication(ExampleConfig.class);
application.setWebApplicationType(WebApplicationType.NONE);
ExitCodeListener exitCodeListener = new ExitCodeListener();
application.addListeners(exitCodeListener);
@SuppressWarnings("unchecked")
ApplicationListener<SpringApplicationEvent> listener = mock(ApplicationListener.class);
application.addListeners(listener);
ExitStatusException failure = new ExitStatusException();
willThrow(failure).given(listener).onApplicationEvent(isA(ApplicationReadyEvent.class));
assertThatExceptionOfType(RuntimeException.class).isThrownBy(application::run);
then(listener).should().onApplicationEvent(isA(ApplicationReadyEvent.class));
then(listener).should(never()).onApplicationEvent(isA(ApplicationFailedEvent.class));
assertThat(exitCodeListener.getExitCode()).isEqualTo(11);
// Leading space only happens when logging
assertThat(output).contains(" Application run failed").contains("ExitStatusException");
}

@Test
@ForkedClassPath
void failureInANativeImageWritesFailureToSystemOut(CapturedOutput output) {
System.setProperty("org.graalvm.nativeimage.imagecode", "true");
try {
SpringApplication application = new SpringApplication(ExampleConfig.class);
application.setWebApplicationType(WebApplicationType.NONE);
ExitCodeListener exitCodeListener = new ExitCodeListener();
application.addListeners(exitCodeListener);
@SuppressWarnings("unchecked")
ApplicationListener<SpringApplicationEvent> listener = mock(ApplicationListener.class);
application.addListeners(listener);
ExitStatusException failure = new ExitStatusException();
willThrow(failure).given(listener).onApplicationEvent(isA(ApplicationReadyEvent.class));
assertThatExceptionOfType(RuntimeException.class).isThrownBy(application::run);
then(listener).should().onApplicationEvent(isA(ApplicationReadyEvent.class));
then(listener).should(never()).onApplicationEvent(isA(ApplicationFailedEvent.class));
assertThat(exitCodeListener.getExitCode()).isEqualTo(11);
// Leading space only happens when logging
assertThat(output).doesNotContain(" Application run failed")
.contains("Application run failed")
.contains("ExitStatusException");
}
finally {
System.clearProperty("org.graalvm.nativeimage.imagecode");
}
}

@Test
void loadSources() {
Class<?>[] sources = { ExampleConfig.class, TestCommandLineRunner.class };
Expand Down

0 comments on commit 3725d70

Please sign in to comment.