From ef14f4abc56c92f8c842aac713bba5951e115c71 Mon Sep 17 00:00:00 2001 From: Jakub Klebek <54934272+klebekj@users.noreply.github.com> Date: Mon, 16 Sep 2019 16:57:56 +0200 Subject: [PATCH] Add error body with info when none of labels found (#1461) --- .../ConfigServicePropertySourceLocator.java | 4 ++-- ...nfigServicePropertySourceLocatorTests.java | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java index 32a8e2a8..d750d936 100644 --- a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java +++ b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java @@ -130,6 +130,7 @@ public class ConfigServicePropertySourceLocator implements PropertySourceLocator return composite; } } + errorBody = String.format("None of labels %s found", Arrays.toString(labels)); } catch (HttpServerErrorException e) { error = e; @@ -147,8 +148,7 @@ public class ConfigServicePropertySourceLocator implements PropertySourceLocator + (errorBody == null ? "" : ": " + errorBody), error); } - logger.warn("Could not locate PropertySource: " + (errorBody == null - ? error == null ? "label not found" : error.getMessage() : errorBody)); + logger.warn("Could not locate PropertySource: " + (error != null ? error.getMessage() : errorBody)); return null; } diff --git a/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocatorTests.java b/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocatorTests.java index fc11db75..a615d887 100644 --- a/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocatorTests.java +++ b/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocatorTests.java @@ -111,12 +111,29 @@ public class ConfigServicePropertySourceLocatorTests { @Test public void sunnyDayWithNoSuchLabel() { mockRequestResponseWithLabel( - new ResponseEntity((Void) null, HttpStatus.NOT_FOUND), + new ResponseEntity<>((Void) null, HttpStatus.NOT_FOUND), "nosuchlabel"); this.locator.setRestTemplate(this.restTemplate); assertThat(this.locator.locate(this.environment)).isNull(); } + @Test + public void sunnyDayWithNoSuchLabelAndFailFast() { + ConfigClientProperties defaults = new ConfigClientProperties(this.environment); + defaults.setFailFast(true); + this.locator = new ConfigServicePropertySourceLocator(defaults); + mockRequestResponseWithLabel( + new ResponseEntity<>((Void) null, HttpStatus.NOT_FOUND), + "release(_)v1.0.0"); + this.locator.setRestTemplate(this.restTemplate); + TestPropertyValues.of("spring.cloud.config.label:release/v1.0.1") + .applyTo(this.environment); + this.expected.expect(IsInstanceOf.instanceOf(IllegalStateException.class)); + this.expected.expectMessage( + "Could not locate PropertySource and the fail fast property is set, failing: None of labels [release/v1.0.1] found"); + this.locator.locate(this.environment); + } + @Test public void failsQuietly() { mockRequestResponseWithoutLabel(