Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Have FlowFrameworkException status recognized by ExceptionsHelper #811

Merged
merged 4 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.1.0/)

### Bug Fixes
- Handle Not Found exceptions as successful deletions for agents and models ([#805](https://github.com/opensearch-project/flow-framework/pull/805))
- Have FlowFrameworkException status recognized by ExceptionsHelper ([#811](https://github.com/opensearch-project/flow-framework/pull/811))

### Infrastructure
### Documentation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Compatible with OpenSearch 2.16.0

### Bug Fixes
- Handle Not Found deprovision exceptions as successful deletions ([#805](https://github.com/opensearch-project/flow-framework/pull/805))
- Have FlowFrameworkException status recognized by ExceptionsHelper ([#811](https://github.com/opensearch-project/flow-framework/pull/811))

### Infrastructure
- Update dependency com.fasterxml.jackson.core:jackson-core to v2.17.2 ([#760](https://github.com/opensearch-project/flow-framework/pull/760))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
*/
package org.opensearch.flowframework.exception;

import org.opensearch.OpenSearchException;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.rest.RestStatus;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;
Expand All @@ -17,7 +20,7 @@
/**
* Representation of Flow Framework Exceptions
*/
public class FlowFrameworkException extends RuntimeException implements ToXContentObject {
public class FlowFrameworkException extends OpenSearchException implements ToXContentObject {

private static final long serialVersionUID = 1L;

Expand Down Expand Up @@ -56,6 +59,16 @@ public FlowFrameworkException(String message, Throwable cause, RestStatus restSt
this.restStatus = restStatus;
}

/**
* Read from a stream.
* @param in THe input stream
* @throws IOException on stream reading failure
*/
public FlowFrameworkException(StreamInput in) throws IOException {
super(in);
restStatus = RestStatus.readFrom(in);
}

/**
* Getter for restStatus.
*
Expand All @@ -65,8 +78,26 @@ public RestStatus getRestStatus() {
return restStatus;
}

// Same getter but for superclass
@Override
public final RestStatus status() {
return restStatus;
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
return builder.startObject().field("error", this.getMessage()).endObject();
}

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
RestStatus.writeTo(out, restStatus);
}

// Keeping toXContentObject for backwards compatibility but this is needed for overriding superclass fragment
@Override
public boolean isFragment() {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.flowframework.exception;

import org.opensearch.ExceptionsHelper;
import org.opensearch.OpenSearchException;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.xcontent.json.JsonXContent;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.core.common.io.stream.BytesStreamInput;
import org.opensearch.core.rest.RestStatus;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.test.OpenSearchTestCase;

import java.io.IOException;

public class FlowFrameworkExceptionTests extends OpenSearchTestCase {

public void testExceptions() {
WorkflowStepException wse = new WorkflowStepException("WSE", RestStatus.OK);
assertTrue(wse instanceof FlowFrameworkException);
assertTrue(wse instanceof OpenSearchException);
assertEquals(RestStatus.OK, ExceptionsHelper.status(wse));
assertFalse(wse.isFragment());
}

public void testSerialize() throws IOException {
FlowFrameworkException ffe = new FlowFrameworkException("FFE", RestStatus.OK);
assertTrue(ffe instanceof OpenSearchException);
assertEquals(RestStatus.OK, ExceptionsHelper.status(ffe));

try (BytesStreamOutput out = new BytesStreamOutput()) {
ffe.writeTo(out);
try (BytesStreamInput in = new BytesStreamInput(BytesReference.toBytes(out.bytes()))) {
ffe = new FlowFrameworkException(in);
assertTrue(ffe instanceof OpenSearchException);
assertEquals(RestStatus.OK, ExceptionsHelper.status(ffe));
}
}

XContentBuilder builder = JsonXContent.contentBuilder();
assertEquals("{\"error\":\"FFE\"}", ffe.toXContent(builder, ToXContent.EMPTY_PARAMS).toString());
}
}
Loading