Skip to content

Commit 4e6dae2

Browse files
committed
[DMR-11] Add useful exception messages for illegal conversions
1 parent ca3f4a5 commit 4e6dae2

File tree

3 files changed

+43
-25
lines changed

3 files changed

+43
-25
lines changed

src/main/java/org/jboss/dmr/ModelValue.java

+35-22
Original file line numberDiff line numberDiff line change
@@ -50,101 +50,101 @@ ModelType getType() {
5050
}
5151

5252
long asLong() {
53-
throw new IllegalArgumentException();
53+
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("long", ModelType.LONG));
5454
}
5555

5656
long asLong(final long defVal) {
57-
throw new IllegalArgumentException();
57+
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("long", ModelType.LONG));
5858
}
5959

6060
int asInt() {
61-
throw new IllegalArgumentException();
61+
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("int", ModelType.INT));
6262
}
6363

6464
int asInt(final int defVal) {
65-
throw new IllegalArgumentException();
65+
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("int", ModelType.INT));
6666
}
6767

6868
boolean asBoolean() {
69-
throw new IllegalArgumentException();
69+
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("boolean", ModelType.BOOLEAN));
7070
}
7171

7272
boolean asBoolean(final boolean defVal) {
73-
throw new IllegalArgumentException();
73+
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("boolean", ModelType.BOOLEAN));
7474
}
7575

7676
double asDouble() {
77-
throw new IllegalArgumentException();
77+
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("double", ModelType.DOUBLE));
7878
}
7979

8080
double asDouble(final double defVal) {
81-
throw new IllegalArgumentException();
81+
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("double", ModelType.DOUBLE));
8282
}
8383

8484
byte[] asBytes() {
85-
throw new IllegalArgumentException();
85+
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("byte[]", ModelType.BYTES));
8686
}
8787

8888
BigDecimal asBigDecimal() {
89-
throw new IllegalArgumentException();
89+
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("BigDecimal", ModelType.BIG_DECIMAL));
9090
}
9191

9292
BigInteger asBigInteger() {
93-
throw new IllegalArgumentException();
93+
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("BigInteger", ModelType.BIG_INTEGER));
9494
}
9595

9696
abstract String asString();
9797

9898
Property asProperty() {
99-
throw new IllegalArgumentException();
99+
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("Property", ModelType.PROPERTY));
100100
}
101101

102102
List<Property> asPropertyList() {
103-
throw new IllegalArgumentException();
103+
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("List<Property>", ModelType.OBJECT));
104104
}
105105

106106
ValueExpression asExpression() {
107-
throw new IllegalArgumentException();
107+
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("ValueExpression", ModelType.EXPRESSION));
108108
}
109109

110110
ModelNode asObject() {
111-
throw new IllegalArgumentException();
111+
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("OBJECT", ModelType.OBJECT));
112112
}
113113

114114
ModelNode getChild(final String name) {
115-
throw new IllegalArgumentException();
115+
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("OBJECT", ModelType.OBJECT));
116116
}
117117

118118
ModelNode removeChild(final String name) {
119-
throw new IllegalArgumentException();
119+
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("OBJECT", ModelType.OBJECT));
120120
}
121121

122122
ModelNode removeChild(final int index) {
123123
throw new IllegalArgumentException();
124124
}
125125

126126
ModelNode getChild(final int index) {
127-
throw new IllegalArgumentException();
127+
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("OBJECT", ModelType.OBJECT));
128128
}
129129

130130
ModelNode addChild() {
131-
throw new IllegalArgumentException();
131+
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("OBJECT", ModelType.OBJECT));
132132
}
133133

134134
ModelNode insertChild(int index) {
135135
throw new IllegalArgumentException();
136136
}
137137

138138
Set<String> getKeys() {
139-
throw new IllegalArgumentException();
139+
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("OBJECT", ModelType.OBJECT));
140140
}
141141

142142
List<ModelNode> asList() {
143-
throw new IllegalArgumentException();
143+
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("List<ModelNode>", ModelType.LIST));
144144
}
145145

146146
ModelType asType() {
147-
throw new IllegalArgumentException();
147+
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("ModelType", ModelType.OBJECT));
148148
}
149149

150150
ModelValue protect() {
@@ -390,4 +390,17 @@ ModelNode requireChild(final String name) throws NoSuchElementException {
390390
ModelNode requireChild(final int index) throws NoSuchElementException {
391391
throw new NoSuchElementException("No child exists at index [" + index + "]");
392392
}
393+
394+
private String getNonConversionMessageWithSuggestion(String desiredConversion, ModelType suggestedType) {
395+
String suggestion = suggestedType.name();
396+
if (suggestedType == ModelType.LIST) {
397+
suggestion = '[' + suggestion + ']';
398+
} else if (suggestedType == ModelType.OBJECT) {
399+
suggestion = '{' + suggestion + '}';
400+
}
401+
402+
// TODO i18n
403+
return "Cannot convert a node of type " + getType() + " to " + desiredConversion +
404+
". Recommended type for this conversion is " + suggestion;
405+
}
393406
}

src/main/java/org/jboss/dmr/StringModelValue.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ boolean asBoolean() {
154154
} else if (value.equalsIgnoreCase("false")) {
155155
return false;
156156
} else {
157-
throw new IllegalArgumentException();
157+
// TODO i18n
158+
throw new IllegalArgumentException("Cannot convert the string '" + value + "' to a boolean. Must be either 'true' or 'false', ignoring case");
158159
}
159160
}
160161

src/main/java/org/jboss/dmr/ValueExpression.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,9 @@ public boolean resolveBoolean() {
175175
} else if (value.equalsIgnoreCase("false")) {
176176
return false;
177177
} else {
178-
throw new IllegalArgumentException();
178+
// TODO i18n
179+
throw new IllegalArgumentException("Cannot convert the string '" + value + "' resolved from expression '" +
180+
expressionString + "' to a boolean. Resolved string must be either 'true' or 'false', ignoring case");
179181
}
180182
}
181183

@@ -192,7 +194,9 @@ public boolean resolveBoolean(final ValueExpressionResolver resolver) {
192194
} else if (value.equalsIgnoreCase("false")) {
193195
return false;
194196
} else {
195-
throw new IllegalArgumentException();
197+
// TODO i18n
198+
throw new IllegalArgumentException("Cannot convert the string '" + value + "' resolved from expression '" +
199+
expressionString + "' to a boolean. Resolved string must be either 'true' or 'false', ignoring case");
196200
}
197201
}
198202

0 commit comments

Comments
 (0)