Search in sources :

Example 71 with ParsingException

use of org.opensearch.common.ParsingException in project OpenSearch by opensearch-project.

the class StoredScriptTests method testSourceParsingErrors.

public void testSourceParsingErrors() throws Exception {
    // check for missing lang parameter when parsing a script
    try (XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON)) {
        builder.startObject().field("script").startObject().field("source", "code").endObject().endObject();
        IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, () -> StoredScriptSource.parse(BytesReference.bytes(builder), XContentType.JSON));
        assertThat(iae.getMessage(), equalTo("must specify lang for stored script"));
    }
    // check for missing source 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(BytesReference.bytes(builder), XContentType.JSON));
        assertThat(iae.getMessage(), equalTo("must specify source 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("source", "code").startObject("options").field("option", "option").endObject().endObject().endObject();
        IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, () -> StoredScriptSource.parse(BytesReference.bytes(builder), XContentType.JSON));
        assertThat(iae.getMessage(), equalTo("illegal compiler options [{option=option}] specified"));
    }
    // check for unsupported template context
    try (XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON)) {
        builder.startObject().field("template", "code").endObject();
        ParsingException pEx = expectThrows(ParsingException.class, () -> StoredScriptSource.parse(BytesReference.bytes(builder), XContentType.JSON));
        assertThat(pEx.getMessage(), equalTo("unexpected field [template], expected [" + StoredScriptSource.SCRIPT_PARSE_FIELD.getPreferredName() + "]"));
    }
}
Also used : ParsingException(org.opensearch.common.ParsingException) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 72 with ParsingException

use of org.opensearch.common.ParsingException in project OpenSearch by opensearch-project.

the class SearchSourceBuilderTests method assertIndicesBoostParseErrorMessage.

private void assertIndicesBoostParseErrorMessage(String restContent, String expectedErrorMessage) throws IOException {
    try (XContentParser parser = createParser(JsonXContent.jsonXContent, restContent)) {
        ParsingException e = expectThrows(ParsingException.class, () -> SearchSourceBuilder.fromXContent(parser));
        assertEquals(expectedErrorMessage, e.getMessage());
    }
}
Also used : ParsingException(org.opensearch.common.ParsingException) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 73 with ParsingException

use of org.opensearch.common.ParsingException in project OpenSearch by opensearch-project.

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)) {
                String f = parser.text();
                // split on ','
                String[] fs = f.split(",");
                if (fs != null) {
                    // add each feature, separately:
                    for (String feature : fs) {
                        features.add(feature.trim());
                    }
                } else {
                    features.add(f);
                }
            } 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.opensearch.common.ParsingException) ArrayList(java.util.ArrayList) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 74 with ParsingException

use of org.opensearch.common.ParsingException in project OpenSearch by opensearch-project.

the class DoSection method parse.

