From 3d635f148870bd5f01be5e97c948bfe697d8e5d7 Mon Sep 17 00:00:00 2001 From: Nicolas Homble Date: Mon, 18 Nov 2019 10:26:43 -0500 Subject: [PATCH] Feature/gh 1283 exception (#1458) * wrap errors in NativeEnvironmentRepository --- .../environment/EnvironmentController.java | 6 ++++ .../environment/EnvironmentException.java | 33 ++++++++++++++++++ ...FailedToConstructEnvironmentException.java | 34 +++++++++++++++++++ .../NativeEnvironmentRepository.java | 16 ++++++--- .../EnvironmentControllerTests.java | 16 +++++++++ .../NativeEnvironmentRepositoryTests.java | 14 ++++++++ .../resources/test/bad-syntax/application.yml | 2 ++ 7 files changed, 116 insertions(+), 5 deletions(-) create mode 100644 spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/EnvironmentException.java create mode 100644 spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/FailedToConstructEnvironmentException.java create mode 100644 spring-cloud-config-server/src/test/resources/test/bad-syntax/application.yml diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/EnvironmentController.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/EnvironmentController.java index 55874e5f..32ae18e4 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/EnvironmentController.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/EnvironmentController.java @@ -275,6 +275,12 @@ public class EnvironmentController { response.sendError(HttpStatus.BAD_REQUEST.value()); } + @ExceptionHandler(EnvironmentException.class) + public void environmentException(HttpServletResponse response, EnvironmentException e) + throws IOException { + response.sendError(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage()); + } + private void validateProfiles(String profiles) { if (profiles.contains("-")) { throw new IllegalArgumentException( diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/EnvironmentException.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/EnvironmentException.java new file mode 100644 index 00000000..2a7049c3 --- /dev/null +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/EnvironmentException.java @@ -0,0 +1,33 @@ +/* + * Copyright 2015-2019 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 + * + * https://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.environment; + +/** + * @author Nicolas Homble + */ +@SuppressWarnings("serial") +public class EnvironmentException extends RuntimeException { + + public EnvironmentException(String string) { + super(string); + } + + public EnvironmentException(String message, Throwable cause) { + super(message, cause); + } + +} diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/FailedToConstructEnvironmentException.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/FailedToConstructEnvironmentException.java new file mode 100644 index 00000000..67212fae --- /dev/null +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/FailedToConstructEnvironmentException.java @@ -0,0 +1,34 @@ +/* + * Copyright 2015-2019 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 + * + * https://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.environment; + +/** + * @author Nicolas Homble + * + */ +@SuppressWarnings("serial") +public class FailedToConstructEnvironmentException extends EnvironmentException { + + public FailedToConstructEnvironmentException(String string) { + super(string); + } + + public FailedToConstructEnvironmentException(String message, Throwable cause) { + super(message, cause); + } + +} diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepository.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepository.java index 88fa136a..121a6a42 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepository.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepository.java @@ -36,6 +36,7 @@ import org.springframework.boot.context.config.ConfigFileApplicationListener; import org.springframework.cloud.config.environment.Environment; import org.springframework.cloud.config.environment.PropertySource; import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.core.NestedExceptionUtils; import org.springframework.core.Ordered; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.MapPropertySource; @@ -145,14 +146,19 @@ public class NativeEnvironmentRepository // log levels in the caller) builder.application() .setListeners(Arrays.asList(new ConfigFileApplicationListener())); - ConfigurableApplicationContext context = builder.run(args); - environment.getPropertySources().remove("profiles"); - try { + + try (ConfigurableApplicationContext context = builder.run(args)) { + environment.getPropertySources().remove("profiles"); return clean(new PassthruEnvironmentRepository(environment).findOne(config, profile, label, includeOrigin)); } - finally { - context.close(); + catch (Exception e) { + String msg = String.format( + "Could not construct context for config=%s profile=%s label=%s includeOrigin=%b", + config, profile, label, includeOrigin); + String completeMessage = NestedExceptionUtils.buildMessage(msg, + NestedExceptionUtils.getMostSpecificCause(e)); + throw new FailedToConstructEnvironmentException(completeMessage, e); } } diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/EnvironmentControllerTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/EnvironmentControllerTests.java index 0f48bf42..2b4e4a6c 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/EnvironmentControllerTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/EnvironmentControllerTests.java @@ -31,11 +31,14 @@ import org.springframework.cloud.config.environment.Environment; import org.springframework.cloud.config.environment.PropertySource; import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -612,4 +615,17 @@ public class EnvironmentControllerTests { } + @Test + public void handleEnvironmentException() throws Exception { + when(repository.findOne(eq("exception"), eq("bad-syntax.ext"), any(), eq(false))) + .thenThrow(new FailedToConstructEnvironmentException("Cannot construct", + new RuntimeException("underlier"))); + MockMvc mvc = MockMvcBuilders.standaloneSetup(controller) + .setControllerAdvice(controller).build(); + MvcResult result = mvc + .perform(MockMvcRequestBuilders.get("/exception/bad-syntax.ext")) + .andExpect(MockMvcResultMatchers.status().is(500)).andReturn(); + assertThat(result.getResponse().getErrorMessage()).isEqualTo("Cannot construct"); + } + } diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepositoryTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepositoryTests.java index 01ae5cc6..21782eae 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepositoryTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepositoryTests.java @@ -26,6 +26,7 @@ import org.springframework.cloud.config.server.environment.SearchPathLocator.Loc import org.springframework.context.ConfigurableApplicationContext; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * @author Dave Syer @@ -219,4 +220,17 @@ public class NativeEnvironmentRepositoryTests { .get(0).getSource().get("foo")).isEqualTo("test_bar"); } + @Test + public void duplicateYamlKeys() { + this.repository.setSearchLocations("classpath:/test/bad-syntax"); + NativeEnvironmentRepository repo = this.repository; + assertThatExceptionOfType(FailedToConstructEnvironmentException.class) + .isThrownBy(() -> repo.findOne("foo", "master", "default")).withMessage( + "Could not construct context for config=foo profile=master label=default includeOrigin=false; nested exception is while constructing a mapping\n" + + " in 'reader', line 1, column 1:\n" + " key: value\n" + + " ^\n" + "found duplicate key key\n" + + " in 'reader', line 2, column 1:\n" + " key: value\n" + + " ^\n"); + } + } diff --git a/spring-cloud-config-server/src/test/resources/test/bad-syntax/application.yml b/spring-cloud-config-server/src/test/resources/test/bad-syntax/application.yml new file mode 100644 index 00000000..b19e2752 --- /dev/null +++ b/spring-cloud-config-server/src/test/resources/test/bad-syntax/application.yml @@ -0,0 +1,2 @@ +key: value +key: value \ No newline at end of file