Skip to content

Commit ea1a42f

Browse files
Wrapping string in try/catch and adding test
1 parent 6372934 commit ea1a42f

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/custom/CustomResponseHandler.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
package org.elasticsearch.xpack.inference.services.custom;
99

1010
import org.elasticsearch.ElasticsearchStatusException;
11+
import org.elasticsearch.common.Strings;
1112
import org.elasticsearch.inference.InferenceServiceResults;
1213
import org.elasticsearch.rest.RestStatus;
1314
import org.elasticsearch.xpack.inference.external.http.HttpResult;
@@ -24,9 +25,14 @@
2425
* Defines how to handle various response types returned from the custom integration.
2526
*/
2627
public class CustomResponseHandler extends BaseResponseHandler {
27-
private static final Function<HttpResult, ErrorResponse> ERROR_PARSER = (httpResult) -> new ErrorResponse(
28-
new String(httpResult.body(), StandardCharsets.UTF_8)
29-
);
28+
// default for testing
29+
static final Function<HttpResult, ErrorResponse> ERROR_PARSER = (httpResult) -> {
30+
try {
31+
return new ErrorResponse(new String(httpResult.body(), StandardCharsets.UTF_8));
32+
} catch (Exception e) {
33+
return new ErrorResponse(Strings.format("Failed to parse error response body: %s", e.getMessage()));
34+
}
35+
};
3036

3137
public CustomResponseHandler(String requestType, ResponseParser parseFunction) {
3238
super(requestType, parseFunction, ERROR_PARSER);
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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.services.custom;
9+
10+
import org.apache.http.HttpResponse;
11+
import org.apache.http.StatusLine;
12+
import org.elasticsearch.common.Strings;
13+
import org.elasticsearch.common.xcontent.XContentHelper;
14+
import org.elasticsearch.test.ESTestCase;
15+
import org.elasticsearch.xpack.inference.external.http.HttpResult;
16+
import org.elasticsearch.xpack.inference.external.http.retry.ErrorResponse;
17+
18+
import java.io.IOException;
19+
import java.nio.charset.StandardCharsets;
20+
21+
import static org.elasticsearch.xpack.inference.services.custom.CustomResponseHandler.ERROR_PARSER;
22+
import static org.hamcrest.Matchers.is;
23+
import static org.mockito.Mockito.mock;
24+
import static org.mockito.Mockito.when;
25+
26+
public class CustomResponseHandlerTests extends ESTestCase {
27+
28+
public void testErrorBodyParser() throws IOException {
29+
var expected = XContentHelper.stripWhitespace(Strings.format("""
30+
{
31+
"error": {
32+
"message": "%s",
33+
"type": "content_too_large",
34+
"param": null,
35+
"code": null
36+
}
37+
}
38+
""", "message"));
39+
40+
assertThat(ERROR_PARSER.apply(createResult(400, "message")), is(new ErrorResponse(expected)));
41+
}
42+
43+
private static HttpResult createResult(int statusCode, String message) throws IOException {
44+
var statusLine = mock(StatusLine.class);
45+
when(statusLine.getStatusCode()).thenReturn(statusCode);
46+
var httpResponse = mock(HttpResponse.class);
47+
when(httpResponse.getStatusLine()).thenReturn(statusLine);
48+
49+
String responseJson = XContentHelper.stripWhitespace(Strings.format("""
50+
{
51+
"error": {
52+
"message": "%s",
53+
"type": "content_too_large",
54+
"param": null,
55+
"code": null
56+
}
57+
}
58+
""", message));
59+
60+
return new HttpResult(httpResponse, responseJson.getBytes(StandardCharsets.UTF_8));
61+
}
62+
}

0 commit comments

Comments
 (0)