use of org.opensearch.script.ScriptException in project OpenSearch by opensearch-project.
the class PainlessScript method convertToScriptException.
/**
* Adds stack trace and other useful information to exceptions thrown
* from a Painless script.
* @param t The throwable to build an exception around.
* @return The generated ScriptException.
*/
default ScriptException convertToScriptException(Throwable t, Map<String, List<String>> extraMetadata) {
// create a script stack: this is just the script portion
List<String> scriptStack = new ArrayList<>();
ScriptException.Position pos = null;
for (StackTraceElement element : t.getStackTrace()) {
if (WriterConstants.CLASS_NAME.equals(element.getClassName())) {
// found the script portion
int originalOffset = element.getLineNumber();
if (originalOffset == -1) {
scriptStack.add("<<< unknown portion of script >>>");
} else {
// offset is 1 based, line numbers must be!
int offset = --originalOffset;
int startOffset = getPreviousStatement(offset);
if (startOffset == -1) {
// should never happen unless we hit exc in ctor prologue...
assert false;
startOffset = 0;
}
int endOffset = getNextStatement(startOffset);
if (endOffset == -1) {
endOffset = getSource().length();
}
// TODO: if this is still too long, truncate and use ellipses
String snippet = getSource().substring(startOffset, endOffset);
scriptStack.add(snippet);
StringBuilder pointer = new StringBuilder();
for (int i = startOffset; i < offset; i++) {
pointer.append(' ');
}
pointer.append("^---- HERE");
scriptStack.add(pointer.toString());
pos = new ScriptException.Position(originalOffset, startOffset, endOffset);
}
break;
// but filter our own internal stacks (e.g. indy bootstrap)
} else if (!shouldFilter(element)) {
scriptStack.add(element.toString());
}
}
ScriptException scriptException = new ScriptException("runtime error", t, scriptStack, getName(), PainlessScriptEngine.NAME, pos);
for (Map.Entry<String, List<String>> entry : extraMetadata.entrySet()) {
scriptException.addMetadata(entry.getKey(), entry.getValue());
}
return scriptException;
}
use of org.opensearch.script.ScriptException in project OpenSearch by opensearch-project.
the class MustacheTests method testsUnsupportedTagsToJson.
public void testsUnsupportedTagsToJson() {
final String script = "{{#toJson}}{{foo}}{{bar}}{{/toJson}}";
ScriptException e = expectThrows(ScriptException.class, () -> compile(script));
assertThat(e.getMessage(), containsString("Mustache function [toJson] must contain one and only one identifier"));
assertEquals(MustacheScriptEngine.NAME, e.getLang());
assertEquals(script, e.getScript());
final String script2 = "{{#toJson}}{{/toJson}}";
e = expectThrows(ScriptException.class, () -> compile(script2));
assertThat(e.getMessage(), containsString("Mustache function [toJson] must contain one and only one identifier"));
assertEquals(MustacheScriptEngine.NAME, e.getLang());
assertEquals(script2, e.getScript());
}
use of org.opensearch.script.ScriptException in project OpenSearch by opensearch-project.
the class MustacheTests method testsUnsupportedTagsJoin.
public void testsUnsupportedTagsJoin() {
final String script = "{{#join}}{{/join}}";
ScriptException e = expectThrows(ScriptException.class, () -> compile(script));
assertThat(e.getMessage(), containsString("Mustache function [join] must contain one and only one identifier"));
assertEquals(MustacheScriptEngine.NAME, e.getLang());
assertEquals(script, e.getScript());
final String script2 = "{{#join delimiter='a'}}{{/join delimiter='b'}}";
e = expectThrows(ScriptException.class, () -> compile(script2));
assertThat(e.getMessage(), containsString("Mismatched start/end tags"));
assertEquals(MustacheScriptEngine.NAME, e.getLang());
assertEquals(script2, e.getScript());
}
use of org.opensearch.script.ScriptException in project OpenSearch by opensearch-project.
the class ExpressionNumberSortScriptTests method testCompileError.
public void testCompileError() {
ScriptException e = expectThrows(ScriptException.class, () -> {
compile("doc['field'].value * *@#)(@$*@#$ + 4");
});
assertTrue(e.getCause() instanceof ParseException);
}
use of org.opensearch.script.ScriptException in project OpenSearch by opensearch-project.
the class MustacheScriptEngine method compile.
/**
* Compile a template string to (in this case) a Mustache object than can
* later be re-used for execution to fill in missing parameter values.
*
* @param templateSource a string representing the template to compile.
* @return a compiled template object for later execution.
*/
@Override
public <T> T compile(String templateName, String templateSource, ScriptContext<T> context, Map<String, String> options) {
if (context.instanceClazz.equals(TemplateScript.class) == false) {
throw new IllegalArgumentException("mustache engine does not know how to handle context [" + context.name + "]");
}
final MustacheFactory factory = createMustacheFactory(options);
Reader reader = new StringReader(templateSource);
try {
Mustache template = factory.compile(reader, "query-template");
TemplateScript.Factory compiled = params -> new MustacheExecutableScript(template, params);
return context.factoryClazz.cast(compiled);
} catch (MustacheException ex) {
throw new ScriptException(ex.getMessage(), ex, Collections.emptyList(), templateSource, NAME);
}
}
Aggregations