Send 404 if Git label not found

See gh-153
This commit is contained in:
Dave Syer
2015-05-19 11:47:28 +01:00
parent e94b4039a6
commit c685931308
6 changed files with 73 additions and 5 deletions

View File

@@ -186,6 +186,11 @@ public class EnvironmentController {
return result == null ? new LinkedHashMap<String, Object>() : 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());

View File

@@ -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);

View File

@@ -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);

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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)