Skip to content

Commit e360e4a

Browse files
committed
Polish 'Add a FailureAnalyzer for ConfigDataNotFound'
See gh-23633
1 parent be7d697 commit e360e4a

File tree

5 files changed

+181
-53
lines changed

5 files changed

+181
-53
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataLocationNotFoundExceptionFailureAnalyzer.java

Lines changed: 0 additions & 21 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.context.config;
18+
19+
import org.springframework.boot.diagnostics.AbstractFailureAnalyzer;
20+
import org.springframework.boot.diagnostics.FailureAnalysis;
21+
import org.springframework.boot.origin.Origin;
22+
23+
/**
24+
* An implementation of {@link AbstractFailureAnalyzer} to analyze failures caused by
25+
* {@link ConfigDataNotFoundException}.
26+
*
27+
* @author Michal Mlak
28+
* @author Phillip Webb
29+
*/
30+
class ConfigDataNotFoundFailureAnalyzer extends AbstractFailureAnalyzer<ConfigDataNotFoundException> {
31+
32+
@Override
33+
protected FailureAnalysis analyze(Throwable rootFailure, ConfigDataNotFoundException cause) {
34+
ConfigDataLocation location = getLocation(cause);
35+
Origin origin = Origin.from(location);
36+
StringBuilder message = new StringBuilder(
37+
String.format("Config data %s does not exist", cause.getReferenceDescription()));
38+
StringBuilder action = new StringBuilder("Check that the value ");
39+
if (location != null) {
40+
action.append(String.format("'%s' ", location));
41+
}
42+
if (origin != null) {
43+
action.append(String.format("at %s ", origin));
44+
}
45+
action.append("is correct");
46+
if (location != null && !location.isOptional()) {
47+
action.append(String.format(", or prefix it with '%s'", ConfigDataLocation.OPTIONAL_PREFIX));
48+
}
49+
return new FailureAnalysis(message.toString(), action.toString(), cause);
50+
}
51+
52+
private ConfigDataLocation getLocation(ConfigDataNotFoundException cause) {
53+
if (cause instanceof ConfigDataLocationNotFoundException) {
54+
return ((ConfigDataLocationNotFoundException) cause).getLocation();
55+
}
56+
if (cause instanceof ConfigDataResourceNotFoundException) {
57+
return ((ConfigDataResourceNotFoundException) cause).getLocation();
58+
}
59+
return null;
60+
}
61+
62+
}

spring-boot-project/spring-boot/src/main/resources/META-INF/spring.factories

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ org.springframework.boot.reactor.DebugAgentEnvironmentPostProcessor
5858

5959
# Failure Analyzers
6060
org.springframework.boot.diagnostics.FailureAnalyzer=\
61+
org.springframework.boot.context.config.ConfigDataNotFoundFailureAnalyzer,\
6162
org.springframework.boot.context.properties.IncompatibleConfigurationFailureAnalyzer,\
6263
org.springframework.boot.context.properties.NotConstructorBoundInjectionFailureAnalyzer,\
6364
org.springframework.boot.diagnostics.analyzer.BeanCurrentlyInCreationFailureAnalyzer,\
@@ -74,8 +75,7 @@ org.springframework.boot.diagnostics.analyzer.ValidationExceptionFailureAnalyzer
7475
org.springframework.boot.diagnostics.analyzer.InvalidConfigurationPropertyNameFailureAnalyzer,\
7576
org.springframework.boot.diagnostics.analyzer.InvalidConfigurationPropertyValueFailureAnalyzer,\
7677
org.springframework.boot.diagnostics.analyzer.PatternParseFailureAnalyzer,\
77-
org.springframework.boot.liquibase.LiquibaseChangelogMissingFailureAnalyzer,\
78-
org.springframework.boot.context.config.ConfigDataLocationNotFoundExceptionFailureAnalyzer
78+
org.springframework.boot.liquibase.LiquibaseChangelogMissingFailureAnalyzer
7979

8080
# Failure Analysis Reporters
8181
org.springframework.boot.diagnostics.FailureAnalysisReporter=\

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataLocationNotFoundExceptionFailureAnalyzerTests.java

