Skip to content

Commit 0baccfd

Browse files
committed
use throwStableConfigMappingException helper function
1 parent bcebbcb commit 0baccfd

File tree

4 files changed

+35
-45
lines changed

4 files changed

+35
-45
lines changed

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

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,14 @@ public Rule(List<Selector> selectors, Map<String, Object> configuration) {
2828
public static Rule from(Map<?, ?> map) {
2929
Object selectorsObj = map.get("selectors");
3030
if (selectorsObj == null) {
31-
throw new StableConfigMappingException(
32-
"Missing 'selectors' in rule: " + StableConfigMappingException.safeToString(map));
31+
StableConfigMappingException.throwStableConfigMappingException(
32+
"Missing 'selectors' in rule", map);
3333
}
3434

3535
if (!(selectorsObj instanceof List)) {
36-
throw new StableConfigMappingException(
37-
"'selectors' must be a list, but got: "
38-
+ selectorsObj.getClass().getSimpleName()
39-
+ ", value: "
40-
+ StableConfigMappingException.safeToString(selectorsObj));
36+
StableConfigMappingException.throwStableConfigMappingException(
37+
"'selectors' must be a list, but got: " + selectorsObj.getClass().getSimpleName(),
38+
selectorsObj);
4139
}
4240

4341
List<Selector> selectors =
@@ -50,25 +48,23 @@ public static Rule from(Map<?, ?> map) {
5048
if (s instanceof Map) {
5149
return Selector.from((Map<?, ?>) s);
5250
}
53-
throw new StableConfigMappingException(
51+
StableConfigMappingException.throwStableConfigMappingException(
5452
"Each selector must be a map, but got: "
55-
+ s.getClass().getSimpleName()
56-
+ ", value: "
57-
+ StableConfigMappingException.safeToString(s));
53+
+ s.getClass().getSimpleName(),
54+
s);
55+
return null;
5856
})
5957
.collect(toList()));
6058

6159
Object configObj = map.get("configuration");
6260
if (configObj == null) {
63-
throw new StableConfigMappingException(
64-
"Missing 'configuration' in rule: " + StableConfigMappingException.safeToString(map));
61+
StableConfigMappingException.throwStableConfigMappingException(
62+
"Missing 'configuration' in rule", map);
6563
}
6664
if (!(configObj instanceof Map)) {
67-
throw new StableConfigMappingException(
68-
"'configuration' must be a map, but got: "
69-
+ configObj.getClass().getSimpleName()
70-
+ ", value: "
71-
+ StableConfigMappingException.safeToString(configObj));
65+
StableConfigMappingException.throwStableConfigMappingException(
66+
"'configuration' must be a map, but got: " + configObj.getClass().getSimpleName(),
67+
configObj);
7268
}
7369
Map<String, Object> configuration = (Map<String, Object>) configObj;
7470

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

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,12 @@ public Selector(String origin, String key, List<String> matches, String operator
2020
public static Selector from(Map<?, ?> map) {
2121
Object originObj = map.get("origin");
2222
if (originObj == null) {
23-
throw new StableConfigMappingException(
24-
"Missing 'origin' in selector: " + StableConfigMappingException.safeToString(map));
23+
StableConfigMappingException.throwStableConfigMappingException(
24+
"Missing 'origin' in selector", map);
2525
}
2626
if (!(originObj instanceof String)) {
27-
throw new StableConfigMappingException(
28-
"'origin' must be a string, but got: "
29-
+ originObj.getClass().getSimpleName()
30-
+ ", value: "
31-
+ StableConfigMappingException.safeToString(originObj));
27+
StableConfigMappingException.throwStableConfigMappingException(
28+
"'origin' must be a string, but got: " + originObj.getClass().getSimpleName(), originObj);
3229
}
3330
String origin = (String) originObj;
3431

@@ -37,27 +34,23 @@ public static Selector from(Map<?, ?> map) {
3734

3835
Object matchesObj = map.get("matches");
3936
if (matchesObj != null && !(matchesObj instanceof List)) {
40-
throw new StableConfigMappingException(
41-
"'matches' must be a list, but got: "
42-
+ matchesObj.getClass().getSimpleName()
43-
+ ", value: "
44-
+ StableConfigMappingException.safeToString(matchesObj));
37+
StableConfigMappingException.throwStableConfigMappingException(
38+
"'matches' must be a list, but got: " + matchesObj.getClass().getSimpleName(),
39+
matchesObj);
4540
}
4641
List<String> rawMatches = (List<String>) matchesObj;
4742
List<String> matches =
4843
rawMatches != null ? Collections.unmodifiableList(rawMatches) : Collections.emptyList();
4944

5045
Object operatorObj = map.get("operator");
5146
if (operatorObj == null) {
52-
throw new StableConfigMappingException(
53-
"Missing 'operator' in selector: " + StableConfigMappingException.safeToString(map));
47+
StableConfigMappingException.throwStableConfigMappingException(
48+
"Missing 'operator' in selector", map);
5449
}
5550
if (!(operatorObj instanceof String)) {
56-
throw new StableConfigMappingException(
57-
"'operator' must be a string, but got: "
58-
+ operatorObj.getClass().getSimpleName()
59-
+ ", value: "
60-
+ StableConfigMappingException.safeToString(operatorObj));
51+
StableConfigMappingException.throwStableConfigMappingException(
52+
"'operator' must be a string, but got: " + operatorObj.getClass().getSimpleName(),
53+
operatorObj);
6154
}
6255
String operator = (String) operatorObj;
6356

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,9 @@ private List<Rule> parseRules(Map<?, ?> map) {
5151
if (ruleObj instanceof Map) {
5252
rules.add(Rule.from((Map<?, ?>) ruleObj));
5353
} else {
54-
// Log or throw with a detailed message
55-
throw new StableConfigMappingException(
56-
"Rule must be a map, but got: "
57-
+ (ruleObj == null ? "null" : ruleObj.getClass().getSimpleName())
58-
+ ", value: "
59-
+ StableConfigMappingException.safeToString(ruleObj));
54+
StableConfigMappingException.throwStableConfigMappingException(
55+
"Rule must be a map, but got: " + ruleObj.getClass().getSimpleName(), ruleObj);
56+
return emptyList();
6057
}
6158
}
6259
return unmodifiableList(rules);
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
package datadog.trace.bootstrap.config.provider.stableconfig;
22

33
public class StableConfigMappingException extends RuntimeException {
4-
private static final int maxLen = 10;
4+
private static final int maxLen = 100;
55

66
public StableConfigMappingException(String message) {
77
super(message);
88
}
99

10-
public static String safeToString(Object value) {
10+
static String safeToString(Object value) {
1111
if (value == null) return "null";
1212
String str = value.toString();
1313
if (str.length() > maxLen) {
1414
return str.substring(0, maxLen) + "...(truncated)";
1515
}
1616
return str;
1717
}
18+
19+
public static void throwStableConfigMappingException(String message, Object value) {
20+
throw new StableConfigMappingException(message + " " + safeToString(value));
21+
}
1822
}

0 commit comments

Comments
 (0)