Skip to content

[9.0] Update InferenceException to retain top-level message (#126345) #126405

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

Merged
merged 1 commit into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@
package org.elasticsearch.xpack.inference;

import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ElasticsearchWrapperException;
import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.rest.RestStatus;

public class InferenceException extends ElasticsearchException implements ElasticsearchWrapperException {
public class InferenceException extends ElasticsearchException {
public InferenceException(String message, Throwable cause, Object... args) {
super(message, cause, args);
}

@Override
public RestStatus status() {
// Override status so that we get the status of the cause while retaining the message of the inference exception when emitting to
// XContent
return ExceptionsHelper.status(getCause());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

package org.elasticsearch.xpack.inference;

import org.elasticsearch.ElasticsearchStatusException;
import org.elasticsearch.common.Strings;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xcontent.ToXContent;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentFactory;

import static org.hamcrest.CoreMatchers.equalTo;

public class InferenceExceptionTests extends ESTestCase {
public void testWrapException() throws Exception {
ElasticsearchStatusException cause = new ElasticsearchStatusException("test", RestStatus.BAD_REQUEST);
InferenceException testException = new InferenceException("test wrapper", cause);

XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
testException.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();

assertThat(
Strings.toString(builder),
equalTo(
"{\"type\":\"inference_exception\",\"reason\":\"test wrapper\","
+ "\"caused_by\":{\"type\":\"status_exception\",\"reason\":\"test\"}}"
)
);
assertThat(testException.status(), equalTo(RestStatus.BAD_REQUEST));
}

public void testNullCause() throws Exception {
InferenceException testException = new InferenceException("test exception", null);

XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
testException.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();

assertThat(Strings.toString(builder), equalTo("{\"type\":\"inference_exception\",\"reason\":\"test exception\"}"));
assertThat(testException.status(), equalTo(RestStatus.INTERNAL_SERVER_ERROR));
}
}