Skip to content

Commit

Permalink
use OpenSearch rest status code
Browse files Browse the repository at this point in the history
Signed-off-by: Jackie Han <[email protected]>
  • Loading branch information
jackiehanyang committed Oct 6, 2023
1 parent ed5dcc7 commit 52e85f0
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 21 deletions.
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ dependencies {
implementation "org.opensearch:opensearch:${opensearch_version}"
implementation 'org.junit.jupiter:junit-jupiter:5.10.0'
implementation "com.google.guava:guava:32.1.2-jre"
implementation 'javax.ws.rs:javax.ws.rs-api:2.1.1'
api group: 'org.opensearch', name:'opensearch-ml-client', version: "${opensearch_build}"

configurations.all {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@
*/
package org.opensearch.flowframework.exception;

import javax.ws.rs.core.Response;
import org.opensearch.core.rest.RestStatus;

/**
* Representation of Flow Framework Exceptions
*/
public class FlowFrameworkException extends RuntimeException {

private final Response.Status restStatus;
private final RestStatus restStatus;

/**
* Constructor with error message.
*
* @param message message of the exception
* @param restStatus HTTP status code of the response
*/
public FlowFrameworkException(String message, Response.Status restStatus) {
public FlowFrameworkException(String message, RestStatus restStatus) {
super(message);
this.restStatus = restStatus;
}
Expand All @@ -33,7 +33,7 @@ public FlowFrameworkException(String message, Response.Status restStatus) {
* @param cause exception cause
* @param restStatus HTTP status code of the response
*/
public FlowFrameworkException(Throwable cause, Response.Status restStatus) {
public FlowFrameworkException(Throwable cause, RestStatus restStatus) {
super(cause);
this.restStatus = restStatus;
}
Expand All @@ -44,7 +44,7 @@ public FlowFrameworkException(Throwable cause, Response.Status restStatus) {
* @param cause exception cause
* @param restStatus HTTP status code of the response
*/
public FlowFrameworkException(String message, Throwable cause, Response.Status restStatus) {
public FlowFrameworkException(String message, Throwable cause, RestStatus restStatus) {
super(message, cause);
this.restStatus = restStatus;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@
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.core.rest.RestStatus.INTERNAL_SERVER_ERROR;
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;
Expand Down Expand Up @@ -74,9 +73,7 @@ private void initGlobalContextIndexIfAbsent(ActionListener<Boolean> listener) {
public void putTemplateToGlobalContext(Template template, ActionListener<IndexResponse> listener) {
initGlobalContextIndexIfAbsent(ActionListener.wrap(indexCreated -> {
if (!indexCreated) {
listener.onFailure(
new FlowFrameworkException("No response to create global_context index", Response.Status.INTERNAL_SERVER_ERROR)
);
listener.onFailure(new FlowFrameworkException("No response to create global_context index", INTERNAL_SERVER_ERROR));
return;
}
IndexRequest request = new IndexRequest(GLOBAL_CONTEXT_INDEX);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/
package org.opensearch.flowframework.workflow;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Charsets;
import com.google.common.io.Resources;
import org.apache.logging.log4j.LogManager;
Expand All @@ -28,8 +27,6 @@
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;
Expand All @@ -38,6 +35,7 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicBoolean;

import static org.opensearch.core.rest.RestStatus.INTERNAL_SERVER_ERROR;
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;
Expand Down Expand Up @@ -168,7 +166,7 @@ public void initIndexIfAbsent(FlowFrameworkIndex index, ActionListener<Boolean>
internalListener.onFailure(
new FlowFrameworkException(
"Failed to update index setting for: " + indexName,
Response.Status.INTERNAL_SERVER_ERROR
INTERNAL_SERVER_ERROR
)
);
}
Expand All @@ -178,10 +176,7 @@ public void initIndexIfAbsent(FlowFrameworkIndex index, ActionListener<Boolean>
}));
} else {
internalListener.onFailure(
new FlowFrameworkException(
"Failed to update index: " + indexName,
Response.Status.INTERNAL_SERVER_ERROR
)
new FlowFrameworkException("Failed to update index: " + indexName, INTERNAL_SERVER_ERROR)
);
}
}, exception -> {
Expand Down Expand Up @@ -227,8 +222,7 @@ public static String getIndexMappings(String mapping) throws IOException {
* @param newVersion new index mapping version
* @param listener action listener, if update index is needed, will pass true to its onResponse method
*/
@VisibleForTesting
protected void shouldUpdateIndex(String indexName, Integer newVersion, ActionListener<Boolean> listener) {
private void shouldUpdateIndex(String indexName, Integer newVersion, ActionListener<Boolean> listener) {
IndexMetadata indexMetaData = clusterService.state().getMetadata().indices().get(indexName);
if (indexMetaData == null) {
listener.onResponse(Boolean.FALSE);
Expand Down

0 comments on commit 52e85f0

Please sign in to comment.