Improve error messages - fixes #878

This commit is contained in:
Tom Morris 2014-05-26 18:12:14 -04:00
parent 672e9bf62c
commit 2624e2a65c

View File

@ -58,15 +58,26 @@ public class Phonetic implements Function {
@Override @Override
public Object call(Properties bindings, Object[] args) { public Object call(Properties bindings, Object[] args) {
if (args.length == 2) { String str;
if (args.length > 0 && args[0] != null) {
Object o1 = args[0]; Object o1 = args[0];
Object o2 = args[1]; str = (o1 instanceof String) ? (String) o1 : o1.toString();
if (o1 != null && o2 != null && o2 instanceof String) { } else {
String str = (o1 instanceof String) ? (String) o1 : o1.toString(); return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects at least one argument");
String encoding = ((String) o2).toLowerCase();
if (encoding == null) {
encoding = "metaphone3";
} }
String encoding = "metaphone3";
if (args.length > 1) {
Object o2 = args[1];
if (o2 != null) {
if (o2 instanceof String) {
encoding = ((String) o2).toLowerCase();
} else {
return new EvalError(ControlFunctionRegistry.getFunctionName(this)
+ " expects a string for the second argument");
}
}
}
if (args.length < 3) {
if ("doublemetaphone".equalsIgnoreCase(encoding)) { if ("doublemetaphone".equalsIgnoreCase(encoding)) {
return metaphone2.key(str); return metaphone2.key(str);
} else if ("metaphone3".equalsIgnoreCase(encoding)) { } else if ("metaphone3".equalsIgnoreCase(encoding)) {
@ -78,12 +89,15 @@ public class Phonetic implements Function {
} else if ("cologne".equalsIgnoreCase(encoding)) { } else if ("cologne".equalsIgnoreCase(encoding)) {
return cologne.key(str); return cologne.key(str);
} else { } else {
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " doesn't know how to handle the '" + encoding + "' encoding."); return new EvalError(ControlFunctionRegistry.getFunctionName(this)
+ " doesn't know how to handle the '"
+ encoding + "' encoding.");
} }
} else {
return new EvalError(ControlFunctionRegistry.getFunctionName(this)
+ " expects one or two string arguments");
} }
} }
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 3 strings");
}
@Override @Override
public void write(JSONWriter writer, Properties options) public void write(JSONWriter writer, Properties options)