Skip to content

Commit

Permalink
fixing some error message handeling (#1222)
Browse files Browse the repository at this point in the history
Signed-off-by: Amit Galitzky <[email protected]>
Co-authored-by: Amit Galitzky <[email protected]>
  • Loading branch information
2 people authored and zane-neo committed Sep 1, 2023
1 parent 863255b commit 07aabd8
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ private static <T, S> S init(Map<T, Class<?>> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<String> urlRegexes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -92,7 +91,8 @@ protected void doExecute(Task task, ActionRequest request, ActionListener<MLConn
}
}, e -> {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, String> params = new HashMap<>();
RestRequest request = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withParams(params).build();

Expand Down

0 comments on commit 07aabd8

Please sign in to comment.