Skip to content

Commit 85d11cf

Browse files
committed
Improve exception message: include value of object that caused the failure
1 parent 5b639aa commit 85d11cf

File tree

5 files changed

+74
-44
lines changed

5 files changed

+74
-44
lines changed

internal-api/src/main/java/datadog/trace/bootstrap/config/provider/stableconfig/Rule.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ public static Rule from(Map<?, ?> map) {
3232
}
3333
if (!(selectorsObj instanceof List)) {
3434
throw new StableConfigMappingException(
35-
"'selectors' must be a list, but got: " + selectorsObj.getClass().getSimpleName());
35+
"'selectors' must be a list, but got: "
36+
+ selectorsObj.getClass().getSimpleName()
37+
+ ", value: "
38+
+ StableConfigMappingException.safeToString(selectorsObj));
3639
}
3740
List<Selector> selectors =
3841
unmodifiableList(
@@ -43,7 +46,10 @@ public static Rule from(Map<?, ?> map) {
4346
s -> {
4447
if (!(s instanceof Map)) {
4548
throw new StableConfigMappingException(
46-
"Selector must be a map, but got: " + s.getClass().getSimpleName());
49+
"Each selector must be a map, but got: "
50+
+ s.getClass().getSimpleName()
51+
+ ", value: "
52+
+ StableConfigMappingException.safeToString(s));
4753
}
4854
return Selector.from((Map<?, ?>) s);
4955
})
@@ -55,7 +61,10 @@ public static Rule from(Map<?, ?> map) {
5561
}
5662
if (!(configObj instanceof Map)) {
5763
throw new StableConfigMappingException(
58-
"'configuration' must be a map, but got: " + configObj.getClass().getSimpleName());
64+
"'configuration' must be a map, but got: "
65+
+ configObj.getClass().getSimpleName()
66+
+ ", value: "
67+
+ StableConfigMappingException.safeToString(configObj));
5968
}
6069
Map<String, Object> configuration = (Map<String, Object>) configObj;
6170

internal-api/src/main/java/datadog/trace/bootstrap/config/provider/stableconfig/Selector.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ public static Selector from(Map<?, ?> map) {
2424
}
2525
if (!(originObj instanceof String)) {
2626
throw new StableConfigMappingException(
27-
"'origin' must be a string, but got: " + originObj.getClass().getSimpleName());
27+
"'origin' must be a string, but got: "
28+
+ originObj.getClass().getSimpleName()
29+
+ ", value: "
30+
+ StableConfigMappingException.safeToString(originObj));
2831
}
2932
String origin = (String) originObj;
3033

@@ -34,7 +37,10 @@ public static Selector from(Map<?, ?> map) {
3437
Object matchesObj = map.get("matches");
3538
if (matchesObj != null && !(matchesObj instanceof List)) {
3639
throw new StableConfigMappingException(
37-
"'matches' must be a list, but got: " + matchesObj.getClass().getSimpleName());
40+
"'matches' must be a list, but got: "
41+
+ matchesObj.getClass().getSimpleName()
42+
+ ", value: "
43+
+ StableConfigMappingException.safeToString(matchesObj));
3844
}
3945
List<String> rawMatches = (List<String>) matchesObj;
4046
List<String> matches =
@@ -46,7 +52,10 @@ public static Selector from(Map<?, ?> map) {
4652
}
4753
if (!(operatorObj instanceof String)) {
4854
throw new StableConfigMappingException(
49-
"'operator' must be a string, but got: " + operatorObj.getClass().getSimpleName());
55+
"'operator' must be a string, but got: "
56+
+ operatorObj.getClass().getSimpleName()
57+
+ ", value: "
58+
+ StableConfigMappingException.safeToString(operatorObj));
5059
}
5160
String operator = (String) operatorObj;
5261

internal-api/src/main/java/datadog/trace/bootstrap/config/provider/stableconfig/StableConfig.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ private List<Rule> parseRules(Map<?, ?> map) {
5454
// Log or throw with a detailed message
5555
throw new StableConfigMappingException(
5656
"Rule must be a map, but got: "
57-
+ (ruleObj == null ? "null" : ruleObj.getClass().getSimpleName()));
57+
+ (ruleObj == null ? "null" : ruleObj.getClass().getSimpleName())
58+
+ ", value: "
59+
+ StableConfigMappingException.safeToString(ruleObj));
5860
}
5961
}
6062
return unmodifiableList(rules);

