Fixes gh-1940. 404 should be returned instead of 500 when missing label for resource.

This commit is contained in:
Oleksandr Karpenko
2021-08-09 18:23:08 +03:00
parent 1f71006382
commit 0d6191a41e
2 changed files with 17 additions and 0 deletions

View File

@@ -22,12 +22,15 @@ import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.server.encryption.ResourceEncryptor;
import org.springframework.cloud.config.server.environment.EnvironmentRepository;
import org.springframework.cloud.config.server.environment.RepositoryException;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
@@ -220,4 +223,9 @@ public class ResourceController {
public void notFound(NoSuchResourceException e) {
}
@ExceptionHandler(RepositoryException.class)
public void noSuchLabel(HttpServletResponse response) throws IOException {
response.sendError(HttpStatus.NOT_FOUND.value());
}
}

View File

@@ -34,6 +34,7 @@ import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.server.encryption.ResourceEncryptor;
import org.springframework.cloud.config.server.environment.EnvironmentController;
import org.springframework.cloud.config.server.environment.EnvironmentRepository;
import org.springframework.cloud.config.server.environment.NoSuchLabelException;
import org.springframework.cloud.config.server.resource.ResourceControllerIntegrationTests.ControllerConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
@@ -159,6 +160,14 @@ public class ResourceControllerIntegrationTests {
verify(this.resources).findOne("foo", "default", null, "foo.txt");
}
@Test
public void resourceWithMissingLabel() throws Exception {
when(this.resources.findOne("foo", "default", "missing", "foo.txt"))
.thenThrow(new NoSuchLabelException("Planned"));
this.mvc.perform(MockMvcRequestBuilders.get("/foo/default/missing/foo.txt"))
.andExpect(MockMvcResultMatchers.status().isNotFound());
}
@SpringBootConfiguration
@EnableAutoConfiguration
public static class ControllerConfiguration {