Search in sources :

Example 11 with ScriptException

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;
}
Also used : ScriptException(org.opensearch.script.ScriptException) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map)

Example 12 with 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());
}
Also used : ScriptException(org.opensearch.script.ScriptException) Matchers.emptyOrNullString(org.hamcrest.Matchers.emptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 13 with ScriptException

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());
}
Also used : ScriptException(org.opensearch.script.ScriptException) Matchers.emptyOrNullString(org.hamcrest.Matchers.emptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 14 with ScriptException

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);
}
Also used : ScriptException(org.opensearch.script.ScriptException) ParseException(java.text.ParseException)

Example 15 with ScriptException

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);
    }
}
Also used : Script(org.opensearch.script.Script) ScriptEngine(org.opensearch.script.ScriptEngine) StringWriter(java.io.StringWriter) Mustache(com.github.mustachejava.Mustache) Set(java.util.Set) MustacheException(com.github.mustachejava.MustacheException) Reader(java.io.Reader) PrivilegedAction(java.security.PrivilegedAction) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) GeneralScriptException(org.opensearch.script.GeneralScriptException) ScriptException(org.opensearch.script.ScriptException) Logger(org.apache.logging.log4j.Logger) StringReader(java.io.StringReader) Supplier(org.apache.logging.log4j.util.Supplier) Map(java.util.Map) MustacheFactory(com.github.mustachejava.MustacheFactory) SpecialPermission(org.opensearch.SpecialPermission) TemplateScript(org.opensearch.script.TemplateScript) AccessController(java.security.AccessController) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) ScriptContext(org.opensearch.script.ScriptContext) GeneralScriptException(org.opensearch.script.GeneralScriptException) ScriptException(org.opensearch.script.ScriptException) StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) Mustache(com.github.mustachejava.Mustache) MustacheException(com.github.mustachejava.MustacheException) TemplateScript(org.opensearch.script.TemplateScript) MustacheFactory(com.github.mustachejava.MustacheFactory)

Aggregations

ScriptException (org.opensearch.script.ScriptException)18 ParseException (java.text.ParseException)8 Map (java.util.Map)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 Script (org.opensearch.script.Script)3 ScriptService (org.opensearch.script.ScriptService)3 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Matchers.emptyOrNullString (org.hamcrest.Matchers.emptyOrNullString)2 IngestConditionalScript (org.opensearch.script.IngestConditionalScript)2 MockScriptService (org.opensearch.script.MockScriptService)2 Mustache (com.github.mustachejava.Mustache)1 MustacheException (com.github.mustachejava.MustacheException)1 MustacheFactory (com.github.mustachejava.MustacheFactory)1 EOFException (java.io.EOFException)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Reader (java.io.Reader)1