Skip to content

Commit 7cde5f3

Browse files
committed
Simplify status implementation
1 parent d0cddd1 commit 7cde5f3

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/InferenceException.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@ public InferenceException(String message, Throwable cause, Object... args) {
2020
public RestStatus status() {
2121
// Override status so that we get the status of the cause while retaining the message of the inference exception when emitting to
2222
// XContent
23-
RestStatus status = RestStatus.INTERNAL_SERVER_ERROR;
24-
Throwable cause = getCause();
25-
if (cause != null) {
26-
status = ExceptionsHelper.status(cause);
27-
}
28-
29-
return status;
23+
return ExceptionsHelper.status(getCause());
3024
}
3125
}

x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/InferenceExceptionTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,16 @@ public void testWrapException() throws Exception {
3636
);
3737
assertThat(testException.status(), equalTo(RestStatus.BAD_REQUEST));
3838
}
39+
40+
public void testNullCause() throws Exception {
41+
InferenceException testException = new InferenceException("test exception", null);
42+
43+
XContentBuilder builder = XContentFactory.jsonBuilder();
44+
builder.startObject();
45+
testException.toXContent(builder, ToXContent.EMPTY_PARAMS);
46+
builder.endObject();
47+
48+
assertThat(Strings.toString(builder), equalTo("{\"type\":\"inference_exception\",\"reason\":\"test exception\"}"));
49+
assertThat(testException.status(), equalTo(RestStatus.INTERNAL_SERVER_ERROR));
50+
}
3951
}

0 commit comments

Comments
 (0)