diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/EnvironmentController.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/EnvironmentController.java index 278f660b..e90f55b2 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/EnvironmentController.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/EnvironmentController.java @@ -186,6 +186,11 @@ public class EnvironmentController { return result == null ? new LinkedHashMap() : result; } + @ExceptionHandler(NoSuchLabelException.class) + public void noSuchLabel(HttpServletResponse response) throws IOException { + response.sendError(HttpStatus.NOT_FOUND.value()); + } + @ExceptionHandler(IllegalArgumentException.class) public void illegalArgument(HttpServletResponse response) throws IOException { response.sendError(HttpStatus.BAD_REQUEST.value()); diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/JGitEnvironmentRepository.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/JGitEnvironmentRepository.java index 3c9fb7dc..54f16b7b 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/JGitEnvironmentRepository.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/JGitEnvironmentRepository.java @@ -21,6 +21,7 @@ import java.io.IOException; import java.util.List; import com.jcraft.jsch.Session; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.eclipse.jgit.api.CheckoutCommand; @@ -33,13 +34,13 @@ import org.eclipse.jgit.api.ListBranchCommand.ListMode; import org.eclipse.jgit.api.PullCommand; import org.eclipse.jgit.api.TransportCommand; import org.eclipse.jgit.api.errors.GitAPIException; +import org.eclipse.jgit.api.errors.RefNotFoundException; import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.transport.JschConfigSessionFactory; import org.eclipse.jgit.transport.OpenSshConfig.Host; import org.eclipse.jgit.transport.SshSessionFactory; import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; import org.eclipse.jgit.util.FileUtils; - import org.springframework.cloud.config.environment.Environment; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.io.UrlResource; @@ -79,8 +80,11 @@ public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository git = createGitClient(); return loadEnvironment(git, application, profile, label); } + catch (RefNotFoundException e) { + throw new NoSuchLabelException("No such label: " + label); + } catch (GitAPIException e) { - throw new IllegalStateException("Cannot clone repository", e); + throw new IllegalStateException("Cannot clone or checkout repository", e); } catch (Exception e) { throw new IllegalStateException("Cannot load environment", e); diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/NativeEnvironmentRepository.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/NativeEnvironmentRepository.java index efa323fa..e859ccbb 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/NativeEnvironmentRepository.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/NativeEnvironmentRepository.java @@ -94,8 +94,7 @@ public class NativeEnvironmentRepository implements EnvironmentRepository { builder.web(false).showBanner(false); String[] args = getArgs(config, label); // Explicitly set the listeners (to exclude logging listener which would change - // log - // levels in the caller) + // log levels in the caller) builder.application().setListeners( Collections.singletonList(new ConfigFileApplicationListener())); ConfigurableApplicationContext context = builder.run(args); diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/NoSuchLabelException.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/NoSuchLabelException.java new file mode 100644 index 00000000..4ff1aaf3 --- /dev/null +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/NoSuchLabelException.java @@ -0,0 +1,30 @@ +/* + * Copyright 2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.config.server; + +/** + * @author Dave Syer + * + */ +@SuppressWarnings("serial") +public class NoSuchLabelException extends RepositoryException { + + public NoSuchLabelException(String string) { + super(string); + } + +} diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/RepositoryException.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/RepositoryException.java new file mode 100644 index 00000000..a1581327 --- /dev/null +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/RepositoryException.java @@ -0,0 +1,30 @@ +/* + * Copyright 2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.config.server; + +/** + * @author Dave Syer + * + */ +@SuppressWarnings("serial") +public class RepositoryException extends RuntimeException { + + public RepositoryException(String string) { + super(string); + } + +} diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/JGitEnvironmentRepositoryIntegrationTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/JGitEnvironmentRepositoryIntegrationTests.java index 10a316b9..520acaa3 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/JGitEnvironmentRepositoryIntegrationTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/JGitEnvironmentRepositoryIntegrationTests.java @@ -122,7 +122,7 @@ public class JGitEnvironmentRepositoryIntegrationTests { assertEquals("master", repository.getDefaultLabel()); } - @Test(expected = IllegalStateException.class) + @Test(expected = NoSuchLabelException.class) public void invalidLabel() throws IOException { String uri = ConfigServerTestUtils.prepareLocalRepo(); context = new SpringApplicationBuilder(TestConfiguration.class).web(false)