Skip to content

Commit 07aabd8

Browse files
ylwu-amznamitgalitz
authored andcommitted
fixing some error message handeling (opensearch-project#1222)
Signed-off-by: Amit Galitzky <amgalitz@amazon.com> Co-authored-by: Amit Galitzky <amgalitz@amazon.com>
1 parent 863255b commit 07aabd8

File tree

5 files changed

+25
-12
lines changed

5 files changed

+25
-12
lines changed

common/src/main/java/org/opensearch/ml/common/MLCommonsClassLoader.java

+2
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ private static <T, S> S init(Map<T, Class<?>> map, T type,
259259
Throwable cause = e.getCause();
260260
if (cause instanceof MLException) {
261261
throw (MLException)cause;
262+
} else if (cause instanceof IllegalArgumentException) {
263+
throw (IllegalArgumentException) cause;
262264
} else {
263265
log.error("Failed to init instance for type " + type, e);
264266
return null;

common/src/main/java/org/opensearch/ml/common/connector/Connector.java

+18-5
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,21 @@ default void validatePayload(String payload) {
8888
}
8989

9090
static Connector fromStream(StreamInput in) throws IOException {
91-
String connectorProtocol = in.readString();
92-
return MLCommonsClassLoader.initConnector(connectorProtocol, new Object[]{connectorProtocol, in}, String.class, StreamInput.class);
91+
try {
92+
String connectorProtocol = in.readString();
93+
return MLCommonsClassLoader.initConnector(connectorProtocol, new Object[]{connectorProtocol, in}, String.class, StreamInput.class);
94+
} catch (IllegalArgumentException illegalArgumentException) {
95+
throw illegalArgumentException;
96+
}
9397
}
9498

9599
static Connector createConnector(XContentBuilder builder, String connectorProtocol) throws IOException {
96-
String jsonStr = builder.toString();
97-
return createConnector(jsonStr, connectorProtocol);
100+
try {
101+
String jsonStr = builder.toString();
102+
return createConnector(jsonStr, connectorProtocol);
103+
} catch (IllegalArgumentException illegalArgumentException) {
104+
throw illegalArgumentException;
105+
}
98106
}
99107

100108
static Connector createConnector(XContentParser parser) throws IOException {
@@ -118,7 +126,12 @@ private static Connector createConnector(String jsonStr, String connectorProtoco
118126
throw new IllegalArgumentException("connector protocol is null");
119127
}
120128
return MLCommonsClassLoader.initConnector(connectorProtocol, new Object[]{connectorProtocol, connectorParser}, String.class, XContentParser.class);
121-
}
129+
} catch (Exception ex) {
130+
if (ex instanceof IllegalArgumentException) {
131+
throw ex;
132+
}
133+
return null;
134+
}
122135
}
123136

124137
default void validateConnectorURL(List<String> urlRegexes) {

plugin/src/main/java/org/opensearch/ml/action/connector/GetConnectorTransportAction.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.opensearch.core.xcontent.XContentParser;
2424
import org.opensearch.index.IndexNotFoundException;
2525
import org.opensearch.ml.common.connector.Connector;
26-
import org.opensearch.ml.common.exception.MLResourceNotFoundException;
2726
import org.opensearch.ml.common.exception.MLValidationException;
2827
import org.opensearch.ml.common.transport.connector.MLConnectorGetAction;
2928
import org.opensearch.ml.common.transport.connector.MLConnectorGetRequest;
@@ -92,7 +91,8 @@ protected void doExecute(Task task, ActionRequest request, ActionListener<MLConn
9291
}
9392
}, e -> {
9493
if (e instanceof IndexNotFoundException) {
95-
actionListener.onFailure(new MLResourceNotFoundException("Fail to find connector"));
94+
log.error("Failed to get connector index", e);
95+
actionListener.onFailure(new IllegalArgumentException("Fail to find connector"));
9696
} else {
9797
log.error("Failed to get ML connector " + connectorId, e);
9898
actionListener.onFailure(e);

plugin/src/main/java/org/opensearch/ml/rest/RestMLCreateConnectorAction.java

-3
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client
5656
*/
5757
@VisibleForTesting
5858
MLCreateConnectorRequest getRequest(RestRequest request) throws IOException {
59-
if (!request.hasContent()) {
60-
throw new IOException("Create Connector request has empty body");
61-
}
6259
XContentParser parser = request.contentParser();
6360
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
6461
MLCreateConnectorInput mlCreateConnectorInput = MLCreateConnectorInput.parse(parser);

plugin/src/test/java/org/opensearch/ml/rest/RestMLCreateConnectorActionTests.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.mockito.ArgumentCaptor;
2626
import org.mockito.Mock;
2727
import org.opensearch.core.action.ActionListener;
28+
import org.opensearch.OpenSearchParseException;
2829
import org.opensearch.client.node.NodeClient;
2930
import org.opensearch.common.settings.Settings;
3031
import org.opensearch.core.common.Strings;
@@ -113,8 +114,8 @@ public void testPrepareRequest() throws Exception {
113114
}
114115

115116
public void testPrepareRequest_EmptyContent() throws Exception {
116-
thrown.expect(IOException.class);
117-
thrown.expectMessage("Create Connector request has empty body");
117+
thrown.expect(OpenSearchParseException.class);
118+
thrown.expectMessage("request body is required");
118119
Map<String, String> params = new HashMap<>();
119120
RestRequest request = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withParams(params).build();
120121

0 commit comments

Comments
 (0)