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 Jul 21, 2014
1 parent 504fb9d commit 723fd48
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 33 deletions.
69 changes: 41 additions & 28 deletions src/main/java/org/jboss/dmr/ModelValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,93 +47,93 @@ 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 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));
}

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 @@ -277,7 +277,7 @@ public int hashCode() {

/**
* Adds the number of indentations (4 spaces each) specified to the writer's output.
*
*
* @param writer The PrintWriter instance containing the current output.
* @param count The number of indentations to be written.
*/
Expand All @@ -289,7 +289,7 @@ protected static void indent(final PrintWriter writer, final int count) {

/**
* Formats the current value object as part of a DMR string.
*
*
* @param writer A PrintWriter instance containing the generated DMR string representation.
* @param indent The number of tabs to indent the current generated string.
* @param multiLine Flag indicating whether or not the string should begin on a new line.
Expand All @@ -300,7 +300,7 @@ void format(final PrintWriter writer, final int indent, final boolean multiLine)

/**
* Formats the current value object as part of a JSON string.
*
*
* @param writer A PrintWriter instance containing the JSON string.
* @param indent The number of tabs to indent the current generated string.
* @param multiLine Flag that indicates whether or not the string should
Expand All @@ -320,7 +320,7 @@ public String toString() {

/**
* Outputs the DMR representation of this value to the supplied PrintWriter instance.
*
*
* @param writer A PrintWriter instance use to output the DMR string.
* @param compact Flag indicating whether or not to include new lines in the generated string representation.
*/
Expand All @@ -330,7 +330,7 @@ public void writeString(final PrintWriter writer, final boolean compact) {

/**
* Converts this value to a JSON string representation.
*
*
* @param compact Flag indicating whether or not to include new lines in the generated string representation.
* @return The JSON formatted string representation of this value.
*/
Expand All @@ -343,7 +343,7 @@ public String toJSONString(final boolean compact) {

/**
* Outputs this value as a JSON string representation to the supplied PrintWriter instance.
*
*
* @param writer A PrintWriter instance use to output the JSON string.
* @param compact Flag indicating whether or not to include new lines in the generated string representation.
*/
Expand Down Expand Up @@ -372,4 +372,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;
}
}
7 changes: 4 additions & 3 deletions src/main/java/org/jboss/dmr/StringModelValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,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 Expand Up @@ -216,7 +217,7 @@ void formatAsJSON(final PrintWriter writer, final int indent, final boolean mult

/**
* Determine whether this object is equal to another.
*
*
* @param other the other object
* @return {@code true} if they are equal, {@code false} otherwise
*/
Expand All @@ -227,7 +228,7 @@ public boolean equals(final Object other) {

/**
* Determine whether this object is equal to another.
*
*
* @param other the other object
* @return {@code true} if they are equal, {@code false} otherwise
*/
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 723fd48

Please sign in to comment.