diff --git a/common/src/main/java/org/opensearch/ml/common/MLCommonsClassLoader.java b/common/src/main/java/org/opensearch/ml/common/MLCommonsClassLoader.java index efe6e2c39d..8f3e537e68 100644 --- a/common/src/main/java/org/opensearch/ml/common/MLCommonsClassLoader.java +++ b/common/src/main/java/org/opensearch/ml/common/MLCommonsClassLoader.java @@ -259,6 +259,8 @@ private static S init(Map> map, T type, Throwable cause = e.getCause(); if (cause instanceof MLException) { throw (MLException)cause; + } else if (cause instanceof IllegalArgumentException) { + throw (IllegalArgumentException) cause; } else { log.error("Failed to init instance for type " + type, e); return null; diff --git a/common/src/main/java/org/opensearch/ml/common/connector/Connector.java b/common/src/main/java/org/opensearch/ml/common/connector/Connector.java index 70b180115e..81278ceb89 100644 --- a/common/src/main/java/org/opensearch/ml/common/connector/Connector.java +++ b/common/src/main/java/org/opensearch/ml/common/connector/Connector.java @@ -88,13 +88,21 @@ default void validatePayload(String payload) { } static Connector fromStream(StreamInput in) throws IOException { - String connectorProtocol = in.readString(); - return MLCommonsClassLoader.initConnector(connectorProtocol, new Object[]{connectorProtocol, in}, String.class, StreamInput.class); + try { + String connectorProtocol = in.readString(); + return MLCommonsClassLoader.initConnector(connectorProtocol, new Object[]{connectorProtocol, in}, String.class, StreamInput.class); + } catch (IllegalArgumentException illegalArgumentException) { + throw illegalArgumentException; + } } static Connector createConnector(XContentBuilder builder, String connectorProtocol) throws IOException { - String jsonStr = builder.toString(); - return createConnector(jsonStr, connectorProtocol); + try { + String jsonStr = builder.toString(); + return createConnector(jsonStr, connectorProtocol); + } catch (IllegalArgumentException illegalArgumentException) { + throw illegalArgumentException; + } } static Connector createConnector(XContentParser parser) throws IOException { @@ -118,7 +126,12 @@ private static Connector createConnector(String jsonStr, String connectorProtoco throw new IllegalArgumentException("connector protocol is null"); } return MLCommonsClassLoader.initConnector(connectorProtocol, new Object[]{connectorProtocol, connectorParser}, String.class, XContentParser.class); - } + } catch (Exception ex) { + if (ex instanceof IllegalArgumentException) { + throw ex; + } + return null; + } } default void validateConnectorURL(List urlRegexes) { diff --git a/plugin/src/main/java/org/opensearch/ml/action/connector/GetConnectorTransportAction.java b/plugin/src/main/java/org/opensearch/ml/action/connector/GetConnectorTransportAction.java index 8a1a7368fe..841a17ea65 100644 --- a/plugin/src/main/java/org/opensearch/ml/action/connector/GetConnectorTransportAction.java +++ b/plugin/src/main/java/org/opensearch/ml/action/connector/GetConnectorTransportAction.java @@ -23,7 +23,6 @@ import org.opensearch.core.xcontent.XContentParser; import org.opensearch.index.IndexNotFoundException; import org.opensearch.ml.common.connector.Connector; -import org.opensearch.ml.common.exception.MLResourceNotFoundException; import org.opensearch.ml.common.exception.MLValidationException; import org.opensearch.ml.common.transport.connector.MLConnectorGetAction; import org.opensearch.ml.common.transport.connector.MLConnectorGetRequest; @@ -92,7 +91,8 @@ protected void doExecute(Task task, ActionRequest request, ActionListener { if (e instanceof IndexNotFoundException) { - actionListener.onFailure(new MLResourceNotFoundException("Fail to find connector")); + log.error("Failed to get connector index", e); + actionListener.onFailure(new IllegalArgumentException("Fail to find connector")); } else { log.error("Failed to get ML connector " + connectorId, e); actionListener.onFailure(e); diff --git a/plugin/src/main/java/org/opensearch/ml/rest/RestMLCreateConnectorAction.java b/plugin/src/main/java/org/opensearch/ml/rest/RestMLCreateConnectorAction.java index 662e635505..6f004296df 100644 --- a/plugin/src/main/java/org/opensearch/ml/rest/RestMLCreateConnectorAction.java +++ b/plugin/src/main/java/org/opensearch/ml/rest/RestMLCreateConnectorAction.java @@ -56,9 +56,6 @@ public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client */ @VisibleForTesting MLCreateConnectorRequest getRequest(RestRequest request) throws IOException { - if (!request.hasContent()) { - throw new IOException("Create Connector request has empty body"); - } XContentParser parser = request.contentParser(); ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser); MLCreateConnectorInput mlCreateConnectorInput = MLCreateConnectorInput.parse(parser); diff --git a/plugin/src/test/java/org/opensearch/ml/rest/RestMLCreateConnectorActionTests.java b/plugin/src/test/java/org/opensearch/ml/rest/RestMLCreateConnectorActionTests.java index 36bb59a96a..8ec9f103f8 100644 --- a/plugin/src/test/java/org/opensearch/ml/rest/RestMLCreateConnectorActionTests.java +++ b/plugin/src/test/java/org/opensearch/ml/rest/RestMLCreateConnectorActionTests.java @@ -25,6 +25,7 @@ import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.opensearch.core.action.ActionListener; +import org.opensearch.OpenSearchParseException; import org.opensearch.client.node.NodeClient; import org.opensearch.common.settings.Settings; import org.opensearch.core.common.Strings; @@ -113,8 +114,8 @@ public void testPrepareRequest() throws Exception { } public void testPrepareRequest_EmptyContent() throws Exception { - thrown.expect(IOException.class); - thrown.expectMessage("Create Connector request has empty body"); + thrown.expect(OpenSearchParseException.class); + thrown.expectMessage("request body is required"); Map params = new HashMap<>(); RestRequest request = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withParams(params).build();