Skip to content

Commit 5c28ee8

Browse files
Passing request to the error response handler
1 parent 76ee76a commit 5c28ee8

File tree

15 files changed

+70
-12
lines changed

15 files changed

+70
-12
lines changed

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/external/http/retry/BaseResponseHandler.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import org.elasticsearch.xpack.inference.logging.ThrottlerManager;
1818

1919
import java.util.Objects;
20-
import java.util.function.Function;
20+
import java.util.function.BiFunction;
2121

2222
import static org.elasticsearch.core.Strings.format;
2323
import static org.elasticsearch.xpack.inference.external.http.HttpUtils.checkForEmptyBody;
@@ -37,17 +37,21 @@ public abstract class BaseResponseHandler implements ResponseHandler {
3737

3838
protected final String requestType;
3939
private final ResponseParser parseFunction;
40-
private final Function<HttpResult, ErrorResponse> errorParseFunction;
40+
private final BiFunction<Request, HttpResult, ErrorResponse> errorParseFunction;
4141
private final boolean canHandleStreamingResponses;
4242

43-
public BaseResponseHandler(String requestType, ResponseParser parseFunction, Function<HttpResult, ErrorResponse> errorParseFunction) {
43+
public BaseResponseHandler(
44+
String requestType,
45+
ResponseParser parseFunction,
46+
BiFunction<Request, HttpResult, ErrorResponse> errorParseFunction
47+
) {
4448
this(requestType, parseFunction, errorParseFunction, false);
4549
}
4650

4751
public BaseResponseHandler(
4852
String requestType,
4953
ResponseParser parseFunction,
50-
Function<HttpResult, ErrorResponse> errorParseFunction,
54+
BiFunction<Request, HttpResult, ErrorResponse> errorParseFunction,
5155
boolean canHandleStreamingResponses
5256
) {
5357
this.requestType = Objects.requireNonNull(requestType);
@@ -96,7 +100,7 @@ public void validateResponse(
96100
protected abstract void checkForFailureStatusCode(Request request, HttpResult result);
97101

98102
private void checkForErrorObject(Request request, HttpResult result) {
99-
var errorEntity = errorParseFunction.apply(result);
103+
var errorEntity = errorParseFunction.apply(request, result);
100104

101105
if (errorEntity.errorStructureFound()) {
102106
// We don't really know what happened because the status code was 200 so we'll return a failure and let the
@@ -109,7 +113,7 @@ private void checkForErrorObject(Request request, HttpResult result) {
109113
}
110114

111115
protected Exception buildError(String message, Request request, HttpResult result) {
112-
var errorEntityMsg = errorParseFunction.apply(result);
116+
var errorEntityMsg = errorParseFunction.apply(request, result);
113117
return buildError(message, request, result, errorEntityMsg);
114118
}
115119

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/external/response/ErrorMessageResponseEntity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.elasticsearch.xcontent.XContentType;
1414
import org.elasticsearch.xpack.inference.external.http.HttpResult;
1515
import org.elasticsearch.xpack.inference.external.http.retry.ErrorResponse;
16+
import org.elasticsearch.xpack.inference.external.request.Request;
1617

1718
import java.util.Map;
1819
import java.util.Objects;
@@ -68,4 +69,8 @@ public static ErrorResponse fromResponse(HttpResult response, String defaultMess
6869
public static ErrorResponse fromResponse(HttpResult response) {
6970
return fromResponse(response, "");
7071
}
72+
73+
public static ErrorResponse fromResponse(Request request, HttpResult response) {
74+
return fromResponse(response);
75+
}
7176
}

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/alibabacloudsearch/response/AlibabaCloudSearchErrorResponseEntity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.elasticsearch.xcontent.XContentType;
1616
import org.elasticsearch.xpack.inference.external.http.HttpResult;
1717
import org.elasticsearch.xpack.inference.external.http.retry.ErrorResponse;
18+
import org.elasticsearch.xpack.inference.external.request.Request;
1819

1920
public class AlibabaCloudSearchErrorResponseEntity extends ErrorResponse {
2021
private static final Logger logger = LogManager.getLogger(AlibabaCloudSearchErrorResponseEntity.class);
@@ -23,6 +24,10 @@ private AlibabaCloudSearchErrorResponseEntity(String errorMessage) {
2324
super(errorMessage);
2425
}
2526

27+
public static ErrorResponse fromResponse(Request request, HttpResult response) {
28+
return fromResponse(response);
29+
}
30+
2631
/**
2732
* An example error response for invalid auth would look like
2833
* <code>

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/azureopenai/response/AzureMistralOpenAiExternalResponseHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.elasticsearch.xpack.inference.services.openai.OpenAiStreamingProcessor;
2626

2727
import java.util.concurrent.Flow;
28-
import java.util.function.Function;
28+
import java.util.function.BiFunction;
2929

3030
import static org.elasticsearch.xpack.inference.external.http.HttpUtils.checkForEmptyBody;
3131
import static org.elasticsearch.xpack.inference.external.http.retry.ResponseHandlerUtils.getFirstHeaderOrUnknown;
@@ -55,7 +55,7 @@ public class AzureMistralOpenAiExternalResponseHandler extends BaseResponseHandl
5555
public AzureMistralOpenAiExternalResponseHandler(
5656
String requestType,
5757
ResponseParser parseFunction,
58-
Function<HttpResult, ErrorResponse> errorParseFunction,
58+
BiFunction<Request, HttpResult, ErrorResponse> errorParseFunction,
5959
boolean canHandleStreamingResponses
6060
) {
6161
super(requestType, parseFunction, errorParseFunction);

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/cohere/response/CohereErrorResponseEntity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,18 @@
1313
import org.elasticsearch.xcontent.XContentType;
1414
import org.elasticsearch.xpack.inference.external.http.HttpResult;
1515
import org.elasticsearch.xpack.inference.external.http.retry.ErrorResponse;
16+
import org.elasticsearch.xpack.inference.external.request.Request;
1617

1718
public class CohereErrorResponseEntity extends ErrorResponse {
1819

1920
private CohereErrorResponseEntity(String errorMessage) {
2021
super(errorMessage);
2122
}
2223

24+
public static ErrorResponse fromResponse(Request request, HttpResult response) {
25+
return fromResponse(response);
26+
}
27+
2328
/**
2429
* An example error response for invalid auth would look like
2530
* <code>

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/elastic/response/ElasticInferenceServiceErrorResponseEntity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.elasticsearch.xcontent.XContentType;
1717
import org.elasticsearch.xpack.inference.external.http.HttpResult;
1818
import org.elasticsearch.xpack.inference.external.http.retry.ErrorResponse;
19+
import org.elasticsearch.xpack.inference.external.request.Request;
1920

2021
import java.io.IOException;
2122

@@ -37,6 +38,10 @@ private ElasticInferenceServiceErrorResponseEntity(String errorMessage) {
3738
super(errorMessage);
3839
}
3940

41+
public static ErrorResponse fromResponse(Request request, HttpResult response) {
42+
return fromResponse(response);
43+
}
44+
4045
public static ErrorResponse fromResponse(HttpResult response) {
4146
return fromParser(
4247
() -> XContentFactory.xContent(XContentType.JSON).createParser(XContentParserConfiguration.EMPTY, response.body())

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/googleaistudio/response/GoogleAiStudioErrorResponseEntity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.elasticsearch.xcontent.XContentType;
1414
import org.elasticsearch.xpack.inference.external.http.HttpResult;
1515
import org.elasticsearch.xpack.inference.external.http.retry.ErrorResponse;
16+
import org.elasticsearch.xpack.inference.external.request.Request;
1617

1718
import java.util.Map;
1819
import java.util.Objects;
@@ -23,6 +24,10 @@ private GoogleAiStudioErrorResponseEntity(String errorMessage) {
2324
super(errorMessage);
2425
}
2526

27+
public static ErrorResponse fromResponse(Request request, HttpResult response) {
28+
return fromResponse(response);
29+
}
30+
2631
/**
2732
* An example error response for invalid auth would look like
2833
* <code>

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/googlevertexai/response/GoogleVertexAiErrorResponseEntity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.elasticsearch.xcontent.XContentType;
1414
import org.elasticsearch.xpack.inference.external.http.HttpResult;
1515
import org.elasticsearch.xpack.inference.external.http.retry.ErrorResponse;
16+
import org.elasticsearch.xpack.inference.external.request.Request;
1617

1718
import java.util.Map;
1819
import java.util.Objects;
@@ -23,6 +24,10 @@ private GoogleVertexAiErrorResponseEntity(String errorMessage) {
2324
super(errorMessage);
2425
}
2526

27+
public static ErrorResponse fromResponse(Request request, HttpResult response) {
28+
return fromResponse(response);
29+
}
30+
2631
/**
2732
* An example error response for invalid auth would look like
2833
* <code>

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/huggingface/response/HuggingFaceErrorResponseEntity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,18 @@
1313
import org.elasticsearch.xcontent.XContentType;
1414
import org.elasticsearch.xpack.inference.external.http.HttpResult;
1515
import org.elasticsearch.xpack.inference.external.http.retry.ErrorResponse;
16+
import org.elasticsearch.xpack.inference.external.request.Request;
1617

1718
public class HuggingFaceErrorResponseEntity extends ErrorResponse {
1819

1920
public HuggingFaceErrorResponseEntity(String message) {
2021
super(message);
2122
}
2223

24+
public static ErrorResponse fromResponse(Request request, HttpResult response) {
25+
return fromResponse(response);
26+
}
27+
2328
/**
2429
* An example error response for invalid auth would look like
2530
* <code>

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/ibmwatsonx/response/IbmWatsonxErrorResponseEntity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.elasticsearch.xcontent.XContentType;
1414
import org.elasticsearch.xpack.inference.external.http.HttpResult;
1515
import org.elasticsearch.xpack.inference.external.http.retry.ErrorResponse;
16+
import org.elasticsearch.xpack.inference.external.request.Request;
1617

1718
import java.util.Map;
1819
import java.util.Objects;
@@ -23,6 +24,10 @@ private IbmWatsonxErrorResponseEntity(String errorMessage) {
2324
super(errorMessage);
2425
}
2526

27+
public static ErrorResponse fromResponse(Request request, HttpResult response) {
28+
return fromResponse(response);
29+
}
30+
2631
@SuppressWarnings("unchecked")
2732
public static ErrorResponse fromResponse(HttpResult response) {
2833
try (

0 commit comments

Comments
 (0)