internal-api/src/main/java/datadog/trace/bootstrap/config/provider/stableconfig/StableConfigMappingException.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,13 @@ public StableConfigMappingException(String message) {
88
public StableConfigMappingException(String message, Throwable cause) {
99
super(message, cause);
1010
}
11+
12+
public static String safeToString(Object value) {
13+
if (value == null) return "null";
14+
String str = value.toString();
15+
if (str.length() > 100) {
16+
return str.substring(0, 100) + "...(truncated)";
17+
}
18+
return str;
19+
}
1120
}

internal-api/src/test/groovy/datadog/trace/bootstrap/config/provider/StableConfigSourceTest.groovy

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -135,52 +135,53 @@ class StableConfigSourceTest extends DDSpecification {
135135

136136
where:
137137
yaml | expectedLogSubstring
138-
// Missing 'origin' in selector
139138
'''apm_configuration_rules:
140-
- selectors:
141-
- key: "someKey"
142-
matches: ["someValue"]
143-
operator: equals
144-
configuration:
145-
DD_SERVICE: "test"
146-
''' | "Missing 'origin' in selector"
147-
// Missing 'configuration' in rule
139+
- selectors:
140+
- key: "someKey"
141+
matches: ["someValue"]
142+
operator: equals
143+
configuration:
144+
DD_SERVICE: "test"
145+
''' | "Missing 'origin' in selector"
148146
'''apm_configuration_rules:
149-
- selectors:
150-
- origin: process_arguments
151-
key: "-Dfoo"
152-
matches: ["bar"]
153-
operator: equals
154-
''' | "Missing 'configuration' in rule"
155-
// Missing 'selectors' in rule
147+
- selectors:
148+
- origin: process_arguments
149+
key: "-Dfoo"
150+
matches: ["bar"]
151+
operator: equals
152+
''' | "Missing 'configuration' in rule"
156153
'''apm_configuration_rules:
157154
- configuration:
158155
DD_SERVICE: "test"
159156
''' | "Missing 'selectors' in rule"
160-
// Triggers ClassCastException (selectors should be a list, not a string)
161157
'''apm_configuration_rules:
162-
- selectors: "not-a-list"
163-
configuration:
164-
DD_SERVICE: "test"
165-
''' | "'selectors' must be a list, but got: String"
166-
// configuration present but not a map
158+
- selectors: "not-a-list"
159+
configuration:
160+
DD_SERVICE: "test"
161+
''' | "'selectors' must be a list, but got: String"
162+
'''apm_configuration_rules:
163+
- selectors:
164+
- "not-a-map"
165+
''' | "Each selector must be a map, but got: String"
166+
'''apm_configuration_rules:
167+
- selectors:
168+
- origin: process_arguments
169+
key: "-Dfoo"
170+
matches: ["bar"]
171+
operator: equals
172+
configuration: "not-a-map"
173+
''' | "'configuration' must be a map, but got: String"
167174
'''apm_configuration_rules:
168-
- selectors:
169-
- origin: process_arguments
170-
key: "-Dfoo"
171-
matches: ["bar"]
172-
operator: equals
173-
configuration: "not-a-map"
174-
''' | "'configuration' must be a map, but got: String"
175-
// configuration present but not a map (integer)
175+
- selectors:
176+
- origin: process_arguments
177+
key: "-Dfoo"
178+
matches: ["bar"]
179+
operator: equals
180+
configuration: 12345
181+
''' | "'configuration' must be a map, but got: Integer"
176182
'''apm_configuration_rules:
177-
- selectors:
178-
- origin: process_arguments
179-
key: "-Dfoo"
180-
matches: ["bar"]
181-
operator: equals
182-
configuration: 12345
183-
''' | "'configuration' must be a map, but got: Integer"
183+
- "not-a-map"
184+
''' | "Rule must be a map, but got: String"
184185
}
185186

186187
// Corrupt YAML string variable used for testing, defined outside the 'where' block for readability

0 commit comments

Comments
 (0)