Skip to content

Commit

Permalink
[DMR-11] Add useful exception messages for illegal conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
bstansberry committed Dec 10, 2017
1 parent ca3f4a5 commit 4e6dae2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 25 deletions.
57 changes: 35 additions & 22 deletions src/main/java/org/jboss/dmr/ModelValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,101 +50,101 @@ ModelType getType() {
}

long asLong() {
throw new IllegalArgumentException();
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("long", ModelType.LONG));
}

long asLong(final long defVal) {
throw new IllegalArgumentException();
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("long", ModelType.LONG));
}

int asInt() {
throw new IllegalArgumentException();
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("int", ModelType.INT));
}

int asInt(final int defVal) {
throw new IllegalArgumentException();
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("int", ModelType.INT));
}

boolean asBoolean() {
throw new IllegalArgumentException();
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("boolean", ModelType.BOOLEAN));
}

boolean asBoolean(final boolean defVal) {
throw new IllegalArgumentException();
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("boolean", ModelType.BOOLEAN));
}

double asDouble() {
throw new IllegalArgumentException();
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("double", ModelType.DOUBLE));
}

double asDouble(final double defVal) {
throw new IllegalArgumentException();
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("double", ModelType.DOUBLE));
}

byte[] asBytes() {
throw new IllegalArgumentException();
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("byte[]", ModelType.BYTES));
}

BigDecimal asBigDecimal() {
throw new IllegalArgumentException();
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("BigDecimal", ModelType.BIG_DECIMAL));
}

BigInteger asBigInteger() {
throw new IllegalArgumentException();
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("BigInteger", ModelType.BIG_INTEGER));
}

abstract String asString();

Property asProperty() {
throw new IllegalArgumentException();
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("Property", ModelType.PROPERTY));
}

List<Property> asPropertyList() {
throw new IllegalArgumentException();
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("List<Property>", ModelType.OBJECT));
}

ValueExpression asExpression() {
throw new IllegalArgumentException();
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("ValueExpression", ModelType.EXPRESSION));
}

ModelNode asObject() {
throw new IllegalArgumentException();
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("OBJECT", ModelType.OBJECT));
}

ModelNode getChild(final String name) {
throw new IllegalArgumentException();
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("OBJECT", ModelType.OBJECT));
}

ModelNode removeChild(final String name) {
throw new IllegalArgumentException();
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("OBJECT", ModelType.OBJECT));
}

ModelNode removeChild(final int index) {
throw new IllegalArgumentException();
}

ModelNode getChild(final int index) {
throw new IllegalArgumentException();
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("OBJECT", ModelType.OBJECT));
}

ModelNode addChild() {
throw new IllegalArgumentException();
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("OBJECT", ModelType.OBJECT));
}

ModelNode insertChild(int index) {
throw new IllegalArgumentException();
}

Set<String> getKeys() {
throw new IllegalArgumentException();
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("OBJECT", ModelType.OBJECT));
}

List<ModelNode> asList() {
throw new IllegalArgumentException();
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("List<ModelNode>", ModelType.LIST));
}

ModelType asType() {
throw new IllegalArgumentException();
throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("ModelType", ModelType.OBJECT));
}

ModelValue protect() {
Expand Down Expand Up @@ -390,4 +390,17 @@ ModelNode requireChild(final String name) throws NoSuchElementException {
ModelNode requireChild(final int index) throws NoSuchElementException {
throw new NoSuchElementException("No child exists at index [" + index + "]");
}

private String getNonConversionMessageWithSuggestion(String desiredConversion, ModelType suggestedType) {
String suggestion = suggestedType.name();
if (suggestedType == ModelType.LIST) {
suggestion = '[' + suggestion + ']';
} else if (suggestedType == ModelType.OBJECT) {
suggestion = '{' + suggestion + '}';
}

// TODO i18n
return "Cannot convert a node of type " + getType() + " to " + desiredConversion +
". Recommended type for this conversion is " + suggestion;
}
}
3 changes: 2 additions & 1 deletion src/main/java/org/jboss/dmr/StringModelValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ boolean asBoolean() {
} else if (value.equalsIgnoreCase("false")) {
return false;
} else {
throw new IllegalArgumentException();
// TODO i18n
throw new IllegalArgumentException("Cannot convert the string '" + value + "' to a boolean. Must be either 'true' or 'false', ignoring case");
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/main/java/org/jboss/dmr/ValueExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ public boolean resolveBoolean() {
} else if (value.equalsIgnoreCase("false")) {
return false;
} else {
throw new IllegalArgumentException();
// TODO i18n
throw new IllegalArgumentException("Cannot convert the string '" + value + "' resolved from expression '" +
expressionString + "' to a boolean. Resolved string must be either 'true' or 'false', ignoring case");
}
}

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

Expand Down

0 comments on commit 4e6dae2

Please sign in to comment.