Search in sources :

Example 1 with XContentLocation

use of org.elasticsearch.common.xcontent.XContentLocation in project elasticsearch by elastic.

the class ExecutableSection method parse.

static ExecutableSection parse(XContentParser parser) throws IOException {
    ParserUtils.advanceToFieldName(parser);
    String section = parser.currentName();
    XContentLocation location = parser.getTokenLocation();
    try {
        ExecutableSection executableSection = parser.namedObject(ExecutableSection.class, section, null);
        parser.nextToken();
        return executableSection;
    } catch (Exception e) {
        throw new IOException("Error parsing section starting at [" + location + "]", e);
    }
}
Also used : XContentLocation(org.elasticsearch.common.xcontent.XContentLocation) IOException(java.io.IOException) IOException(java.io.IOException)

Example 2 with XContentLocation

use of org.elasticsearch.common.xcontent.XContentLocation in project elasticsearch by elastic.

the class GreaterThanAssertion method parse.

public static GreaterThanAssertion parse(XContentParser parser) throws IOException {
    XContentLocation location = parser.getTokenLocation();
    Tuple<String, Object> stringObjectTuple = ParserUtils.parseTuple(parser);
    if (!(stringObjectTuple.v2() instanceof Comparable)) {
        throw new IllegalArgumentException("gt section can only be used with objects that support natural ordering, found " + stringObjectTuple.v2().getClass().getSimpleName());
    }
    return new GreaterThanAssertion(location, stringObjectTuple.v1(), stringObjectTuple.v2());
}
Also used : XContentLocation(org.elasticsearch.common.xcontent.XContentLocation)

Example 3 with XContentLocation

use of org.elasticsearch.common.xcontent.XContentLocation in project elasticsearch by elastic.

the class LengthAssertion method parse.

public static LengthAssertion parse(XContentParser parser) throws IOException {
    XContentLocation location = parser.getTokenLocation();
    Tuple<String, Object> stringObjectTuple = ParserUtils.parseTuple(parser);
    assert stringObjectTuple.v2() != null;
    int value;
    if (stringObjectTuple.v2() instanceof Number) {
        value = ((Number) stringObjectTuple.v2()).intValue();
    } else {
        try {
            value = Integer.valueOf(stringObjectTuple.v2().toString());
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("length is not a valid number", e);
        }
    }
    return new LengthAssertion(location, stringObjectTuple.v1(), value);
}
Also used : XContentLocation(org.elasticsearch.common.xcontent.XContentLocation)

Example 4 with XContentLocation

use of org.elasticsearch.common.xcontent.XContentLocation in project elasticsearch by elastic.

the class ClientYamlTestSectionTests method testAddingDoWithWarningWithSkipButNotWarnings.

public void testAddingDoWithWarningWithSkipButNotWarnings() {
    int lineNumber = between(1, 10000);
    ClientYamlTestSection section = new ClientYamlTestSection(new XContentLocation(0, 0), "test");
    section.setSkipSection(new SkipSection(null, singletonList("yaml"), null));
    DoSection doSection = new DoSection(new XContentLocation(lineNumber, 0));
    doSection.setExpectedWarningHeaders(singletonList("foo"));
    Exception e = expectThrows(IllegalArgumentException.class, () -> section.addExecutableSection(doSection));
    assertEquals("Attempted to add a [do] with a [warnings] section without a corresponding [skip] so runners that do not support the" + " [warnings] section can skip the test at line [" + lineNumber + "]", e.getMessage());
}
Also used : XContentLocation(org.elasticsearch.common.xcontent.XContentLocation) ParsingException(org.elasticsearch.common.ParsingException) IOException(java.io.IOException)

Example 5 with XContentLocation

use of org.elasticsearch.common.xcontent.XContentLocation in project elasticsearch by elastic.

the class QueryParseContext method parseInnerQueryBuilder.

/**
     * Parses a query excluding the query element that wraps it
     */
public QueryBuilder parseInnerQueryBuilder() throws IOException {
    if (parser.currentToken() != XContentParser.Token.START_OBJECT) {
        if (parser.nextToken() != XContentParser.Token.START_OBJECT) {
            throw new ParsingException(parser.getTokenLocation(), "[_na] query malformed, must start with start_object");
        }
    }
    if (parser.nextToken() == XContentParser.Token.END_OBJECT) {
        // we encountered '{}' for a query clause, it used to be supported, deprecated in 5.0 and removed in 6.0
        throw new IllegalArgumentException("query malformed, empty clause found at [" + parser.getTokenLocation() + "]");
    }
    if (parser.currentToken() != XContentParser.Token.FIELD_NAME) {
        throw new ParsingException(parser.getTokenLocation(), "[_na] query malformed, no field after start_object");
    }
    String queryName = parser.currentName();
    // move to the next START_OBJECT
    if (parser.nextToken() != XContentParser.Token.START_OBJECT) {
        throw new ParsingException(parser.getTokenLocation(), "[" + queryName + "] query malformed, no start_object after query name");
    }
    QueryBuilder result;
    try {
        result = parser.namedObject(QueryBuilder.class, queryName, this);
    } catch (UnknownNamedObjectException e) {
        // This intentionally doesn't include the causing exception because that'd change the "root_cause" of any unknown query errors
        throw new ParsingException(new XContentLocation(e.getLineNumber(), e.getColumnNumber()), "no [query] registered for [" + e.getName() + "]");
    }
    //end_object of the specific query (e.g. match, multi_match etc.) element
    if (parser.currentToken() != XContentParser.Token.END_OBJECT) {
        throw new ParsingException(parser.getTokenLocation(), "[" + queryName + "] malformed query, expected [END_OBJECT] but found [" + parser.currentToken() + "]");
    }
    //end_object of the query object
    if (parser.nextToken() != XContentParser.Token.END_OBJECT) {
        throw new ParsingException(parser.getTokenLocation(), "[" + queryName + "] malformed query, expected [END_OBJECT] but found [" + parser.currentToken() + "]");
    }
    return result;
}
Also used : UnknownNamedObjectException(org.elasticsearch.common.xcontent.NamedXContentRegistry.UnknownNamedObjectException) ParsingException(org.elasticsearch.common.ParsingException) XContentLocation(org.elasticsearch.common.xcontent.XContentLocation)

Aggregations

XContentLocation (org.elasticsearch.common.xcontent.XContentLocation)13 ParsingException (org.elasticsearch.common.ParsingException)3 IOException (java.io.IOException)2 SearchParseException (org.elasticsearch.search.SearchParseException)2 TestSearchContext (org.elasticsearch.test.TestSearchContext)2 ArrayList (java.util.ArrayList)1 Collections.emptyList (java.util.Collections.emptyList)1 Collections.singletonList (java.util.Collections.singletonList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 SearchPhaseExecutionException (org.elasticsearch.action.search.SearchPhaseExecutionException)1 ShardSearchFailure (org.elasticsearch.action.search.ShardSearchFailure)1 ClusterBlockException (org.elasticsearch.cluster.block.ClusterBlockException)1 CircuitBreakingException (org.elasticsearch.common.breaker.CircuitBreakingException)1 Tuple (org.elasticsearch.common.collect.Tuple)1 UnknownNamedObjectException (org.elasticsearch.common.xcontent.NamedXContentRegistry.UnknownNamedObjectException)1 Index (org.elasticsearch.index.Index)1 SearchShardTarget (org.elasticsearch.search.SearchShardTarget)1 SearchContext (org.elasticsearch.search.internal.SearchContext)1