diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/diagnostics/GitUriFailureAnalyzer.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/diagnostics/GitUriFailureAnalyzer.java new file mode 100644 index 00000000..1fc04280 --- /dev/null +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/diagnostics/GitUriFailureAnalyzer.java @@ -0,0 +1,25 @@ +package org.springframework.cloud.config.server.diagnostics; + +import org.springframework.boot.diagnostics.AbstractFailureAnalyzer; +import org.springframework.boot.diagnostics.FailureAnalysis; +import org.springframework.cloud.config.server.environment.JGitEnvironmentRepository; + +/** + * @author Ryan Baxter + */ +public class GitUriFailureAnalyzer extends AbstractFailureAnalyzer { + + public static final String DESCRIPTION = "Invalid config server configuration."; + public static final String ACTION = "If you are using the git profile, you need to set a Git URI in your " + + "configuration. If you are using a native profile and have spring.cloud.config.server.bootstrap=true, " + + "you need to use a composite configuration."; + + @Override + protected FailureAnalysis analyze(Throwable rootFailure, IllegalStateException cause) { + if(JGitEnvironmentRepository.MESSAGE.equalsIgnoreCase(cause.getMessage())) { + return new FailureAnalysis(DESCRIPTION, ACTION, cause); + } + return null; + } + +} diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JGitEnvironmentRepository.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JGitEnvironmentRepository.java index 214fe5fb..d7afb1d1 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JGitEnvironmentRepository.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/JGitEnvironmentRepository.java @@ -80,6 +80,8 @@ import static org.eclipse.jgit.transport.ReceiveCommand.Type.DELETE; public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository implements EnvironmentRepository, SearchPathLocator, InitializingBean { + public static final String MESSAGE = "You need to configure a uri for the git repository."; + private static final String FILE_URI_PREFIX = "file:"; private static final String LOCAL_BRANCH_REF_PREFIX = "refs/remotes/origin/"; @@ -243,7 +245,7 @@ public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository @Override public void afterPropertiesSet() throws Exception { Assert.state(getUri() != null, - "You need to configure a uri for the git repository"); + MESSAGE); initialize(); if (this.cloneOnStart) { initClonedRepository(); diff --git a/spring-cloud-config-server/src/main/resources/META-INF/spring.factories b/spring-cloud-config-server/src/main/resources/META-INF/spring.factories index f145f03c..4c3a8b2e 100644 --- a/spring-cloud-config-server/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-config-server/src/main/resources/META-INF/spring.factories @@ -11,3 +11,6 @@ org.springframework.cloud.config.server.bootstrap.ConfigServerBootstrapApplicati org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.cloud.config.server.config.ConfigServerAutoConfiguration,\ org.springframework.cloud.config.server.config.EncryptionAutoConfiguration + +org.springframework.boot.diagnostics.FailureAnalyzer=\ +org.springframework.cloud.config.server.diagnostics.GitUriFailureAnalyzer diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/NativeBootstrapFailureAnalyzerTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/NativeBootstrapFailureAnalyzerTests.java new file mode 100644 index 00000000..5666ed40 --- /dev/null +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/NativeBootstrapFailureAnalyzerTests.java @@ -0,0 +1,34 @@ +package org.springframework.cloud.config.server; + +import org.junit.Rule; +import org.junit.Test; +import org.springframework.boot.WebApplicationType; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.test.rule.OutputCapture; +import org.springframework.cloud.config.server.diagnostics.GitUriFailureAnalyzer; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.fail; + +/** + * @author Ryan Baxter + */ +public class NativeBootstrapFailureAnalyzerTests { + + @Rule + public OutputCapture outputCapture = new OutputCapture(); + + @Test + public void contextLoads(){ + try { + new SpringApplicationBuilder(ConfigServerApplication.class) + .web(WebApplicationType.SERVLET).properties("spring.cloud.bootstrap.name:enable-nativebootstrap").profiles("test","native").run(); + fail("Application started successfully"); + } + catch (Exception ex) { + assertThat(this.outputCapture.toString()) + .contains(GitUriFailureAnalyzer.ACTION); + assertThat(this.outputCapture.toString()).contains(GitUriFailureAnalyzer.DESCRIPTION); + } + } +} diff --git a/spring-cloud-config-server/src/test/resources/enable-nativebootstrap.yml b/spring-cloud-config-server/src/test/resources/enable-nativebootstrap.yml new file mode 100644 index 00000000..66226479 --- /dev/null +++ b/spring-cloud-config-server/src/test/resources/enable-nativebootstrap.yml @@ -0,0 +1,5 @@ +spring: + cloud: + config: + server: + bootstrap: true