Search in sources :

Example 71 with ParsingException

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

the class TermsQueryBuilderTests method testBothValuesAndLookupSet.

public void testBothValuesAndLookupSet() throws IOException {
    String query = "{\n" + "  \"terms\": {\n" + "    \"field\": [\n" + "      \"blue\",\n" + "      \"pill\"\n" + "    ],\n" + "    \"field_lookup\": {\n" + "      \"index\": \"pills\",\n" + "      \"type\": \"red\",\n" + "      \"id\": \"3\",\n" + "      \"path\": \"white rabbit\"\n" + "    }\n" + "  }\n" + "}";
    ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(query));
    assertThat(e.getMessage(), containsString("[" + TermsQueryBuilder.NAME + "] query does not support more than one field."));
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 72 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 73 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 74 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 75 with ParsingException

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

the class StoredScriptTests method testSourceParsingErrors.

public void testSourceParsingErrors() throws Exception {
    // check for missing lang parameter when parsing a template
    try (XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON)) {
        builder.startObject().field("template", "code").endObject();
        IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, () -> StoredScriptSource.parse(null, builder.bytes(), XContentType.JSON));
        assertThat(iae.getMessage(), equalTo("unexpected stored script format"));
    }
    // check for missing lang parameter when parsing a script
    try (XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON)) {
        builder.startObject().field("script").startObject().field("code", "code").endObject().endObject();
        IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, () -> StoredScriptSource.parse(null, builder.bytes(), XContentType.JSON));
        assertThat(iae.getMessage(), equalTo("must specify lang for stored script"));
    }
    // check for missing code parameter when parsing a script
    try (XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON)) {
        builder.startObject().field("script").startObject().field("lang", "lang").endObject().endObject();
        IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, () -> StoredScriptSource.parse(null, builder.bytes(), XContentType.JSON));
        assertThat(iae.getMessage(), equalTo("must specify code for stored script"));
    }
    // check for illegal options parameter when parsing a script
    try (XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON)) {
        builder.startObject().field("script").startObject().field("lang", "lang").field("code", "code").startObject("options").field("option", "option").endObject().endObject().endObject();
        IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, () -> StoredScriptSource.parse(null, builder.bytes(), XContentType.JSON));
        assertThat(iae.getMessage(), equalTo("illegal compiler options [{option=option}] specified"));
    }
    // check for illegal use of content type option
    try (XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON)) {
        builder.startObject().field("script").startObject().field("lang", "lang").field("code", "code").startObject("options").field("content_type", "option").endObject().endObject().endObject();
        ParsingException pe = expectThrows(ParsingException.class, () -> StoredScriptSource.parse(null, builder.bytes(), XContentType.JSON));
        assertThat(pe.getRootCause().getMessage(), equalTo("content_type cannot be user-specified"));
    }
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

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