Skip to content

Commit 749bc36

Browse files
authored
Update InferenceException to retain top-level message (#126345) (#126405)
1 parent baa9703 commit 749bc36

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,18 @@
88
package org.elasticsearch.xpack.inference;
99

1010
import org.elasticsearch.ElasticsearchException;
11-
import org.elasticsearch.ElasticsearchWrapperException;
11+
import org.elasticsearch.ExceptionsHelper;
12+
import org.elasticsearch.rest.RestStatus;
1213

13-
public class InferenceException extends ElasticsearchException implements ElasticsearchWrapperException {
14+
public class InferenceException extends ElasticsearchException {
1415
public InferenceException(String message, Throwable cause, Object... args) {
1516
super(message, cause, args);
1617
}
1718

19+
@Override
20+
public RestStatus status() {
21+
// Override status so that we get the status of the cause while retaining the message of the inference exception when emitting to
22+
// XContent
23+
return ExceptionsHelper.status(getCause());
24+
}
1825
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.inference;
9+
10+
import org.elasticsearch.ElasticsearchStatusException;
11+
import org.elasticsearch.common.Strings;
12+
import org.elasticsearch.rest.RestStatus;
13+
import org.elasticsearch.test.ESTestCase;
14+
import org.elasticsearch.xcontent.ToXContent;
15+
import org.elasticsearch.xcontent.XContentBuilder;
16+
import org.elasticsearch.xcontent.XContentFactory;
17+
18+
import static org.hamcrest.CoreMatchers.equalTo;
19+
20+
public class InferenceExceptionTests extends ESTestCase {
21+
public void testWrapException() throws Exception {
22+
ElasticsearchStatusException cause = new ElasticsearchStatusException("test", RestStatus.BAD_REQUEST);
23+
InferenceException testException = new InferenceException("test wrapper", cause);
24+
25+
XContentBuilder builder = XContentFactory.jsonBuilder();
26+
builder.startObject();
27+
testException.toXContent(builder, ToXContent.EMPTY_PARAMS);
28+
builder.endObject();
29+
30+
assertThat(
31+
Strings.toString(builder),
32+
equalTo(
33+
"{\"type\":\"inference_exception\",\"reason\":\"test wrapper\","
34+
+ "\"caused_by\":{\"type\":\"status_exception\",\"reason\":\"test\"}}"
35+
)
36+
);
37+
assertThat(testException.status(), equalTo(RestStatus.BAD_REQUEST));
38+
}
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+
}
51+
}

0 commit comments

Comments
 (0)