Преглед на файлове

Remove unused SearchParseException (#92446)

This exception is only used as random exception in tests and isn't used in
production code since 7.11, so we should remove it. ParsingException or
XContentParseException are preferred choices when parsing location for errors is
available.
Christoph Büscher преди 2 години
родител
ревизия
e53a0f8028

+ 0 - 3
modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RankEvalResponseTests.java

@@ -23,7 +23,6 @@ import org.elasticsearch.common.util.Maps;
 import org.elasticsearch.common.xcontent.XContentHelper;
 import org.elasticsearch.index.shard.ShardId;
 import org.elasticsearch.search.SearchHit;
-import org.elasticsearch.search.SearchParseException;
 import org.elasticsearch.search.SearchShardTarget;
 import org.elasticsearch.test.ESTestCase;
 import org.elasticsearch.xcontent.ToXContent;
@@ -44,7 +43,6 @@ import java.util.function.Predicate;
 
 import static java.util.Collections.singleton;
 import static org.elasticsearch.common.xcontent.XContentHelper.toXContent;
-import static org.elasticsearch.test.TestSearchContext.SHARD_TARGET;
 import static org.elasticsearch.test.XContentTestUtils.insertRandomFields;
 import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertToXContentEquivalent;
 import static org.hamcrest.Matchers.instanceOf;
@@ -54,7 +52,6 @@ public class RankEvalResponseTests extends ESTestCase {
     private static final Exception[] RANDOM_EXCEPTIONS = new Exception[] {
         new ClusterBlockException(singleton(NoMasterBlockService.NO_MASTER_BLOCK_WRITES)),
         new CircuitBreakingException("Data too large", 123, 456, CircuitBreaker.Durability.PERMANENT),
-        new SearchParseException(SHARD_TARGET, "Parse failure", new XContentLocation(12, 98)),
         new IllegalArgumentException("Closed resource", new RuntimeException("Resource")),
         new SearchPhaseExecutionException(
             "search",

+ 1 - 6
server/src/main/java/org/elasticsearch/ElasticsearchException.java

@@ -1090,12 +1090,7 @@ public class ElasticsearchException extends RuntimeException implements ToXConte
             71,
             UNKNOWN_VERSION_ADDED
         ),
-        SEARCH_PARSE_EXCEPTION(
-            org.elasticsearch.search.SearchParseException.class,
-            org.elasticsearch.search.SearchParseException::new,
-            72,
-            UNKNOWN_VERSION_ADDED
-        ),
+        // 72 was SearchParseException, only used in tests after 7.11
         CONCURRENT_SNAPSHOT_EXECUTION_EXCEPTION(
             org.elasticsearch.snapshots.ConcurrentSnapshotExecutionException.class,
             org.elasticsearch.snapshots.ConcurrentSnapshotExecutionException::new,

+ 0 - 85
server/src/main/java/org/elasticsearch/search/SearchParseException.java

@@ -1,85 +0,0 @@
-/*
- * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
- * or more contributor license agreements. Licensed under the Elastic License
- * 2.0 and the Server Side Public License, v 1; you may not use this file except
- * in compliance with, at your election, the Elastic License 2.0 or the Server
- * Side Public License, v 1.
- */
-
-package org.elasticsearch.search;
-
-import org.elasticsearch.common.io.stream.StreamInput;
-import org.elasticsearch.common.io.stream.StreamOutput;
-import org.elasticsearch.core.Nullable;
-import org.elasticsearch.rest.RestStatus;
-import org.elasticsearch.xcontent.XContentBuilder;
-import org.elasticsearch.xcontent.XContentLocation;
-
-import java.io.IOException;
-
-public class SearchParseException extends SearchException {
-
-    public static final int UNKNOWN_POSITION = -1;
-    private final int lineNumber;
-    private final int columnNumber;
-
-    public SearchParseException(SearchShardTarget shardTarget, String msg, @Nullable XContentLocation location) {
-        this(shardTarget, msg, location, null);
-    }
-
-    public SearchParseException(SearchShardTarget shardTarget, String msg, @Nullable XContentLocation location, Throwable cause) {
-        super(shardTarget, msg, cause);
-        int lineNumber = UNKNOWN_POSITION;
-        int columnNumber = UNKNOWN_POSITION;
-        if (location != null) {
-            lineNumber = location.lineNumber();
-            columnNumber = location.columnNumber();
-        }
-        this.columnNumber = columnNumber;
-        this.lineNumber = lineNumber;
-    }
-
-    public SearchParseException(StreamInput in) throws IOException {
-        super(in);
-        lineNumber = in.readInt();
-        columnNumber = in.readInt();
-    }
-
-    @Override
-    public void writeTo(StreamOutput out) throws IOException {
-        super.writeTo(out);
-        out.writeInt(lineNumber);
-        out.writeInt(columnNumber);
-    }
-
-    @Override
-    public RestStatus status() {
-        return RestStatus.BAD_REQUEST;
-    }
-
-    @Override
-    protected void metadataToXContent(XContentBuilder builder, Params params) throws IOException {
-        if (lineNumber != UNKNOWN_POSITION) {
-            builder.field("line", lineNumber);
-            builder.field("col", columnNumber);
-        }
-    }
-
-    /**
-     * Line number of the location of the error
-     *
-     * @return the line number or -1 if unknown
-     */
-    public int getLineNumber() {
-        return lineNumber;
-    }
-
-    /**
-     * Column number of the location of the error
-     *
-     * @return the column number or -1 if unknown
-     */
-    public int getColumnNumber() {
-        return columnNumber;
-    }
-}

+ 3 - 16
server/src/test/java/org/elasticsearch/ElasticsearchExceptionTests.java

@@ -37,7 +37,6 @@ import org.elasticsearch.repositories.RepositoryException;
 import org.elasticsearch.rest.RestStatus;
 import org.elasticsearch.script.ScriptException;
 import org.elasticsearch.search.SearchContextMissingException;
-import org.elasticsearch.search.SearchParseException;
 import org.elasticsearch.search.SearchShardTarget;
 import org.elasticsearch.search.internal.ShardSearchContextId;
 import org.elasticsearch.test.ESTestCase;
@@ -46,7 +45,6 @@ import org.elasticsearch.xcontent.ToXContent;
 import org.elasticsearch.xcontent.XContent;
 import org.elasticsearch.xcontent.XContentBuilder;
 import org.elasticsearch.xcontent.XContentFactory;
-import org.elasticsearch.xcontent.XContentLocation;
 import org.elasticsearch.xcontent.XContentParseException;
 import org.elasticsearch.xcontent.XContentParser;
 import org.elasticsearch.xcontent.XContentType;
@@ -63,7 +61,6 @@ import java.util.Map;
 import static java.util.Collections.emptyList;
 import static java.util.Collections.singleton;
 import static java.util.Collections.singletonList;
-import static org.elasticsearch.test.TestSearchContext.SHARD_TARGET;
 import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertToXContentEquivalent;
 import static org.hamcrest.CoreMatchers.hasItem;
 import static org.hamcrest.CoreMatchers.hasItems;
@@ -543,12 +540,6 @@ public class ElasticsearchExceptionTests extends ESTestCase {
                   }
                 }""");
         }
-        {
-            ElasticsearchException e = new SearchParseException(SHARD_TARGET, "foo", new XContentLocation(1, 0));
-
-            assertExceptionAsJson(e, """
-                {"type":"search_parse_exception","reason":"foo","line":1,"col":0}""");
-        }
         {
             ElasticsearchException ex = new ElasticsearchException(
                 "foo",
@@ -1202,7 +1193,7 @@ public class ElasticsearchExceptionTests extends ESTestCase {
         Throwable actual;
         ElasticsearchException expected;
 
-        int type = randomIntBetween(0, 5);
+        int type = randomIntBetween(0, 4);
         switch (type) {
             case 0 -> {
                 actual = new ClusterBlockException(singleton(NoMasterBlockService.NO_MASTER_BLOCK_WRITES));
@@ -1215,17 +1206,13 @@ public class ElasticsearchExceptionTests extends ESTestCase {
                 expected = new ElasticsearchException("Elasticsearch exception [type=parsing_exception, reason=Unknown identifier]");
             }
             case 2 -> {
-                actual = new SearchParseException(SHARD_TARGET, "Parse failure", new XContentLocation(12, 98));
-                expected = new ElasticsearchException("Elasticsearch exception [type=search_parse_exception, reason=Parse failure]");
-            }
-            case 3 -> {
                 actual = new IllegalArgumentException("Closed resource", new RuntimeException("Resource"));
                 expected = new ElasticsearchException(
                     "Elasticsearch exception [type=illegal_argument_exception, reason=Closed resource]",
                     new ElasticsearchException("Elasticsearch exception [type=runtime_exception, reason=Resource]")
                 );
             }
-            case 4 -> {
+            case 3 -> {
                 actual = new SearchPhaseExecutionException(
                     "search",
                     "all shards failed",
@@ -1240,7 +1227,7 @@ public class ElasticsearchExceptionTests extends ESTestCase {
                 );
                 expected.addMetadata("es.phase", "search");
             }
-            case 5 -> {
+            case 4 -> {
                 actual = new ElasticsearchException(
                     "Parsing failed",
                     new ParsingException(9, 42, "Wrong state", new NullPointerException("Unexpected null value"))

+ 1 - 11
server/src/test/java/org/elasticsearch/ExceptionSerializationTests.java

@@ -69,7 +69,6 @@ import org.elasticsearch.rest.RestStatus;
 import org.elasticsearch.rest.action.admin.indices.AliasesNotFoundException;
 import org.elasticsearch.search.SearchContextMissingException;
 import org.elasticsearch.search.SearchException;
-import org.elasticsearch.search.SearchParseException;
 import org.elasticsearch.search.SearchShardTarget;
 import org.elasticsearch.search.aggregations.MultiBucketConsumerService;
 import org.elasticsearch.search.aggregations.UnsupportedAggregationOnDownsampledIndex;
@@ -87,7 +86,6 @@ import org.elasticsearch.transport.ConnectTransportException;
 import org.elasticsearch.transport.NoSeedNodeLeftException;
 import org.elasticsearch.transport.NoSuchRemoteClusterException;
 import org.elasticsearch.transport.TcpTransport;
-import org.elasticsearch.xcontent.XContentLocation;
 
 import java.io.EOFException;
 import java.io.FileNotFoundException;
@@ -118,7 +116,6 @@ import static java.util.Collections.emptyMap;
 import static java.util.Collections.emptySet;
 import static java.util.Collections.singleton;
 import static org.elasticsearch.cluster.routing.TestShardRouting.newShardRouting;
-import static org.elasticsearch.test.TestSearchContext.SHARD_TARGET;
 import static org.hamcrest.Matchers.equalTo;
 import static org.hamcrest.Matchers.greaterThanOrEqualTo;
 
@@ -394,13 +391,6 @@ public class ExceptionSerializationTests extends ESTestCase {
         assertArrayEquals(new String[] { "one", "two", "three" }, ex.getResourceId().toArray(new String[0]));
     }
 
-    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());
-    }
-
     public void testIllegalIndexShardStateException() throws IOException {
         ShardId id = new ShardId("foo", "_na_", 1);
         IndexShardState state = randomFrom(IndexShardState.values());
@@ -740,7 +730,7 @@ public class ExceptionSerializationTests extends ESTestCase {
         ids.put(69, org.elasticsearch.snapshots.SnapshotMissingException.class);
         ids.put(70, org.elasticsearch.action.PrimaryMissingActionException.class);
         ids.put(71, org.elasticsearch.action.FailedNodeException.class);
-        ids.put(72, org.elasticsearch.search.SearchParseException.class);
+        ids.put(72, null); // was SearchParseException, only used in tests since 7.11
         ids.put(73, org.elasticsearch.snapshots.ConcurrentSnapshotExecutionException.class);
         ids.put(74, org.elasticsearch.common.blobstore.BlobStoreException.class);
         ids.put(75, org.elasticsearch.cluster.IncompatibleClusterStateVersionException.class);