Search in sources :

Example 1 with SearchParseException

use of org.opensearch.search.SearchParseException in project OpenSearch by opensearch-project.

the class ExceptionSerializationTests method testSearchParseException.

public void testSearchParseException() throws IOException {
    SearchParseException ex = serialize(new SearchParseException(SHARD_TARGET, "foo", new XContentLocation(66, 666)));
    assertEquals("foo", ex.getMessage());
    assertEquals(66, ex.getLineNumber());
    assertEquals(666, ex.getColumnNumber());
}
Also used : SearchParseException(org.opensearch.search.SearchParseException) XContentLocation(org.opensearch.common.xcontent.XContentLocation)

Example 2 with SearchParseException

use of org.opensearch.search.SearchParseException in project OpenSearch by opensearch-project.

the class OpenSearchExceptionTests method randomExceptions.

public static Tuple<Throwable, OpenSearchException> randomExceptions() {
    Throwable actual;
    OpenSearchException expected;
    int type = randomIntBetween(0, 5);
    switch(type) {
        case 0:
            actual = new ClusterBlockException(singleton(NoMasterBlockService.NO_MASTER_BLOCK_WRITES));
            expected = new OpenSearchException("OpenSearch exception [type=cluster_block_exception, " + "reason=blocked by: [SERVICE_UNAVAILABLE/2/no master];]");
            break;
        case // Simple opensearch exception with headers (other metadata of type number are not parsed)
        1:
            actual = new ParsingException(3, 2, "Unknown identifier", null);
            expected = new OpenSearchException("OpenSearch exception [type=parsing_exception, reason=Unknown identifier]");
            break;
        case 2:
            actual = new SearchParseException(SHARD_TARGET, "Parse failure", new XContentLocation(12, 98));
            expected = new OpenSearchException("OpenSearch exception [type=search_parse_exception, reason=Parse failure]");
            break;
        case 3:
            actual = new IllegalArgumentException("Closed resource", new RuntimeException("Resource"));
            expected = new OpenSearchException("OpenSearch exception [type=illegal_argument_exception, reason=Closed resource]", new OpenSearchException("OpenSearch exception [type=runtime_exception, reason=Resource]"));
            break;
        case 4:
            actual = new SearchPhaseExecutionException("search", "all shards failed", new ShardSearchFailure[] { new ShardSearchFailure(new ParsingException(1, 2, "foobar", null), new SearchShardTarget("node_1", new ShardId("foo", "_na_", 1), null, OriginalIndices.NONE)) });
            expected = new OpenSearchException("OpenSearch exception [type=search_phase_execution_exception, " + "reason=all shards failed]");
            expected.addMetadata("opensearch.phase", "search");
            break;
        case 5:
            actual = new OpenSearchException("Parsing failed", new ParsingException(9, 42, "Wrong state", new NullPointerException("Unexpected null value")));
            OpenSearchException expectedCause = new OpenSearchException("OpenSearch exception [type=parsing_exception, " + "reason=Wrong state]", new OpenSearchException("OpenSearch exception [type=null_pointer_exception, " + "reason=Unexpected null value]"));
            expected = new OpenSearchException("OpenSearch exception [type=exception, reason=Parsing failed]", expectedCause);
            break;
        default:
            throw new UnsupportedOperationException("No randomized exceptions generated for type [" + type + "]");
    }
    if (actual instanceof OpenSearchException) {
        OpenSearchException actualException = (OpenSearchException) actual;
        if (randomBoolean()) {
            int nbHeaders = randomIntBetween(1, 5);
            Map<String, List<String>> randomHeaders = new HashMap<>(nbHeaders);
            for (int i = 0; i < nbHeaders; i++) {
                List<String> values = new ArrayList<>();
                int nbValues = randomIntBetween(1, 3);
                for (int j = 0; j < nbValues; j++) {
                    values.add(frequently() ? randomAlphaOfLength(5) : "");
                }
                randomHeaders.put("header_" + i, values);
            }
            for (Map.Entry<String, List<String>> entry : randomHeaders.entrySet()) {
                actualException.addHeader(entry.getKey(), entry.getValue());
                expected.addHeader(entry.getKey(), entry.getValue());
            }
            if (rarely()) {
                // Empty or null headers are not printed out by the toXContent method
                actualException.addHeader("ignored", randomBoolean() ? emptyList() : null);
            }
        }
        if (randomBoolean()) {
            int nbMetadata = randomIntBetween(1, 5);
            Map<String, List<String>> randomMetadata = new HashMap<>(nbMetadata);
            for (int i = 0; i < nbMetadata; i++) {
                List<String> values = new ArrayList<>();
                int nbValues = randomIntBetween(1, 3);
                for (int j = 0; j < nbValues; j++) {
                    values.add(frequently() ? randomAlphaOfLength(5) : "");
                }
                randomMetadata.put("opensearch.metadata_" + i, values);
            }
            for (Map.Entry<String, List<String>> entry : randomMetadata.entrySet()) {
                actualException.addMetadata(entry.getKey(), entry.getValue());
                expected.addMetadata(entry.getKey(), entry.getValue());
            }
            if (rarely()) {
                // Empty or null metadata are not printed out by the toXContent method
                actualException.addMetadata("opensearch.ignored", randomBoolean() ? emptyList() : null);
            }
        }
        if (randomBoolean()) {
            int nbResources = randomIntBetween(1, 5);
            for (int i = 0; i < nbResources; i++) {
                String resourceType = "type_" + i;
                String[] resourceIds = new String[randomIntBetween(1, 3)];
                for (int j = 0; j < resourceIds.length; j++) {
                    resourceIds[j] = frequently() ? randomAlphaOfLength(5) : "";
                }
                actualException.setResources(resourceType, resourceIds);
                expected.setResources(resourceType, resourceIds);
            }
        }
    }
    return new Tuple<>(actual, expected);
}
Also used : HashMap(java.util.HashMap) SearchPhaseExecutionException(org.opensearch.action.search.SearchPhaseExecutionException) ArrayList(java.util.ArrayList) ShardId(org.opensearch.index.shard.ShardId) ParsingException(org.opensearch.common.ParsingException) Collections.singletonList(java.util.Collections.singletonList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) ArrayList(java.util.ArrayList) ShardSearchFailure(org.opensearch.action.search.ShardSearchFailure) XContentLocation(org.opensearch.common.xcontent.XContentLocation) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) SearchParseException(org.opensearch.search.SearchParseException) SearchShardTarget(org.opensearch.search.SearchShardTarget) Map(java.util.Map) HashMap(java.util.HashMap) Tuple(org.opensearch.common.collect.Tuple)

Aggregations

XContentLocation (org.opensearch.common.xcontent.XContentLocation)2 SearchParseException (org.opensearch.search.SearchParseException)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.opensearch.action.search.SearchPhaseExecutionException)1 ShardSearchFailure (org.opensearch.action.search.ShardSearchFailure)1 ClusterBlockException (org.opensearch.cluster.block.ClusterBlockException)1 ParsingException (org.opensearch.common.ParsingException)1 Tuple (org.opensearch.common.collect.Tuple)1 ShardId (org.opensearch.index.shard.ShardId)1 SearchShardTarget (org.opensearch.search.SearchShardTarget)1