Search in sources :

Example 6 with ScriptException

use of org.elasticsearch.script.ScriptException in project elasticsearch by elastic.

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.
     */
protected final ScriptException convertToScriptException(Throwable t, Map<String, List<String>> extraMetadata) {
    // create a script stack: this is just the script portion
    List<String> scriptStack = new ArrayList<>();
    for (StackTraceElement element : t.getStackTrace()) {
        if (WriterConstants.CLASS_NAME.equals(element.getClassName())) {
            // found the script portion
            int offset = element.getLineNumber();
            if (offset == -1) {
                scriptStack.add("<<< unknown portion of script >>>");
            } else {
                // offset is 1 based, line numbers must be!
                offset--;
                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 = source.length();
                }
                // TODO: if this is still too long, truncate and use ellipses
                String snippet = source.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());
            }
            break;
        // but filter our own internal stacks (e.g. indy bootstrap)
        } else if (!shouldFilter(element)) {
            scriptStack.add(element.toString());
        }
    }
    // build a name for the script:
    final String name;
    if (PainlessScriptEngineService.INLINE_NAME.equals(this.name)) {
        name = source;
    } else {
        name = this.name;
    }
    ScriptException scriptException = new ScriptException("runtime error", t, scriptStack, name, PainlessScriptEngineService.NAME);
    for (Map.Entry<String, List<String>> entry : extraMetadata.entrySet()) {
        scriptException.addMetadata(entry.getKey(), entry.getValue());
    }
    return scriptException;
}
Also used : ScriptException(org.elasticsearch.script.ScriptException) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map)

Aggregations

ScriptException (org.elasticsearch.script.ScriptException)6 ParseException (java.text.ParseException)2 ElasticsearchException (org.elasticsearch.ElasticsearchException)2 EOFException (java.io.EOFException)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 NoShardAvailableActionException (org.elasticsearch.action.NoShardAvailableActionException)1 RoutingMissingException (org.elasticsearch.action.RoutingMissingException)1 SearchPhaseExecutionException (org.elasticsearch.action.search.SearchPhaseExecutionException)1 ShardSearchFailure (org.elasticsearch.action.search.ShardSearchFailure)1 BroadcastShardOperationFailedException (org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException)1 NoNodeAvailableException (org.elasticsearch.client.transport.NoNodeAvailableException)1 ClusterBlockException (org.elasticsearch.cluster.block.ClusterBlockException)1 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)1 ParsingException (org.elasticsearch.common.ParsingException)1