Skip to content

Commit

Permalink
Catch task description error (#12834)
Browse files Browse the repository at this point in the history
* Always return a task description even when it cannot be serialized.

Signed-off-by: dblock <[email protected]>

* Expect tasks to fail.

Signed-off-by: dblock <[email protected]>

* Only catch exceptions when getting description.

Signed-off-by: dblock <[email protected]>

* Added <error: ...> to mark error more clearly.

Signed-off-by: dblock <[email protected]>

---------

Signed-off-by: dblock <[email protected]>
(cherry picked from commit 0c956e8)
  • Loading branch information
dblock committed Mar 22, 2024
1 parent a93e86c commit 1e8ee68
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
package org.opensearch.action.search;

import org.opensearch.LegacyESVersion;
import org.opensearch.OpenSearchException;
import org.opensearch.Version;
import org.opensearch.action.ActionRequest;
import org.opensearch.action.ActionRequestValidationException;
Expand Down Expand Up @@ -731,7 +732,13 @@ public final String buildDescription() {
sb.append("scroll[").append(scroll.keepAlive()).append("], ");
}
if (source != null) {
sb.append("source[").append(source.toString(FORMAT_PARAMS)).append("]");
sb.append("source[");
try {
sb.append(source.toString(FORMAT_PARAMS));
} catch (final OpenSearchException ex) {
sb.append("<error: ").append(ex.getMessage()).append(">");
}
sb.append("]");
} else {
sb.append("source[]");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1825,7 +1825,7 @@ public String toString() {
public String toString(Params params) {
try {
return XContentHelper.toXContent(this, MediaTypeRegistry.JSON, params, true).utf8ToString();
} catch (IOException e) {
} catch (IOException | UnsupportedOperationException e) {
throw new OpenSearchException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import org.opensearch.common.util.ArrayUtils;
import org.opensearch.core.common.Strings;
import org.opensearch.core.tasks.TaskId;
import org.opensearch.geometry.LinearRing;
import org.opensearch.index.query.GeoShapeQueryBuilder;
import org.opensearch.index.query.QueryBuilders;
import org.opensearch.search.AbstractSearchTestCase;
import org.opensearch.search.Scroll;
Expand Down Expand Up @@ -279,6 +281,19 @@ public void testDescriptionIncludesScroll() {
);
}

public void testDescriptionOnSourceError() {
LinearRing linearRing = new LinearRing(new double[] { -25, -35, -25 }, new double[] { -25, -35, -25 });
GeoShapeQueryBuilder queryBuilder = new GeoShapeQueryBuilder("geo", linearRing);
SearchRequest request = new SearchRequest();
request.source(new SearchSourceBuilder().query(queryBuilder));
assertThat(
toDescription(request),
equalTo(
"indices[], search_type[QUERY_THEN_FETCH], source[<error: java.lang.UnsupportedOperationException: line ring cannot be serialized using GeoJson>]"
)
);
}

private String toDescription(SearchRequest request) {
return request.createTask(0, "test", SearchAction.NAME, TaskId.EMPTY_TASK_ID, emptyMap()).getDescription();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public void testProcessRelationSupport() throws Exception {
client().prepareSearch("test")
.setQuery(QueryBuilders.geoShapeQuery(defaultGeoFieldName, rectangle).relation(shapeRelation))
.get();
fail("Expected " + shapeRelation + " query relation not supported for Field [" + defaultGeoFieldName + "]");
} catch (SearchPhaseExecutionException e) {
assertThat(
e.getCause().getMessage(),
Expand All @@ -119,6 +120,7 @@ public void testQueryLine() throws Exception {

try {
client().prepareSearch("test").setQuery(QueryBuilders.geoShapeQuery(defaultGeoFieldName, line)).get();
fail("Expected field [" + defaultGeoFieldName + "] does not support LINEARRING queries");
} catch (SearchPhaseExecutionException e) {
assertThat(e.getCause().getMessage(), containsString("does not support " + GeoShapeType.LINESTRING + " queries"));
}
Expand All @@ -138,6 +140,7 @@ public void testQueryLinearRing() throws Exception {
searchRequestBuilder.setQuery(queryBuilder);
searchRequestBuilder.setIndices("test");
searchRequestBuilder.get();
fail("Expected field [" + defaultGeoFieldName + "] does not support LINEARRING queries");
} catch (SearchPhaseExecutionException e) {
assertThat(
e.getCause().getMessage(),
Expand All @@ -160,6 +163,7 @@ public void testQueryMultiLine() throws Exception {

try {
client().prepareSearch("test").setQuery(QueryBuilders.geoShapeQuery(defaultGeoFieldName, multiline)).get();
fail("Expected field [" + defaultGeoFieldName + "] does not support " + GeoShapeType.MULTILINESTRING + " queries");
} catch (Exception e) {
assertThat(e.getCause().getMessage(), containsString("does not support " + GeoShapeType.MULTILINESTRING + " queries"));
}
Expand All @@ -175,6 +179,7 @@ public void testQueryMultiPoint() throws Exception {

try {
client().prepareSearch("test").setQuery(QueryBuilders.geoShapeQuery(defaultGeoFieldName, multiPoint)).get();
fail("Expected field [" + defaultGeoFieldName + "] does not support " + GeoShapeType.MULTIPOINT + " queries");
} catch (Exception e) {
assertThat(e.getCause().getMessage(), containsString("does not support " + GeoShapeType.MULTIPOINT + " queries"));
}
Expand All @@ -190,6 +195,7 @@ public void testQueryPoint() throws Exception {

try {
client().prepareSearch("test").setQuery(QueryBuilders.geoShapeQuery(defaultGeoFieldName, point)).get();
fail("Expected field [" + defaultGeoFieldName + "] does not support " + GeoShapeType.POINT + " queries");
} catch (Exception e) {
assertThat(e.getCause().getMessage(), containsString("does not support " + GeoShapeType.POINT + " queries"));
}
Expand Down

0 comments on commit 1e8ee68

Please sign in to comment.