From 4e6dae2f994608817fdc908913a5ebef5933bda9 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 | 57 ++++++++++++------- .../java/org/jboss/dmr/StringModelValue.java | 3 +- .../java/org/jboss/dmr/ValueExpression.java | 8 ++- 3 files changed, 43 insertions(+), 25 deletions(-) diff --git a/src/main/java/org/jboss/dmr/ModelValue.java b/src/main/java/org/jboss/dmr/ModelValue.java index d91c22b..4752faa 100644 --- a/src/main/java/org/jboss/dmr/ModelValue.java +++ b/src/main/java/org/jboss/dmr/ModelValue.java @@ -50,73 +50,73 @@ 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 removeChild(final int index) { @@ -124,11 +124,11 @@ ModelNode removeChild(final int index) { } 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) { @@ -136,15 +136,15 @@ ModelNode insertChild(int index) { } 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() { @@ -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; + } } diff --git a/src/main/java/org/jboss/dmr/StringModelValue.java b/src/main/java/org/jboss/dmr/StringModelValue.java index a6d339a..0d484a1 100644 --- a/src/main/java/org/jboss/dmr/StringModelValue.java +++ b/src/main/java/org/jboss/dmr/StringModelValue.java @@ -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"); } } 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"); } }