From 723fd4812a7e4449ef58043d916091ed4a0bc9aa Mon Sep 17 00:00:00 2001 From: Brian Stansberry Date: Thu, 17 Jul 2014 14:51:35 -0500 Subject: [PATCH] [DMR-11] Add useful exception messages for illegal conversions --- src/main/java/org/jboss/dmr/ModelValue.java | 69 +++++++++++-------- .../java/org/jboss/dmr/StringModelValue.java | 7 +- .../java/org/jboss/dmr/ValueExpression.java | 8 ++- 3 files changed, 51 insertions(+), 33 deletions(-) diff --git a/src/main/java/org/jboss/dmr/ModelValue.java b/src/main/java/org/jboss/dmr/ModelValue.java index f68e9d1..6cbb25c 100644 --- a/src/main/java/org/jboss/dmr/ModelValue.java +++ b/src/main/java/org/jboss/dmr/ModelValue.java @@ -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 asPropertyList() { - throw new IllegalArgumentException(); + throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("List", 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 getKeys() { - throw new IllegalArgumentException(); + throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("OBJECT", ModelType.OBJECT)); } List asList() { - throw new IllegalArgumentException(); + throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("List", ModelType.LIST)); } ModelType asType() { - throw new IllegalArgumentException(); + throw new IllegalArgumentException(getNonConversionMessageWithSuggestion("ModelType", ModelType.OBJECT)); } ModelValue protect() { @@ -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. */ @@ -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. @@ -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 @@ -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. */ @@ -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. */ @@ -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. */ @@ -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; + } } diff --git a/src/main/java/org/jboss/dmr/StringModelValue.java b/src/main/java/org/jboss/dmr/StringModelValue.java index 7bd8dba..09b1709 100644 --- a/src/main/java/org/jboss/dmr/StringModelValue.java +++ b/src/main/java/org/jboss/dmr/StringModelValue.java @@ -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"); } } @@ -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 */ @@ -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 */ diff --git a/src/main/java/org/jboss/dmr/ValueExpression.java b/src/main/java/org/jboss/dmr/ValueExpression.java index 936f6f2..e6b4e6c 100644 --- a/src/main/java/org/jboss/dmr/ValueExpression.java +++ b/src/main/java/org/jboss/dmr/ValueExpression.java @@ -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"); } } @@ -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"); } }