Skip to content

Commit 0c956e8

Browse files
authored
Catch task description error (#12834)
* Always return a task description even when it cannot be serialized. Signed-off-by: dblock <dblock@amazon.com> * Expect tasks to fail. Signed-off-by: dblock <dblock@amazon.com> * Only catch exceptions when getting description. Signed-off-by: dblock <dblock@amazon.com> * Added <error: ...> to mark error more clearly. Signed-off-by: dblock <dblock@amazon.com> --------- Signed-off-by: dblock <dblock@amazon.com>
1 parent 0c0bcd9 commit 0c956e8

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

server/src/main/java/org/opensearch/action/search/SearchRequest.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
package org.opensearch.action.search;
3434

35+
import org.opensearch.OpenSearchException;
3536
import org.opensearch.Version;
3637
import org.opensearch.action.ActionRequest;
3738
import org.opensearch.action.ActionRequestValidationException;
@@ -712,7 +713,13 @@ public final String buildDescription() {
712713
sb.append("scroll[").append(scroll.keepAlive()).append("], ");
713714
}
714715
if (source != null) {
715-
sb.append("source[").append(source.toString(FORMAT_PARAMS)).append("]");
716+
sb.append("source[");
717+
try {
718+
sb.append(source.toString(FORMAT_PARAMS));
719+
} catch (final OpenSearchException ex) {
720+
sb.append("<error: ").append(ex.getMessage()).append(">");
721+
}
722+
sb.append("]");
716723
} else {
717724
sb.append("source[]");
718725
}

server/src/main/java/org/opensearch/search/builder/SearchSourceBuilder.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1842,7 +1842,7 @@ public String toString() {
18421842
public String toString(Params params) {
18431843
try {
18441844
return XContentHelper.toXContent(this, MediaTypeRegistry.JSON, params, true).utf8ToString();
1845-
} catch (IOException e) {
1845+
} catch (IOException | UnsupportedOperationException e) {
18461846
throw new OpenSearchException(e);
18471847
}
18481848
}

server/src/test/java/org/opensearch/action/search/SearchRequestTests.java

+15
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
import org.opensearch.common.util.ArrayUtils;
4040
import org.opensearch.core.common.Strings;
4141
import org.opensearch.core.tasks.TaskId;
42+
import org.opensearch.geometry.LinearRing;
43+
import org.opensearch.index.query.GeoShapeQueryBuilder;
4244
import org.opensearch.index.query.QueryBuilders;
4345
import org.opensearch.search.AbstractSearchTestCase;
4446
import org.opensearch.search.Scroll;
@@ -269,6 +271,19 @@ public void testDescriptionIncludesScroll() {
269271
);
270272
}
271273

274+
public void testDescriptionOnSourceError() {
275+
LinearRing linearRing = new LinearRing(new double[] { -25, -35, -25 }, new double[] { -25, -35, -25 });
276+
GeoShapeQueryBuilder queryBuilder = new GeoShapeQueryBuilder("geo", linearRing);
277+
SearchRequest request = new SearchRequest();
278+
request.source(new SearchSourceBuilder().query(queryBuilder));
279+
assertThat(
280+
toDescription(request),
281+
equalTo(
282+
"indices[], search_type[QUERY_THEN_FETCH], source[<error: java.lang.UnsupportedOperationException: line ring cannot be serialized using GeoJson>]"
283+
)
284+
);
285+
}
286+
272287
private String toDescription(SearchRequest request) {
273288
return request.createTask(0, "test", SearchAction.NAME, TaskId.EMPTY_TASK_ID, emptyMap()).getDescription();
274289
}

server/src/test/java/org/opensearch/search/geo/GeoPointShapeQueryTests.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public void testProcessRelationSupport() throws Exception {
100100
client().prepareSearch("test")
101101
.setQuery(QueryBuilders.geoShapeQuery(defaultGeoFieldName, rectangle).relation(shapeRelation))
102102
.get();
103+
fail("Expected " + shapeRelation + " query relation not supported for Field [" + defaultGeoFieldName + "]");
103104
} catch (SearchPhaseExecutionException e) {
104105
assertThat(
105106
e.getCause().getMessage(),
@@ -119,6 +120,7 @@ public void testQueryLine() throws Exception {
119120

120121
try {
121122
client().prepareSearch("test").setQuery(QueryBuilders.geoShapeQuery(defaultGeoFieldName, line)).get();
123+
fail("Expected field [" + defaultGeoFieldName + "] does not support LINEARRING queries");
122124
} catch (SearchPhaseExecutionException e) {
123125
assertThat(e.getCause().getMessage(), containsString("does not support " + GeoShapeType.LINESTRING + " queries"));
124126
}
@@ -138,13 +140,12 @@ public void testQueryLinearRing() throws Exception {
138140
searchRequestBuilder.setQuery(queryBuilder);
139141
searchRequestBuilder.setIndices("test");
140142
searchRequestBuilder.get();
143+
fail("Expected field [" + defaultGeoFieldName + "] does not support LINEARRING queries");
141144
} catch (SearchPhaseExecutionException e) {
142145
assertThat(
143146
e.getCause().getMessage(),
144147
containsString("Field [" + defaultGeoFieldName + "] does not support LINEARRING queries")
145148
);
146-
} catch (UnsupportedOperationException e) {
147-
assertThat(e.getMessage(), containsString("line ring cannot be serialized using GeoJson"));
148149
}
149150
}
150151

@@ -162,6 +163,7 @@ public void testQueryMultiLine() throws Exception {
162163

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

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

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

0 commit comments

Comments
 (0)