Skip to content

Commit b34f414

Browse files
Adding test for lack of error parsing logic
1 parent 7e9a463 commit b34f414

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/services/custom/CustomServiceTests.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
package org.elasticsearch.xpack.inference.services.custom;
99

10+
import org.elasticsearch.ElasticsearchStatusException;
1011
import org.elasticsearch.action.support.PlainActionFuture;
12+
import org.elasticsearch.common.Strings;
1113
import org.elasticsearch.core.Nullable;
1214
import org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper;
1315
import org.elasticsearch.inference.InferenceServiceResults;
@@ -269,6 +271,42 @@ private static CustomServiceSettings.TextEmbeddingSettings getDefaultTextEmbeddi
269271
: CustomServiceSettings.TextEmbeddingSettings.NON_TEXT_EMBEDDING_TASK_TYPE_SETTINGS;
270272
}
271273

274+
public void testInfer_ReturnsAnError_WithoutParsingTheResponseBody() throws IOException {
275+
try (var service = createService(threadPool, clientManager)) {
276+
String responseJson = "error";
277+
278+
webServer.enqueue(new MockResponse().setResponseCode(400).setBody(responseJson));
279+
280+
var model = createInternalEmbeddingModel(new TextEmbeddingResponseParser("$.data[*].embedding"), getUrl(webServer));
281+
PlainActionFuture<InferenceServiceResults> listener = new PlainActionFuture<>();
282+
service.infer(
283+
model,
284+
null,
285+
null,
286+
null,
287+
List.of("test input"),
288+
false,
289+
new HashMap<>(),
290+
InputType.INTERNAL_SEARCH,
291+
InferenceAction.Request.DEFAULT_TIMEOUT,
292+
listener
293+
);
294+
295+
var exception = expectThrows(ElasticsearchStatusException.class, () -> listener.actionGet(TIMEOUT));
296+
297+
assertThat(
298+
exception.getMessage(),
299+
is(
300+
Strings.format(
301+
"Received an unsuccessful status code for request "
302+
+ "from inference entity id [inference_id] status [400]. Error message: [%s]",
303+
responseJson
304+
)
305+
)
306+
);
307+
}
308+
}
309+
272310
public void testInfer_HandlesTextEmbeddingRequest_OpenAI_Format() throws IOException {
273311
try (var service = createService(threadPool, clientManager)) {
274312
String responseJson = """

0 commit comments

Comments
 (0)