public static DoSection parse(XContentParser parser) throws IOException {
    String currentFieldName = null;
    XContentParser.Token token;
    DoSection doSection = new DoSection(parser.getTokenLocation());
    ApiCallSection apiCallSection = null;
    NodeSelector nodeSelector = NodeSelector.ANY;
    Map<String, String> headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
    List<String> expectedWarnings = new ArrayList<>();
    List<String> allowedWarnings = new ArrayList<>();
    if (parser.nextToken() != XContentParser.Token.START_OBJECT) {
        throw new IllegalArgumentException("expected [" + XContentParser.Token.START_OBJECT + "], " + "found [" + parser.currentToken() + "], the do section is not properly indented");
    }
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token.isValue()) {
            if ("catch".equals(currentFieldName)) {
                doSection.setCatch(parser.text());
            } else {
                throw new ParsingException(parser.getTokenLocation(), "unsupported field [" + currentFieldName + "]");
            }
        } else if (token == XContentParser.Token.START_ARRAY) {
            if ("warnings".equals(currentFieldName)) {
                while ((token = parser.nextToken()) == XContentParser.Token.VALUE_STRING) {
                    expectedWarnings.add(parser.text());
                }
                if (token != XContentParser.Token.END_ARRAY) {
                    throw new ParsingException(parser.getTokenLocation(), "[warnings] must be a string array but saw [" + token + "]");
                }
            } else if ("allowed_warnings".equals(currentFieldName)) {
                while ((token = parser.nextToken()) == XContentParser.Token.VALUE_STRING) {
                    allowedWarnings.add(parser.text());
                }
                if (token != XContentParser.Token.END_ARRAY) {
                    throw new ParsingException(parser.getTokenLocation(), "[allowed_warnings] must be a string array but saw [" + token + "]");
                }
            } else {
                throw new ParsingException(parser.getTokenLocation(), "unknown array [" + currentFieldName + "]");
            }
        } else if (token == XContentParser.Token.START_OBJECT) {
            if ("headers".equals(currentFieldName)) {
                String headerName = null;
                while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                    if (token == XContentParser.Token.FIELD_NAME) {
                        headerName = parser.currentName();
                    } else if (token.isValue()) {
                        headers.put(headerName, parser.text());
                    }
                }
            } else if ("node_selector".equals(currentFieldName)) {
                String selectorName = null;
                while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                    if (token == XContentParser.Token.FIELD_NAME) {
                        selectorName = parser.currentName();
                    } else {
                        NodeSelector newSelector = buildNodeSelector(selectorName, parser);
                        nodeSelector = nodeSelector == NodeSelector.ANY ? newSelector : new ComposeNodeSelector(nodeSelector, newSelector);
                    }
                }
            } else if (currentFieldName != null) {
                // must be part of API call then
                apiCallSection = new ApiCallSection(currentFieldName);
                String paramName = null;
                while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                    if (token == XContentParser.Token.FIELD_NAME) {
                        paramName = parser.currentName();
                    } else if (token.isValue()) {
                        if ("body".equals(paramName)) {
                            String body = parser.text();
                            XContentParser bodyParser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, body);
                            // multiple bodies are supported e.g. in case of bulk provided as a whole string
                            while (bodyParser.nextToken() != null) {
                                apiCallSection.addBody(bodyParser.mapOrdered());
                            }
                        } else {
                            apiCallSection.addParam(paramName, parser.text());
                        }
                    } else if (token == XContentParser.Token.START_OBJECT) {
                        if ("body".equals(paramName)) {
                            apiCallSection.addBody(parser.mapOrdered());
                        }
                    }
                }
            }
        }
    }
    try {
        if (apiCallSection == null) {
            throw new IllegalArgumentException("client call section is mandatory within a do section");
        }
        for (String w : expectedWarnings) {
            if (allowedWarnings.contains(w)) {
                throw new IllegalArgumentException("the warning [" + w + "] was both allowed and expected");
            }
        }
        apiCallSection.addHeaders(headers);
        apiCallSection.setNodeSelector(nodeSelector);
        doSection.setApiCallSection(apiCallSection);
        doSection.setExpectedWarningHeaders(unmodifiableList(expectedWarnings));
        doSection.setAllowedWarningHeaders(unmodifiableList(allowedWarnings));
    } finally {
        parser.nextToken();
    }
    return doSection;
}
Also used : ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) ParsingException(org.opensearch.common.ParsingException) HasAttributeNodeSelector(org.opensearch.client.HasAttributeNodeSelector) NodeSelector(org.opensearch.client.NodeSelector) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 75 with ParsingException

use of org.opensearch.common.ParsingException in project OpenSearch by opensearch-project.

the class DoSectionTests method testUnsupportedTopLevelField.

public void testUnsupportedTopLevelField() throws Exception {
    parser = createParser(YamlXContent.yamlXContent, "max_concurrent_shard_requests: 1");
    ParsingException e = expectThrows(ParsingException.class, () -> DoSection.parse(parser));
    assertThat(e.getMessage(), is("unsupported field [max_concurrent_shard_requests]"));
    parser.nextToken();
    parser.nextToken();
}
Also used : ParsingException(org.opensearch.common.ParsingException)

Aggregations

ParsingException (org.opensearch.common.ParsingException)157 XContentParser (org.opensearch.common.xcontent.XContentParser)92 ArrayList (java.util.ArrayList)27 Matchers.containsString (org.hamcrest.Matchers.containsString)21 IOException (java.io.IOException)14 Token (org.opensearch.common.xcontent.XContentParser.Token)11 List (java.util.List)10 ShardId (org.opensearch.index.shard.ShardId)10 SearchShardTarget (org.opensearch.search.SearchShardTarget)10 BytesReference (org.opensearch.common.bytes.BytesReference)9 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)9 HashMap (java.util.HashMap)7 QueryBuilder (org.opensearch.index.query.QueryBuilder)7 ShardSearchFailure (org.opensearch.action.search.ShardSearchFailure)6 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)5 OpenSearchParseException (org.opensearch.OpenSearchParseException)5 TimestampParsingException (org.opensearch.action.TimestampParsingException)5 XContentLocation (org.opensearch.common.xcontent.XContentLocation)5 Script (org.opensearch.script.Script)5 GapPolicy (org.opensearch.search.aggregations.pipeline.BucketHelpers.GapPolicy)5