Skip to content
Open
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 @@ -347,8 +347,9 @@ protected <T> T getResponse(HttpRequest request, HttpResponse<?> response, Class
}
}

if (statusCode == 422) {
var validationError = readValue(body.toString(), ValidationError.class);
var validationError = statusCode == 422 ? parseValidationError(body.toString()) : null;

if (validationError != null) {
var errorText = validationError.getErrorDetails()
.stream()
.map(ValidationErrorDetail::getMessage)
Expand Down Expand Up @@ -376,6 +377,18 @@ protected <T> T getResponse(HttpRequest request, HttpResponse<?> response, Class
}
}

// A 422 from something other than docling-serve (e.g. a gateway) may not carry a validation body;
// fall back to the generic error so the status code and body are not lost to a parse failure.
private @Nullable ValidationError parseValidationError(String body) {
try {
return readValue(body, ValidationError.class);
}
catch (RuntimeException e) {
LOG.debug("422 response body is not a validation error", e);
return null;
}
}

@Override
public HealthCheckResponse health() {
return this.healthOps.health();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package ai.docling.serve.client;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import com.github.tomakehurst.wiremock.junit5.WireMockExtension;

import ai.docling.serve.api.DoclingServeApi;
import ai.docling.serve.api.validation.ValidationException;

class DoclingServeClientErrorResponseTests {
@RegisterExtension
static WireMockExtension wireMock = WireMockExtension.newInstance()
.options(wireMockConfig().dynamicPort())
.build();

private DoclingServeApi client() {
return DoclingServeApi.builder()
.baseUrl("http://localhost:%d".formatted(wireMock.getRuntimeInfo().getHttpPort()))
.build();
}

@Test
void unprocessableEntityWithValidationBodyThrowsValidationException() {
wireMock.stubFor(get(urlPathEqualTo("/health"))
.willReturn(aResponse()
.withStatus(422)
.withHeader("Content-Type", "application/json")
.withBody("{\"detail\": [{\"type\": \"missing\", \"loc\": [\"body\"], \"msg\": \"Field required\"}]}")));

assertThatThrownBy(() -> client().health())
.isInstanceOf(ValidationException.class)
.hasMessageContaining("Field required");
}

@Test
void unprocessableEntityWithNonValidationBodyKeepsStatusAndBody() {
wireMock.stubFor(get(urlPathEqualTo("/health"))
.willReturn(aResponse()
.withStatus(422)
.withHeader("Content-Type", "text/html")
.withBody("<html>Unprocessable by gateway</html>")));

assertThatThrownBy(() -> client().health())
.isInstanceOfSatisfying(DoclingServeClientException.class, e -> {
assertThat(e.getStatusCode()).isEqualTo(422);
assertThat(e.getResponseBody()).isEqualTo("<html>Unprocessable by gateway</html>");
});
}
}