Fix error message if not failing fast

Fixes gh-78
This commit is contained in:
Dave Syer
2015-02-04 11:46:14 +01:00
parent 6d3a60b64c
commit 79d20a0f74
2 changed files with 10 additions and 8 deletions

View File

@@ -62,7 +62,7 @@ public class ConfigServicePropertySourceLocator implements PropertySourceLocator
CompositePropertySource composite = new CompositePropertySource("configService");
RestTemplate restTemplate = this.restTemplate == null ? getSecureRestTemplate(client)
: this.restTemplate;
RuntimeException error = null;
Exception error = null;
String errorBody = null;
try {
Environment result = restTemplate.exchange(
@@ -78,17 +78,18 @@ public class ConfigServicePropertySourceLocator implements PropertySourceLocator
}
catch (HttpServerErrorException e) {
error = e;
if (MediaType.APPLICATION_JSON.includes(e.getResponseHeaders().getContentType())) {
if (MediaType.APPLICATION_JSON.includes(e.getResponseHeaders()
.getContentType())) {
errorBody = e.getResponseBodyAsString();
}
}
catch (Exception e) {
error = new IllegalStateException(
"Could not locate PropertySource. The fail fast property is set, failing",
e);
error = e;
}
if (client != null && client.isFailFast()) {
throw error;
throw new IllegalStateException(
"Could not locate PropertySource and the fail fast property is set, failing",
error);
}
logger.error("Could not locate PropertySource: "
+ (errorBody == null ? error.getMessage() : errorBody));

View File

@@ -6,6 +6,7 @@ import static org.junit.Assert.assertNull;
import java.io.ByteArrayInputStream;
import java.net.URI;
import org.hamcrest.core.IsInstanceOf;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
@@ -75,8 +76,8 @@ public class ConfigServicePropertySourceLocatorTests {
Mockito.when(response.getStatusCode()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR);
Mockito.when(response.getBody()).thenReturn(new ByteArrayInputStream("{}".getBytes()));
locator.setRestTemplate(restTemplate);
expected.expect(HttpServerErrorException.class);
expected.expectMessage("500");
expected.expectCause(IsInstanceOf.<Throwable>instanceOf(HttpServerErrorException.class));
expected.expectMessage("fail fast property is set");
assertNull(locator.locate(environment));
}