Search in sources :

Example 11 with ParsingException

use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.

the class SkipSection method parse.

public static SkipSection parse(XContentParser parser) throws IOException {
    if (parser.nextToken() != XContentParser.Token.START_OBJECT) {
        throw new IllegalArgumentException("Expected [" + XContentParser.Token.START_OBJECT + ", found [" + parser.currentToken() + "], the skip section is not properly indented");
    }
    String currentFieldName = null;
    XContentParser.Token token;
    String version = null;
    String reason = null;
    List<String> features = new ArrayList<>();
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token.isValue()) {
            if ("version".equals(currentFieldName)) {
                version = parser.text();
            } else if ("reason".equals(currentFieldName)) {
                reason = parser.text();
            } else if ("features".equals(currentFieldName)) {
                features.add(parser.text());
            } else {
                throw new ParsingException(parser.getTokenLocation(), "field " + currentFieldName + " not supported within skip section");
            }
        } else if (token == XContentParser.Token.START_ARRAY) {
            if ("features".equals(currentFieldName)) {
                while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
                    features.add(parser.text());
                }
            }
        }
    }
    parser.nextToken();
    if (!Strings.hasLength(version) && features.isEmpty()) {
        throw new ParsingException(parser.getTokenLocation(), "version or features is mandatory within skip section");
    }
    if (Strings.hasLength(version) && !Strings.hasLength(reason)) {
        throw new ParsingException(parser.getTokenLocation(), "reason is mandatory within skip version section");
    }
    return new SkipSection(version, features, reason);
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) ArrayList(java.util.ArrayList) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 12 with ParsingException

use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.

the class AbstractQueryTestCase method testUnknownField.

/**
     * Test that unknown field trigger ParsingException.
     * To find the right position in the root query, we add a marker as `queryName` which
     * all query builders support. The added bogus field after that should trigger the exception.
     * Queries that allow arbitrary field names at this level need to override this test.
     */
public void testUnknownField() throws IOException {
    String marker = "#marker#";
    QB testQuery;
    do {
        testQuery = createTestQueryBuilder();
    } while (testQuery.toString().contains(marker));
    // to find root query to add additional bogus field there
    testQuery.queryName(marker);
    String queryAsString = testQuery.toString().replace("\"" + marker + "\"", "\"" + marker + "\", \"bogusField\" : \"someValue\"");
    ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(queryAsString));
    // we'd like to see the offending field name here
    assertThat(e.getMessage(), containsString("bogusField"));
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 13 with ParsingException

use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.

the class ConstructingObjectParserTests method testBadParamBeforeObjectBuilt.

public void testBadParamBeforeObjectBuilt() throws IOException {
    XContentParser parser = createParser(JsonXContent.jsonXContent, "{\n" + "  \"a\": \"supercalifragilisticexpialidocious\",\n" + "  \"animal\": \"cat\"\n," + "  \"vegetable\": 2\n" + "}");
    ParsingException e = expectThrows(ParsingException.class, () -> randomFrom(HasCtorArguments.ALL_PARSERS).apply(parser, null));
    assertEquals("[has_required_arguments] failed to parse field [vegetable]", e.getMessage());
    assertEquals(4, e.getLineNumber());
    e = (ParsingException) e.getCause();
    assertEquals("failed to build [has_required_arguments] after last required field arrived", e.getMessage());
    assertEquals(2, e.getLineNumber());
    e = (ParsingException) e.getCause();
    assertEquals("[has_required_arguments] failed to parse field [a]", e.getMessage());
    assertEquals(2, e.getLineNumber());
    assertEquals("[a] must be less than 10 characters in length but was [supercalifragilisticexpialidocious]", e.getCause().getMessage());
}
Also used : ParsingException(org.elasticsearch.common.ParsingException)

Example 14 with ParsingException

use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.

the class ObjectParserTests method testParseNamedObjectInOrderNotSupported.

public void testParseNamedObjectInOrderNotSupported() throws IOException {
    XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": [\n" + "  {\"a\": {}}" + "]}");
    // Create our own parser for this test so we can disable support for the "ordered" mode specified by the array above
    ObjectParser<NamedObjectHolder, Void> objectParser = new ObjectParser<>("named_object_holder", NamedObjectHolder::new);
    objectParser.declareNamedObjects(NamedObjectHolder::setNamed, NamedObject.PARSER, new ParseField("named"));
    // Now firing the xml through it fails
    ParsingException e = expectThrows(ParsingException.class, () -> objectParser.apply(parser, null));
    assertEquals("[named_object_holder] failed to parse field [named]", e.getMessage());
    assertEquals("[named] doesn't support arrays. Use a single object with multiple fields.", e.getCause().getMessage());
}
Also used : NamedObjectParser(org.elasticsearch.common.xcontent.ObjectParser.NamedObjectParser) ParsingException(org.elasticsearch.common.ParsingException) ParseField(org.elasticsearch.common.ParseField)

Example 15 with ParsingException

use of org.elasticsearch.common.ParsingException in project elasticsearch by elastic.

the class ObjectParserTests method testParseNamedObjectNoFieldsInArray.

public void testParseNamedObjectNoFieldsInArray() throws IOException {
    XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": [\n" + "  {}" + "]}");
    ParsingException e = expectThrows(ParsingException.class, () -> NamedObjectHolder.PARSER.apply(parser, null));
    assertEquals("[named_object_holder] failed to parse field [named]", e.getMessage());
    assertEquals("[named] can be a single object with any number of fields or an array where each entry is an object with a single field", e.getCause().getMessage());
}
Also used : ParsingException(org.elasticsearch.common.ParsingException)

Aggregations

ParsingException (org.elasticsearch.common.ParsingException)165 XContentParser (org.elasticsearch.common.xcontent.XContentParser)96 ArrayList (java.util.ArrayList)26 Matchers.containsString (org.hamcrest.Matchers.containsString)22 IOException (java.io.IOException)12 HashMap (java.util.HashMap)10 Token (org.elasticsearch.common.xcontent.XContentParser.Token)9 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)7 Index (org.elasticsearch.index.Index)7 Script (org.elasticsearch.script.Script)7 SearchShardTarget (org.elasticsearch.search.SearchShardTarget)7 List (java.util.List)6 BytesReference (org.elasticsearch.common.bytes.BytesReference)6 QueryParseContext (org.elasticsearch.index.query.QueryParseContext)6 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)5 GeoPoint (org.elasticsearch.common.geo.GeoPoint)5 GapPolicy (org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy)5 SearchPhaseExecutionException (org.elasticsearch.action.search.SearchPhaseExecutionException)4 ToXContent (org.elasticsearch.common.xcontent.ToXContent)4 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)4