Skip to content

Commit

Permalink
Feature/gh 1283 exception (#1458)
Browse files Browse the repository at this point in the history
* wrap errors in NativeEnvironmentRepository
  • Loading branch information
nhomble authored and ryanjbaxter committed Nov 18, 2019
1 parent cfe60db commit 3d635f1
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,12 @@ public void illegalArgument(HttpServletResponse response) throws IOException {
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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
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;
Expand Down Expand Up @@ -145,14 +146,19 @@ public Environment findOne(String config, String profile, String label,
// 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@
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;

Expand Down Expand Up @@ -612,4 +615,17 @@ public void mappingForLabelledJsonPropertiesWithHyphen() throws Exception {

}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.springframework.context.ConfigurableApplicationContext;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

/**
* @author Dave Syer
Expand Down Expand Up @@ -219,4 +220,17 @@ public void testDefaultLabel() {
.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");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
key: value
key: value

0 comments on commit 3d635f1

Please sign in to comment.