Skip to content

Commit a9f1146

Browse files
committed
fixing test and enhancing error message
Signed-off-by: Amit Galitzky <amgalitz@amazon.com>
1 parent ce78f8f commit a9f1146

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

src/main/java/org/opensearch/flowframework/rest/RestCreateWorkflowAction.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli
142142
}
143143
} catch (Exception ex) {
144144
RestStatus status = ex instanceof IOException ? RestStatus.BAD_REQUEST : ExceptionsHelper.status(ex);
145-
String errorMessage = "failure parsing request body when a use case is given";
145+
String errorMessage =
146+
"failure parsing request body when a use case is given, make sure to provide a map with values that are either strings, arrays, or map of strings to strings";
146147
logger.error(errorMessage, ex);
147148
throw new FlowFrameworkException(errorMessage, status);
148149
}

src/main/java/org/opensearch/flowframework/workflow/CreateIndexStep.java

+13-4
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,19 @@ public PlainActionFuture<WorkflowData> execute(
9393
byte[] byteArr = configurations.getBytes(StandardCharsets.UTF_8);
9494
BytesReference configurationsBytes = new BytesArray(byteArr);
9595
CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName);
96-
if (!configurations.isEmpty()) {
97-
Map<String, Object> sourceAsMap = XContentHelper.convertToMap(configurationsBytes, false, MediaTypeRegistry.JSON).v2();
98-
sourceAsMap = prepareMappings(sourceAsMap);
99-
createIndexRequest.source(sourceAsMap, LoggingDeprecationHandler.INSTANCE);
96+
97+
try {
98+
if (!configurations.isEmpty()) {
99+
Map<String, Object> sourceAsMap = XContentHelper.convertToMap(configurationsBytes, false, MediaTypeRegistry.JSON).v2();
100+
sourceAsMap = prepareMappings(sourceAsMap);
101+
createIndexRequest.source(sourceAsMap, LoggingDeprecationHandler.INSTANCE);
102+
}
103+
} catch (Exception ex) {
104+
String errorMessage = "Failed to create the index"
105+
+ indexName
106+
+ " due to incorrect mapping, make sure you aren't providing _doc in mapping";
107+
logger.error(errorMessage, ex);
108+
createIndexFuture.onFailure(new WorkflowStepException(errorMessage, RestStatus.BAD_REQUEST));
100109
}
101110

102111
client.admin().indices().create(createIndexRequest, ActionListener.wrap(acknowledgedResponse -> {

src/test/java/org/opensearch/flowframework/workflow/CreateIndexStepTests.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ public void setUp() throws Exception {
7070
this.flowFrameworkIndicesHandler = mock(FlowFrameworkIndicesHandler.class);
7171
MockitoAnnotations.openMocks(this);
7272
String configurations =
73-
"{\"settings\":{\"index\":{\"number_of_shards\":2,\"number_of_replicas\":1}},\"mappings\":{\"_doc\":{\"properties\":{\"age\":{\"type\":\"integer\"}}}},\"aliases\":{\"sample-alias1\":{}}}";
74-
73+
"{\"settings\":{\"index\":{\"number_of_shards\":2,\"number_of_replicas\":1}},\"mappings\":{\"properties\":{\"age\":{\"type\":\"integer\"}}},\"aliases\":{\"sample-alias1\":{}}}";
7574
inputData = new WorkflowData(
7675
Map.ofEntries(Map.entry(INDEX_NAME, "demo"), Map.entry(CONFIGURATIONS, configurations)),
7776
"test-id",

0 commit comments

Comments
 (0)