diff --git a/src/main/java/org/opensearch/flowframework/constant/CommonValue.java b/src/main/java/org/opensearch/flowframework/common/CommonValue.java similarity index 94% rename from src/main/java/org/opensearch/flowframework/constant/CommonValue.java rename to src/main/java/org/opensearch/flowframework/common/CommonValue.java index fb5e49f65..99082b39a 100644 --- a/src/main/java/org/opensearch/flowframework/constant/CommonValue.java +++ b/src/main/java/org/opensearch/flowframework/common/CommonValue.java @@ -6,7 +6,7 @@ * this file be licensed under the Apache-2.0 license or a * compatible open source license. */ -package org.opensearch.flowframework.constant; +package org.opensearch.flowframework.common; /** * Representation of common values that are used across project diff --git a/src/main/java/org/opensearch/flowframework/function/ThrowingSupplier.java b/src/main/java/org/opensearch/flowframework/common/ThrowingSupplier.java similarity index 93% rename from src/main/java/org/opensearch/flowframework/function/ThrowingSupplier.java rename to src/main/java/org/opensearch/flowframework/common/ThrowingSupplier.java index e31268f92..efa06e642 100644 --- a/src/main/java/org/opensearch/flowframework/function/ThrowingSupplier.java +++ b/src/main/java/org/opensearch/flowframework/common/ThrowingSupplier.java @@ -6,7 +6,7 @@ * this file be licensed under the Apache-2.0 license or a * compatible open source license. */ -package org.opensearch.flowframework.function; +package org.opensearch.flowframework.common; /** * A supplier that can throw checked exception diff --git a/src/main/java/org/opensearch/flowframework/function/ThrowingSupplierWrapper.java b/src/main/java/org/opensearch/flowframework/common/ThrowingSupplierWrapper.java similarity index 96% rename from src/main/java/org/opensearch/flowframework/function/ThrowingSupplierWrapper.java rename to src/main/java/org/opensearch/flowframework/common/ThrowingSupplierWrapper.java index 4c23c7277..71398c080 100644 --- a/src/main/java/org/opensearch/flowframework/function/ThrowingSupplierWrapper.java +++ b/src/main/java/org/opensearch/flowframework/common/ThrowingSupplierWrapper.java @@ -6,7 +6,7 @@ * this file be licensed under the Apache-2.0 license or a * compatible open source license. */ -package org.opensearch.flowframework.function; +package org.opensearch.flowframework.common; import java.util.function.Supplier; diff --git a/src/main/java/org/opensearch/flowframework/exception/FlowFrameworkException.java b/src/main/java/org/opensearch/flowframework/exception/FlowFrameworkException.java index 899bbffc9..27fe82306 100644 --- a/src/main/java/org/opensearch/flowframework/exception/FlowFrameworkException.java +++ b/src/main/java/org/opensearch/flowframework/exception/FlowFrameworkException.java @@ -8,33 +8,43 @@ */ package org.opensearch.flowframework.exception; +import javax.ws.rs.core.Response; + /** * Representation of Flow Framework Exceptions */ public class FlowFrameworkException extends RuntimeException { + + private final Response.Status restStatus; /** * Constructor with error message. * * @param message message of the exception + * @param restStatus HTTP status code of the response */ - public FlowFrameworkException(String message) { + public FlowFrameworkException(String message, Response.Status restStatus) { super(message); + this.restStatus = restStatus; } /** * Constructor with specified cause. * @param cause exception cause + * @param restStatus HTTP status code of the response */ - public FlowFrameworkException(Throwable cause) { + public FlowFrameworkException(Throwable cause, Response.Status restStatus) { super(cause); + this.restStatus = restStatus; } /** * Constructor with specified error message adn cause. * @param message error message * @param cause exception cause + * @param restStatus HTTP status code of the response */ - public FlowFrameworkException(String message, Throwable cause) { + public FlowFrameworkException(String message, Throwable cause, Response.Status restStatus) { super(message, cause); + this.restStatus = restStatus; } } diff --git a/src/main/java/org/opensearch/flowframework/indices/FlowFrameworkIndex.java b/src/main/java/org/opensearch/flowframework/indices/FlowFrameworkIndex.java index d28678e2c..f85a904da 100644 --- a/src/main/java/org/opensearch/flowframework/indices/FlowFrameworkIndex.java +++ b/src/main/java/org/opensearch/flowframework/indices/FlowFrameworkIndex.java @@ -8,21 +8,21 @@ */ package org.opensearch.flowframework.indices; -import org.opensearch.flowframework.function.ThrowingSupplierWrapper; +import org.opensearch.flowframework.common.ThrowingSupplierWrapper; import java.util.function.Supplier; -import static org.opensearch.flowframework.constant.CommonValue.GLOBAL_CONTEXT_INDEX; -import static org.opensearch.flowframework.constant.CommonValue.GLOBAL_CONTEXT_INDEX_VERSION; +import static org.opensearch.flowframework.common.CommonValue.GLOBAL_CONTEXT_INDEX; +import static org.opensearch.flowframework.common.CommonValue.GLOBAL_CONTEXT_INDEX_VERSION; /** * An enumeration of Flow Framework indices */ public enum FlowFrameworkIndex { GLOBAL_CONTEXT( - GLOBAL_CONTEXT_INDEX, - ThrowingSupplierWrapper.throwingSupplierWrapper(GlobalContextHandler::getGlobalContextMappings), - GLOBAL_CONTEXT_INDEX_VERSION + GLOBAL_CONTEXT_INDEX, + ThrowingSupplierWrapper.throwingSupplierWrapper(GlobalContextHandler::getGlobalContextMappings), + GLOBAL_CONTEXT_INDEX_VERSION ); private final String indexName; @@ -46,4 +46,4 @@ public String getMapping() { public Integer getVersion() { return version; } -} +} \ No newline at end of file diff --git a/src/main/java/org/opensearch/flowframework/indices/GlobalContextHandler.java b/src/main/java/org/opensearch/flowframework/indices/GlobalContextHandler.java index 07b27a699..0e4068833 100644 --- a/src/main/java/org/opensearch/flowframework/indices/GlobalContextHandler.java +++ b/src/main/java/org/opensearch/flowframework/indices/GlobalContextHandler.java @@ -25,12 +25,13 @@ import org.opensearch.flowframework.model.Template; import org.opensearch.flowframework.workflow.CreateIndexStep; +import javax.ws.rs.core.Response; import java.io.IOException; import java.util.HashMap; import java.util.Map; -import static org.opensearch.flowframework.constant.CommonValue.GLOBAL_CONTEXT_INDEX; -import static org.opensearch.flowframework.constant.CommonValue.GLOBAL_CONTEXT_INDEX_MAPPING; +import static org.opensearch.flowframework.common.CommonValue.GLOBAL_CONTEXT_INDEX; +import static org.opensearch.flowframework.common.CommonValue.GLOBAL_CONTEXT_INDEX_MAPPING; import static org.opensearch.flowframework.workflow.CreateIndexStep.getIndexMappings; /** @@ -72,7 +73,7 @@ private void initGlobalContextIndexIfAbsent(ActionListener listener) { public void putTemplateToGlobalContext(Template template, ActionListener listener) { initGlobalContextIndexIfAbsent(ActionListener.wrap(indexCreated -> { if (!indexCreated) { - listener.onFailure(new FlowFrameworkException("No response to create global_context index")); + listener.onFailure(new FlowFrameworkException("No response to create global_context index", Response.Status.INTERNAL_SERVER_ERROR)); return; } IndexRequest request = new IndexRequest(GLOBAL_CONTEXT_INDEX); diff --git a/src/main/java/org/opensearch/flowframework/workflow/CreateIndexStep.java b/src/main/java/org/opensearch/flowframework/workflow/CreateIndexStep.java index 3065a7540..0e26ec8ee 100644 --- a/src/main/java/org/opensearch/flowframework/workflow/CreateIndexStep.java +++ b/src/main/java/org/opensearch/flowframework/workflow/CreateIndexStep.java @@ -28,6 +28,7 @@ import org.opensearch.flowframework.exception.FlowFrameworkException; import org.opensearch.flowframework.indices.FlowFrameworkIndex; +import javax.ws.rs.core.Response; import java.io.IOException; import java.net.URL; import java.util.HashMap; @@ -36,9 +37,9 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.atomic.AtomicBoolean; -import static org.opensearch.flowframework.constant.CommonValue.META; -import static org.opensearch.flowframework.constant.CommonValue.NO_SCHEMA_VERSION; -import static org.opensearch.flowframework.constant.CommonValue.SCHEMA_VERSION_FIELD; +import static org.opensearch.flowframework.common.CommonValue.META; +import static org.opensearch.flowframework.common.CommonValue.NO_SCHEMA_VERSION; +import static org.opensearch.flowframework.common.CommonValue.SCHEMA_VERSION_FIELD; /** * Step to create an index @@ -164,7 +165,7 @@ public void initIndexIfAbsent(FlowFrameworkIndex index, ActionListener internalListener.onResponse(true); } else { internalListener.onFailure( - new FlowFrameworkException("Failed to update index setting for: " + indexName) + new FlowFrameworkException("Failed to update index setting for: " + indexName, Response.Status.INTERNAL_SERVER_ERROR) ); } }, exception -> { @@ -172,7 +173,7 @@ public void initIndexIfAbsent(FlowFrameworkIndex index, ActionListener internalListener.onFailure(exception); })); } else { - internalListener.onFailure(new FlowFrameworkException("Failed to update index: " + indexName)); + internalListener.onFailure(new FlowFrameworkException("Failed to update index: " + indexName, Response.Status.INTERNAL_SERVER_ERROR)); } }, exception -> { logger.error("Failed to update index " + indexName, exception); diff --git a/src/test/java/org/opensearch/flowframework/indices/GlobalContextHandlerTests.java b/src/test/java/org/opensearch/flowframework/indices/GlobalContextHandlerTests.java index 69fb68bae..ecf4cda26 100644 --- a/src/test/java/org/opensearch/flowframework/indices/GlobalContextHandlerTests.java +++ b/src/test/java/org/opensearch/flowframework/indices/GlobalContextHandlerTests.java @@ -34,7 +34,7 @@ import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import static org.opensearch.flowframework.constant.CommonValue.GLOBAL_CONTEXT_INDEX; +import static org.opensearch.flowframework.common.CommonValue.GLOBAL_CONTEXT_INDEX; import static org.mockito.Mockito.*; public class GlobalContextHandlerTests extends OpenSearchTestCase { @@ -66,7 +66,6 @@ public void setUp() throws Exception { when(client.admin()).thenReturn(adminClient); } - @Test public void testPutTemplateToGlobalContext() throws IOException { Template template = mock(Template.class); when(template.toXContent(any(XContentBuilder.class), eq(ToXContent.EMPTY_PARAMS))).thenAnswer(invocation -> { @@ -89,7 +88,6 @@ public void testPutTemplateToGlobalContext() throws IOException { assertEquals(GLOBAL_CONTEXT_INDEX, requestCaptor.getValue().index()); } - @Test public void testStoreResponseToGlobalContext() { String documentId = "docId"; Map updatedFields = new HashMap<>(); diff --git a/src/test/java/org/opensearch/flowframework/workflow/CreateIndexStepTests.java b/src/test/java/org/opensearch/flowframework/workflow/CreateIndexStepTests.java index f02bd8a39..7aaaeacc9 100644 --- a/src/test/java/org/opensearch/flowframework/workflow/CreateIndexStepTests.java +++ b/src/test/java/org/opensearch/flowframework/workflow/CreateIndexStepTests.java @@ -34,7 +34,7 @@ import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import static org.opensearch.flowframework.constant.CommonValue.*; +import static org.opensearch.flowframework.common.CommonValue.*; import static org.mockito.Mockito.*; public class CreateIndexStepTests extends OpenSearchTestCase {