Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DMR-11] Add useful exception messages for illegal conversions #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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