Improve error handling in cloud connector.

This commit is contained in:
Scott Frederick
2017-06-02 11:32:47 -05:00
parent 3a7e1b548d
commit 68e045adf2
2 changed files with 28 additions and 6 deletions

View File

@@ -45,9 +45,8 @@ public class CredHubInterpolationServiceDataPostProcessor implements ServiceData
credHubOperations = new CredHubConfiguration().credHubTemplate();
}
catch (Exception e) {
logger.log(Level.INFO, "CredHubOperations cannot be initialized, " +
"disabling processing of service data: "
+ e.getMessage());
logger.log(Level.WARNING, "CredHubOperations cannot be initialized, " +
"disabling processing of service data", e);
}
}
@@ -75,10 +74,15 @@ public class CredHubInterpolationServiceDataPostProcessor implements ServiceData
return serviceData;
}
VcapServicesData interpolatedData = credHubOperations
.interpolateServiceData(connectorsToCredHub(serviceData));
try {
VcapServicesData interpolatedData = credHubOperations
.interpolateServiceData(connectorsToCredHub(serviceData));
return credHubToConnectors(interpolatedData);
return credHubToConnectors(interpolatedData);
} catch (Exception e) {
logger.log(Level.WARNING, "Error interpolating service data from CredHub.", e);
return serviceData;
}
}
/**

View File

@@ -31,9 +31,12 @@ import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.cloud.cloudfoundry.CloudFoundryRawServiceData;
import org.springframework.credhub.core.CredHubException;
import org.springframework.credhub.core.CredHubOperations;
import org.springframework.credhub.support.VcapServicesData;
import org.springframework.http.HttpStatus;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.verifyZeroInteractions;
@@ -59,6 +62,20 @@ public class CredHubInterpolationServiceDataPostProcessorTests {
assertThat(actual, matchesContent(interpolatedServiceData));
}
@Test
public void processServiceDataWithCredHubError() {
CloudFoundryRawServiceData rawServiceData = buildRawServiceData();
when(credHubOperations.interpolateServiceData(argThat(matchesContent(rawServiceData))))
.thenThrow(new CredHubException(HttpStatus.UNAUTHORIZED));
CredHubInterpolationServiceDataPostProcessor processor =
new CredHubInterpolationServiceDataPostProcessor(credHubOperations);
CloudFoundryRawServiceData actual = processor.process(rawServiceData);
assertThat(actual, equalTo(rawServiceData));
}
@Test
public void processServiceDataWithInitializationError() {
CredHubInterpolationServiceDataPostProcessor processor =
@@ -81,6 +98,7 @@ public class CredHubInterpolationServiceDataPostProcessorTests {
private Matcher<CloudFoundryRawServiceData> matchesContent(final VcapServicesData expected) {
return new BaseMatcher<CloudFoundryRawServiceData>() {
@Override
@SuppressWarnings("unchecked")
public boolean matches(Object actual) {
return mapsAreEquivalent((Map<String, ?>) actual, expected);
}