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

Better error message while parsing enums #29

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,19 @@ public class ParseOptionConversionException extends ParseException
private final String value;
private final String typeName;

ParseOptionConversionException(String optionTitle, String value, String typeName)
ParseOptionConversionException(String optionTitle, String value, String typeName, String additionalComment)
{
super("%s: can not convert \"%s\" to a %s", optionTitle, value, typeName);
super("%s: can not convert \"%s\" to a %s. %s", optionTitle, value, typeName, additionalComment);
this.optionTitle = optionTitle;
this.value = value;
this.typeName = typeName;
}

ParseOptionConversionException(String optionTitle, String value, String typeName)
{
this(optionTitle, value, typeName, "");
}

public String getOptionTitle()
{
return optionTitle;
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/io/airlift/airline/TypeConverter.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package io.airlift.airline;

import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class TypeConverter
Expand Down Expand Up @@ -53,16 +55,26 @@ else if (Double.class.isAssignableFrom(type) || Double.TYPE.isAssignableFrom(typ
if (valueOf.getReturnType().isAssignableFrom(type)) {
return valueOf.invoke(null, value);
}
} catch (Throwable ignored) {
}
catch (Throwable ignored) {
}

// Look for a static valueOf(String) method (this covers enums which have a valueOf method)
try {
Method valueOf = type.getMethod("valueOf", String.class);
if (valueOf.getReturnType().isAssignableFrom(type)) {
return valueOf.invoke(null, value);
try {
return valueOf.invoke(null, value);
}
catch (InvocationTargetException e) {
String errorMsg = "Invalid " + name + ", Valid values are: " + Joiner.on(", ").join(type.getEnumConstants());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't abbreviate variable names. This can just be message

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use String.format() instead of concatenation

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.getEnumConstants() returns null if the type is not an enum. This error message should only be generated for enums.

throw new ParseOptionConversionException(name, value, type.getSimpleName(), errorMsg);
}
}
}
catch (ParseOptionConversionException e) {
throw e;
}
catch (Throwable ignored) {
}

Expand Down