Skip to content

Commit 7477fd8

Browse files
authored
Improve error handling for malformed query cursors (#3066)
* Add reproducer for malformed cursor handling Signed-off-by: Simeon Widdis <[email protected]> * Handle malformed query cursors Signed-off-by: Simeon Widdis <[email protected]> * Move malformed cursor test to correct file Signed-off-by: Simeon Widdis <[email protected]> * Apply spotless Signed-off-by: Simeon Widdis <[email protected]> --------- Signed-off-by: Simeon Widdis <[email protected]>
1 parent 063015c commit 7477fd8

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

integ-test/src/test/java/org/opensearch/sql/legacy/CursorIT.java

+11
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,17 @@ public void noPaginationWithNonJDBCFormat() throws IOException {
440440
assertThat(rows.length, equalTo(1000));
441441
}
442442

443+
@Test
444+
public void testMalformedCursorGracefullyHandled() throws IOException {
445+
ResponseException result =
446+
assertThrows(
447+
"Expected query with malformed cursor to raise error, but didn't",
448+
ResponseException.class,
449+
() -> executeCursorQuery("d:a11b4db33f"));
450+
assertTrue(result.getMessage().contains("Malformed cursor"));
451+
assertEquals(result.getResponse().getStatusLine().getStatusCode(), 400);
452+
}
453+
443454
public void verifyWithAndWithoutPaginationResponse(
444455
String sqlQuery, String cursorQuery, int fetch_size, boolean shouldFallBackToV1)
445456
throws IOException {

legacy/src/main/java/org/opensearch/sql/legacy/executor/cursor/CursorResultExecutor.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.opensearch.action.search.SearchResponse;
2121
import org.opensearch.client.Client;
2222
import org.opensearch.common.unit.TimeValue;
23+
import org.opensearch.core.rest.RestStatus;
2324
import org.opensearch.rest.BytesRestResponse;
2425
import org.opensearch.rest.RestChannel;
2526
import org.opensearch.search.SearchHit;
@@ -36,6 +37,7 @@
3637
import org.opensearch.sql.legacy.pit.PointInTimeHandler;
3738
import org.opensearch.sql.legacy.pit.PointInTimeHandlerImpl;
3839
import org.opensearch.sql.legacy.rewriter.matchtoterm.VerificationException;
40+
import org.opensearch.sql.opensearch.response.error.ErrorMessageFactory;
3941

4042
public class CursorResultExecutor implements CursorRestExecutor {
4143

@@ -58,7 +60,15 @@ public void execute(Client client, Map<String, String> params, RestChannel chann
5860
} catch (IllegalArgumentException | JSONException e) {
5961
Metrics.getInstance().getNumericalMetric(MetricName.FAILED_REQ_COUNT_CUS).increment();
6062
LOG.error("Error parsing the cursor", e);
61-
channel.sendResponse(new BytesRestResponse(channel, e));
63+
channel.sendResponse(
64+
new BytesRestResponse(
65+
RestStatus.BAD_REQUEST,
66+
"application/json; charset=UTF-8",
67+
ErrorMessageFactory.createErrorMessage(
68+
new IllegalArgumentException(
69+
"Malformed cursor: unable to extract cursor information"),
70+
RestStatus.BAD_REQUEST.getStatus())
71+
.toString()));
6272
} catch (OpenSearchException e) {
6373
int status = (e.status().getStatus());
6474
if (status > 399 && status < 500) {

0 commit comments

Comments
 (0)