More explicit error messages for GREL functions with unexpected arguments (#4255)

Fixes #4193.
This commit is contained in:
nmamoon 2021-11-09 12:13:17 -06:00 committed by GitHub
parent a983781bf7
commit debc4e65c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 51 additions and 8 deletions

View File

@ -38,6 +38,7 @@ import java.util.Properties;
import org.jsoup.nodes.Element;
import com.google.refine.expr.EvalError;
import com.google.refine.expr.functions.Type;
import com.google.refine.expr.functions.xml.InnerXml;
import com.google.refine.grel.ControlFunctionRegistry;
import com.google.refine.grel.Function;
@ -51,10 +52,10 @@ public class InnerHtml implements Function {
if (o1 != null && o1 instanceof Element) {
return new InnerXml().call(bindings, args, "html");
}else{
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " failed as the first parameter is not an HTML Element. Please first use parseHtml(string) and select(query) prior to using this function");
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + "() cannot work with this '" + new Type().call(bindings, args) + "'. The first parameter is not an HTML Element. Please first use parseHtml(string) and select(query) prior to using this function");
}
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a single String as an argument");
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + "() cannot work with this '" + new Type().call(bindings, args) + "' and expects a single String as an argument");
}

View File

@ -36,6 +36,7 @@ package com.google.refine.expr.functions.html;
import java.util.Properties;
import com.google.refine.expr.EvalError;
import com.google.refine.expr.functions.Type;
import com.google.refine.expr.functions.xml.ParseXml;
import com.google.refine.grel.ControlFunctionRegistry;
import com.google.refine.grel.Function;

View File

@ -35,6 +35,7 @@ package com.google.refine.expr.functions.xml;
import java.util.Properties;
import com.google.refine.expr.functions.Type;
import org.jsoup.Jsoup;
import org.jsoup.parser.Parser;

View File

@ -34,6 +34,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package com.google.refine.expr.functions.xml;
import java.util.Properties;
import com.google.refine.expr.functions.Type;
import org.jsoup.nodes.Element;
@ -52,13 +53,12 @@ public class WholeText implements Function {
return e1.wholeText();
}else{
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " failed as the first parameter is not an XML or HTML Element. Please first use parseXml() or parseHtml() and select(query) prior to using this function");
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + "() cannot work with this '" + new Type().call(bindings, args) + "' and failed as the first parameter is not an XML or HTML Element. Please first use parseXml() or parseHtml() and select(query) prior to using this function");
}
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a single XML or HTML element as an argument");
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + "() cannot work with this '" + new Type().call(bindings, args) + "' and expects a single XML or HTML element as an argument");
}
@Override
public String getDescription() {
return "Selects the (unencoded) text of an element and its children, including any new lines and spaces, and returns a string of unencoded, un-normalized text. Use it in conjunction with parseHtml() and select() to provide an element.";

View File

@ -37,6 +37,7 @@ import java.util.Properties;
import org.jsoup.nodes.Element;
import com.google.refine.expr.functions.Type;
import com.google.refine.expr.EvalError;
import com.google.refine.grel.ControlFunctionRegistry;
import com.google.refine.grel.Function;
@ -52,10 +53,10 @@ public class XmlText implements Function {
return e1.text();
}else{
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " failed as the first parameter is not an XML or HTML Element. Please first use parseXml() or parseHtml() and select(query) prior to using this function");
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + "() cannot work with this '" + new Type().call(bindings, args) + "' and failed as the first parameter is not an XML or HTML Element. Please first use parseXml() or parseHtml() and select(query) prior to using this function");
}
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a single XML or HTML element as an argument");
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + "() cannot work with this '" + new Type().call(bindings, args) + "' and expects a single XML or HTML element as an argument");
}

View File

@ -26,14 +26,53 @@
******************************************************************************/
package com.google.refine.expr;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import com.google.refine.expr.EvalError;
import com.google.refine.RefineTest;
import com.google.refine.util.TestUtils;
public class EvalErrorTests {
public class EvalErrorTests extends RefineTest {
@Override
@BeforeTest
public void init() {
logger = LoggerFactory.getLogger(this.getClass());
}
@Test
public void serializeEvalError() {
EvalError e = new EvalError("This is a critical error");
TestUtils.isSerializedTo(e, "{\"type\":\"error\",\"message\":\"This is a critical error\"}");
}
@Test
public void testInnerHtml() {
Assert.assertTrue(invoke("innerHtml") instanceof EvalError);
Assert.assertTrue(invoke("innerHtml", "test") instanceof EvalError);
EvalError evalError = (EvalError) invoke("innerHtml", "test");
Assert.assertEquals(evalError.toString(), "innerHtml() cannot work with this \'string\'. The first parameter is not an HTML Element. Please first use parseHtml(string) and select(query) prior to using this function");
}
@Test
public void testWholeText() {
Assert.assertTrue(invoke("wholeText") instanceof EvalError);
Assert.assertTrue(invoke("wholeText", "test") instanceof EvalError);
EvalError evalError = (EvalError) invoke("wholeText", "test");
Assert.assertEquals(evalError.toString(), "wholeText() cannot work with this \'string\' and failed as the first parameter is not an XML or HTML Element. Please first use parseXml() or parseHtml() and select(query) prior to using this function");
}
@Test
public void testXmlText() {
Assert.assertTrue(invoke("xmlText") instanceof EvalError);
Assert.assertTrue(invoke("xmlText", "test") instanceof EvalError);
EvalError evalError = (EvalError) invoke("xmlText", "test");
Assert.assertEquals(evalError.toString(), "xmlText() cannot work with this \'string\' and failed as the first parameter is not an XML or HTML Element. Please first use parseXml() or parseHtml() and select(query) prior to using this function");
}
}