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 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
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
29 changes: 27 additions & 2 deletions src/main/java/io/airlift/airline/TypeConverter.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package io.airlift.airline;

import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

public class TypeConverter
{
Expand Down Expand Up @@ -53,16 +59,35 @@ 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) {
if (type.isEnum()) {
List<String> enumConstantNames = Lists.transform(Arrays.asList(((Class<Enum>) type).getEnumConstants()), new Function<Enum, String>() {
@Override
public String apply(Enum input)
{
return input.name();
}
});
String message = String.format("Invalid %s, Valid values are: %s", name, Joiner.on(", ").join(enumConstantNames));
throw new ParseOptionConversionException(name, value, type.getSimpleName(), message);
}
}
}
}
catch (ParseOptionConversionException e) {
throw e;
}
catch (Throwable ignored) {
}

Expand Down