From 0d6191a41edb3454425c66964064a4249e4810ac Mon Sep 17 00:00:00 2001 From: Oleksandr Karpenko Date: Mon, 9 Aug 2021 18:23:08 +0300 Subject: [PATCH] Fixes gh-1940. 404 should be returned instead of 500 when missing label for resource. --- .../cloud/config/server/resource/ResourceController.java | 8 ++++++++ .../resource/ResourceControllerIntegrationTests.java | 9 +++++++++ 2 files changed, 17 insertions(+) diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/resource/ResourceController.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/resource/ResourceController.java index 877fe92b..ca0326e4 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/resource/ResourceController.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/resource/ResourceController.java @@ -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()); + } + } diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/resource/ResourceControllerIntegrationTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/resource/ResourceControllerIntegrationTests.java index 072eab07..1a181051 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/resource/ResourceControllerIntegrationTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/resource/ResourceControllerIntegrationTests.java @@ -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 {