Skip to content

Commit e28e18a

Browse files
[Backport 2.16] Have FlowFrameworkException status recognized by ExceptionsHelper (#817)
Have FlowFrameworkException status recognized by ExceptionsHelper (#811) * Have FlowFrameworkException status recognized by ExceptionsHelper * Add tests --------- (cherry picked from commit 7ec848a) Signed-off-by: Daniel Widdis <[email protected]> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 349db41 commit e28e18a

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.1.0/)
2323
### Bug Fixes
2424
- Handle Not Found exceptions as successful deletions for agents and models ([#805](https://github.com/opensearch-project/flow-framework/pull/805))
2525
- Wrap CreateIndexRequest mappings in _doc key as required ([#809](https://github.com/opensearch-project/flow-framework/pull/809))
26+
- Have FlowFrameworkException status recognized by ExceptionsHelper ([#811](https://github.com/opensearch-project/flow-framework/pull/811))
2627

2728
### Infrastructure
2829
### Documentation

release-notes/opensearch-flow-framework.release-notes-2.16.0.0.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ Compatible with OpenSearch 2.16.0
1111
### Bug Fixes
1212
- Handle Not Found deprovision exceptions as successful deletions ([#805](https://github.com/opensearch-project/flow-framework/pull/805))
1313
- Wrap CreateIndexRequest mappings in _doc key as required ([#809](https://github.com/opensearch-project/flow-framework/pull/809))
14+
- Have FlowFrameworkException status recognized by ExceptionsHelper ([#811](https://github.com/opensearch-project/flow-framework/pull/811))

src/main/java/org/opensearch/flowframework/exception/FlowFrameworkException.java

+32-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
*/
99
package org.opensearch.flowframework.exception;
1010

11+
import org.opensearch.OpenSearchException;
12+
import org.opensearch.core.common.io.stream.StreamInput;
13+
import org.opensearch.core.common.io.stream.StreamOutput;
1114
import org.opensearch.core.rest.RestStatus;
1215
import org.opensearch.core.xcontent.ToXContentObject;
1316
import org.opensearch.core.xcontent.XContentBuilder;
@@ -17,7 +20,7 @@
1720
/**
1821
* Representation of Flow Framework Exceptions
1922
*/
20-
public class FlowFrameworkException extends RuntimeException implements ToXContentObject {
23+
public class FlowFrameworkException extends OpenSearchException implements ToXContentObject {
2124

2225
private static final long serialVersionUID = 1L;
2326

@@ -56,6 +59,16 @@ public FlowFrameworkException(String message, Throwable cause, RestStatus restSt
5659
this.restStatus = restStatus;
5760
}
5861

62+
/**
63+
* Read from a stream.
64+
* @param in THe input stream
65+
* @throws IOException on stream reading failure
66+
*/
67+
public FlowFrameworkException(StreamInput in) throws IOException {
68+
super(in);
69+
restStatus = RestStatus.readFrom(in);
70+
}
71+
5972
/**
6073
* Getter for restStatus.
6174
*
@@ -65,8 +78,26 @@ public RestStatus getRestStatus() {
6578
return restStatus;
6679
}
6780

81+
// Same getter but for superclass
82+
@Override
83+
public final RestStatus status() {
84+
return restStatus;
85+
}
86+
6887
@Override
6988
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
7089
return builder.startObject().field("error", this.getMessage()).endObject();
7190
}
91+
92+
@Override
93+
public void writeTo(StreamOutput out) throws IOException {
94+
super.writeTo(out);
95+
RestStatus.writeTo(out, restStatus);
96+
}
97+
98+
// Keeping toXContentObject for backwards compatibility but this is needed for overriding superclass fragment
99+
@Override
100+
public boolean isFragment() {
101+
return false;
102+
}
72103
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* The OpenSearch Contributors require contributions made to
6+
* this file be licensed under the Apache-2.0 license or a
7+
* compatible open source license.
8+
*/
9+
package org.opensearch.flowframework.exception;
10+
11+
import org.opensearch.ExceptionsHelper;
12+
import org.opensearch.OpenSearchException;
13+
import org.opensearch.common.io.stream.BytesStreamOutput;
14+
import org.opensearch.common.xcontent.json.JsonXContent;
15+
import org.opensearch.core.common.bytes.BytesReference;
16+
import org.opensearch.core.common.io.stream.BytesStreamInput;
17+
import org.opensearch.core.rest.RestStatus;
18+
import org.opensearch.core.xcontent.ToXContent;
19+
import org.opensearch.core.xcontent.XContentBuilder;
20+
import org.opensearch.test.OpenSearchTestCase;
21+
22+
import java.io.IOException;
23+
24+
public class FlowFrameworkExceptionTests extends OpenSearchTestCase {
25+
26+
public void testExceptions() {
27+
WorkflowStepException wse = new WorkflowStepException("WSE", RestStatus.OK);
28+
assertTrue(wse instanceof FlowFrameworkException);
29+
assertTrue(wse instanceof OpenSearchException);
30+
assertEquals(RestStatus.OK, ExceptionsHelper.status(wse));
31+
assertFalse(wse.isFragment());
32+
}
33+
34+
public void testSerialize() throws IOException {
35+
FlowFrameworkException ffe = new FlowFrameworkException("FFE", RestStatus.OK);
36+
assertTrue(ffe instanceof OpenSearchException);
37+
assertEquals(RestStatus.OK, ExceptionsHelper.status(ffe));
38+
39+
try (BytesStreamOutput out = new BytesStreamOutput()) {
40+
ffe.writeTo(out);
41+
try (BytesStreamInput in = new BytesStreamInput(BytesReference.toBytes(out.bytes()))) {
42+
ffe = new FlowFrameworkException(in);
43+
assertTrue(ffe instanceof OpenSearchException);
44+
assertEquals(RestStatus.OK, ExceptionsHelper.status(ffe));
45+
}
46+
}
47+
48+
XContentBuilder builder = JsonXContent.contentBuilder();
49+
assertEquals("{\"error\":\"FFE\"}", ffe.toXContent(builder, ToXContent.EMPTY_PARAMS).toString());
50+
}
51+
}

0 commit comments

Comments
 (0)