Skip to content

Commit

Permalink
Improve error handling for malformed query cursors (#3066)
Browse files Browse the repository at this point in the history
* 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]>
(cherry picked from commit 7477fd8)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
github-actions[bot] committed Oct 18, 2024
1 parent 79f9be0 commit 204536e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
11 changes: 11 additions & 0 deletions integ-test/src/test/java/org/opensearch/sql/legacy/CursorIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,17 @@ public void noPaginationWithNonJDBCFormat() throws IOException {
assertThat(rows.length, equalTo(1000));
}

@Test
public void testMalformedCursorGracefullyHandled() throws IOException {
ResponseException result =
assertThrows(
"Expected query with malformed cursor to raise error, but didn't",
ResponseException.class,
() -> executeCursorQuery("d:a11b4db33f"));
assertTrue(result.getMessage().contains("Malformed cursor"));
assertEquals(result.getResponse().getStatusLine().getStatusCode(), 400);
}

public void verifyWithAndWithoutPaginationResponse(
String sqlQuery, String cursorQuery, int fetch_size, boolean shouldFallBackToV1)
throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.opensearch.action.search.SearchResponse;
import org.opensearch.client.Client;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.core.rest.RestStatus;
import org.opensearch.rest.BytesRestResponse;
import org.opensearch.rest.RestChannel;
import org.opensearch.search.SearchHit;
Expand All @@ -30,6 +31,7 @@
import org.opensearch.sql.legacy.metrics.MetricName;
import org.opensearch.sql.legacy.metrics.Metrics;
import org.opensearch.sql.legacy.rewriter.matchtoterm.VerificationException;
import org.opensearch.sql.opensearch.response.error.ErrorMessageFactory;

public class CursorResultExecutor implements CursorRestExecutor {

Expand All @@ -52,7 +54,15 @@ public void execute(Client client, Map<String, String> params, RestChannel chann
} catch (IllegalArgumentException | JSONException e) {
Metrics.getInstance().getNumericalMetric(MetricName.FAILED_REQ_COUNT_CUS).increment();
LOG.error("Error parsing the cursor", e);
channel.sendResponse(new BytesRestResponse(channel, e));
channel.sendResponse(
new BytesRestResponse(
RestStatus.BAD_REQUEST,
"application/json; charset=UTF-8",
ErrorMessageFactory.createErrorMessage(
new IllegalArgumentException(
"Malformed cursor: unable to extract cursor information"),
RestStatus.BAD_REQUEST.getStatus())
.toString()));
} catch (OpenSearchException e) {
int status = (e.status().getStatus());
if (status > 399 && status < 500) {
Expand Down

0 comments on commit 204536e

Please sign in to comment.