Skip to content

Commit

Permalink
return parsing exception 400 for parsing errors (opensearch-project#1603
Browse files Browse the repository at this point in the history
)

add more ut in restupdateconnector

Signed-off-by: Xun Zhang <[email protected]>
Signed-off-by: Sicheng Song <[email protected]>
  • Loading branch information
Zhangxunmt authored and b4sjoo committed Nov 13, 2023
1 parent 948b38a commit f83ef2c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.List;
import java.util.Locale;

import org.opensearch.OpenSearchParseException;
import org.opensearch.client.node.NodeClient;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.ml.common.transport.connector.MLUpdateConnectorAction;
Expand Down Expand Up @@ -59,14 +60,17 @@ private MLUpdateConnectorRequest getRequest(RestRequest request) throws IOExcept
}

if (!request.hasContent()) {
throw new IOException("Failed to update connector: Request body is empty");
throw new OpenSearchParseException("Failed to update connector: Request body is empty");
}

String connectorId = getParameterId(request, PARAMETER_CONNECTOR_ID);

XContentParser parser = request.contentParser();
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);

return MLUpdateConnectorRequest.parse(parser, connectorId);
try {
XContentParser parser = request.contentParser();
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
return MLUpdateConnectorRequest.parse(parser, connectorId);
} catch (IllegalStateException illegalStateException) {
throw new OpenSearchParseException(illegalStateException.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import static org.mockito.Mockito.when;
import static org.opensearch.ml.utils.MLExceptionUtils.REMOTE_INFERENCE_DISABLED_ERR_MSG;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -25,6 +24,7 @@
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.opensearch.OpenSearchParseException;
import org.opensearch.action.update.UpdateResponse;
import org.opensearch.client.node.NodeClient;
import org.opensearch.common.settings.Settings;
Expand Down Expand Up @@ -113,8 +113,15 @@ public void testUpdateConnectorRequest() throws Exception {
assertEquals("2", updateConnectorRequest.getUpdateContent().getVersion());
}

public void testUpdateConnectorRequestWithParsingException() throws Exception {
exceptionRule.expect(OpenSearchParseException.class);
exceptionRule.expectMessage("Can't get text on a VALUE_NULL");
RestRequest request = getRestRequestWithNullValue();
restMLUpdateConnectorAction.handleRequest(request, channel, client);
}

public void testUpdateConnectorRequestWithEmptyContent() throws Exception {
exceptionRule.expect(IOException.class);
exceptionRule.expect(OpenSearchParseException.class);
exceptionRule.expectMessage("Failed to update connector: Request body is empty");
RestRequest request = getRestRequestWithEmptyContent();
restMLUpdateConnectorAction.handleRequest(request, channel, client);
Expand Down Expand Up @@ -151,6 +158,20 @@ private RestRequest getRestRequest() {
return request;
}

private RestRequest getRestRequestWithNullValue() {
RestRequest.Method method = RestRequest.Method.POST;
String requestContent = "{\"version\":\"2\",\"description\":null}";
Map<String, String> params = new HashMap<>();
params.put("connector_id", "test_connectorId");
RestRequest request = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY)
.withMethod(method)
.withPath("/_plugins/_ml/connectors/_update/{connector_id}")
.withParams(params)
.withContent(new BytesArray(requestContent), XContentType.JSON)
.build();
return request;
}

private RestRequest getRestRequestWithEmptyContent() {
RestRequest.Method method = RestRequest.Method.POST;
Map<String, String> params = new HashMap<>();
Expand Down

0 comments on commit f83ef2c

Please sign in to comment.