Lines changed: 0 additions & 30 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.context.config;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.boot.diagnostics.FailureAnalysis;
22+
import org.springframework.boot.origin.Origin;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
26+
/**
27+
* Tests for {@link ConfigDataNotFoundFailureAnalyzer}
28+
*
29+
* @author Michal Mlak
30+
* @author Phillip Webb
31+
*/
32+
class ConfigDataNotFoundFailureAnalyzerTests {
33+
34+
private final ConfigDataNotFoundFailureAnalyzer analyzer = new ConfigDataNotFoundFailureAnalyzer();
35+
36+
@Test
37+
void analyzeWhenConfigDataLocationNotFoundException() {
38+
ConfigDataLocation location = ConfigDataLocation.of("test");
39+
ConfigDataLocationNotFoundException exception = new ConfigDataLocationNotFoundException(location);
40+
FailureAnalysis result = this.analyzer.analyze(exception);
41+
assertThat(result.getDescription()).isEqualTo("Config data location 'test' does not exist");
42+
assertThat(result.getAction())
43+
.isEqualTo("Check that the value 'test' is correct, or prefix it with 'optional:'");
44+
}
45+
46+
@Test
47+
void analyzeWhenOptionalConfigDataLocationNotFoundException() {
48+
ConfigDataLocation location = ConfigDataLocation.of("optional:test");
49+
ConfigDataLocationNotFoundException exception = new ConfigDataLocationNotFoundException(location);
50+
FailureAnalysis result = this.analyzer.analyze(exception);
51+
assertThat(result.getDescription()).isEqualTo("Config data location 'optional:test' does not exist");
52+
assertThat(result.getAction()).isEqualTo("Check that the value 'optional:test' is correct");
53+
}
54+
55+
@Test
56+
void analyzeWhenConfigDataLocationWithOriginNotFoundException() {
57+
ConfigDataLocation location = ConfigDataLocation.of("test").withOrigin(new TestOrigin("origin"));
58+
ConfigDataLocationNotFoundException exception = new ConfigDataLocationNotFoundException(location);
59+
FailureAnalysis result = this.analyzer.analyze(exception);
60+
assertThat(result.getDescription()).isEqualTo("Config data location 'test' does not exist");
61+
assertThat(result.getAction())
62+
.isEqualTo("Check that the value 'test' at origin is correct, or prefix it with 'optional:'");
63+
}
64+
65+
@Test
66+
void analyzeWhenConfigDataResourceNotFoundException() {
67+
ConfigDataResource resource = new TestConfigDataResource("myresource");
68+
ConfigDataResourceNotFoundException exception = new ConfigDataResourceNotFoundException(resource);
69+
FailureAnalysis result = this.analyzer.analyze(exception);
70+
assertThat(result.getDescription()).isEqualTo("Config data resource 'myresource' does not exist");
71+
assertThat(result.getAction()).isEqualTo("Check that the value is correct");
72+
}
73+
74+
@Test
75+
void analyzeWhenConfigDataResourceWithLocationNotFoundException() {
76+
ConfigDataLocation location = ConfigDataLocation.of("test");
77+
ConfigDataResource resource = new TestConfigDataResource("myresource");
78+
ConfigDataResourceNotFoundException exception = new ConfigDataResourceNotFoundException(resource)
79+
.withLocation(location);
80+
FailureAnalysis result = this.analyzer.analyze(exception);
81+
assertThat(result.getDescription())
82+
.isEqualTo("Config data resource 'myresource' via location 'test' does not exist");
83+
assertThat(result.getAction())
84+
.isEqualTo("Check that the value 'test' is correct, or prefix it with 'optional:'");
85+
}
86+
87+
static class TestOrigin implements Origin {
88+
89+
private final String string;
90+
91+
TestOrigin(String string) {
92+
this.string = string;
93+
}
94+
95+
@Override
96+
public String toString() {
97+
return this.string;
98+
}
99+
100+
}
101+
102+
static class TestConfigDataResource extends ConfigDataResource {
103+
104+
private final String string;
105+
106+
TestConfigDataResource(String string) {
107+
this.string = string;
108+
}
109+
110+
@Override
111+
public String toString() {
112+
return this.string;
113+
}
114+
115+
}
116+
117+
}

0 commit comments

Comments
 (0)