Skip to content

Commit 5c02d94

Browse files
Fix: Extract non-JSON error body (#33)
Before: When the inference service returns a non-JSON response, the actual error is masked by a parsing failure. ```text Caused by: java.lang.IllegalArgumentException: Could not process JSON error object Caused by: com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'dial' ``` After: The original error is preserved. ```text Caused by: com.bakdata.kserve.client.InferenceRequestException: Inference request failed: 502: dial tcp 127.0.0.1:8080: connect: connection refused ``` --------- Co-authored-by: Philipp Schirmer <philipp.schirmer@bakdata.com>
1 parent 72e8705 commit 5c02d94

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/main/java/com/bakdata/kserve/client/KServeClientV2.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ protected String extractErrorMessage(final String stringBody) {
5555
.or(() -> Optional.ofNullable(inferenceError.getDetail()))
5656
.orElseThrow(() -> new InferenceRequestException("Could not extract error message."));
5757
} catch (final JsonProcessingException e) {
58-
throw new IllegalArgumentException("Could not process JSON error object", e);
58+
log.warn("Could not parse error body as JSON: {}", stringBody, e);
59+
return stringBody;
5960
}
6061
}
6162

src/test/java/com/bakdata/kserve/client/KServeClientV2Test.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,34 @@ public MockResponse dispatch(@NotNull final RecordedRequest recordedRequest) {
137137
.hasMessage("Inference request failed: 400: Not Found");
138138
}
139139

140+
@Test
141+
void testFallbackNonJsonBody() {
142+
final Dispatcher dispatcher = new Dispatcher() {
143+
@NotNull
144+
@Override
145+
public MockResponse dispatch(@NotNull final RecordedRequest recordedRequest) {
146+
return new MockResponse.Builder()
147+
.code(404)
148+
.body("Not found")
149+
.build();
150+
}
151+
};
152+
this.mockServer.getMockWebServer().setDispatcher(dispatcher);
153+
154+
final KServeClientV2<String> client = KServeClientV2.<String>builder()
155+
.serviceBaseUrl(this.mockServer.getServiceBaseUrl())
156+
.modelName("test-model")
157+
.httpClient(KServeClient.getHttpClient(Duration.ofMillis(10000)))
158+
.build();
159+
160+
final InferenceRequest<String> fakeInferenceRequest = getFakeInferenceRequest("data");
161+
162+
this.softly.assertThatThrownBy(
163+
() -> client.makeInferenceRequest(fakeInferenceRequest, FakePrediction.class, ""))
164+
.isInstanceOf(InferenceRequestException.class)
165+
.hasMessageContaining("Not found");
166+
}
167+
140168
@Test
141169
void testRetry() throws IOException {
142170
this.mockServer.setUpForRetryTest();

0 commit comments

Comments
 (0)