Skip to content
Closed
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 @@ -9,6 +9,7 @@
import io.stargate.sgv2.jsonapi.api.request.tenant.Tenant;
import io.stargate.sgv2.jsonapi.exception.RerankingProviderException;
import io.stargate.sgv2.jsonapi.exception.SchemaException;
import io.stargate.sgv2.jsonapi.exception.ServerException;
import io.stargate.sgv2.jsonapi.service.provider.ModelProvider;
import io.stargate.sgv2.jsonapi.service.reranking.configuration.RerankingProvidersConfig;
import io.stargate.sgv2.jsonapi.service.reranking.operation.RerankingProvider;
Expand Down Expand Up @@ -102,10 +103,23 @@ public Uni<BatchedRerankingResponse> rerank(
.transform(
gatewayResponse -> {
if (gatewayResponse.hasError()) {
var error = gatewayResponse.getError();
var unexpectedServerError = ServerException.Code.UNEXPECTED_SERVER_ERROR;
if (unexpectedServerError.name().equals(error.getErrorCode())) {
throw unexpectedServerError.withPreformattedMessage(error.getErrorBody());
}

// 22-Jan-2026, tatu: This is ugly. But has to be done to work around fragility
// of exception mapping
throw SchemaException.Code.valueOf(gatewayResponse.getError().getErrorCode())
.withPreformattedMessage(gatewayResponse.getError().getErrorBody());
SchemaException.Code schemaCode;
try {
schemaCode = SchemaException.Code.valueOf(error.getErrorCode());
} catch (IllegalArgumentException e) {
// Gateway codes outside SchemaException.Code (e.g. RERANKING_PROVIDER_TIMEOUT)
// must not shadow the original error body with an enum-lookup failure.
throw unexpectedServerError.withPreformattedMessage(error.getErrorBody());
}
throw schemaCode.withPreformattedMessage(error.getErrorBody());
}

return new BatchedRerankingResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.stargate.sgv2.jsonapi.TestConstants;
import io.stargate.sgv2.jsonapi.api.request.RerankingCredentials;
import io.stargate.sgv2.jsonapi.exception.SchemaException;
import io.stargate.sgv2.jsonapi.exception.ServerException;
import io.stargate.sgv2.jsonapi.service.provider.ApiModelSupport;
import io.stargate.sgv2.jsonapi.service.provider.ModelProvider;
import io.stargate.sgv2.jsonapi.service.provider.ModelType;
Expand Down Expand Up @@ -175,4 +176,89 @@ void handleError() {
assertThat(exception.code).isEqualTo(apiException.code);
});
}

@Test
void handleUnexpectedServerError() {
RerankingService rerankService = mock(RerankingService.class);
final ServerException apiException =
ServerException.Code.UNEXPECTED_SERVER_ERROR.withPreformattedMessage("Test fail");
final EmbeddingGateway.RerankingResponse gatewayResponse =
EmbeddingGateway.RerankingResponse.newBuilder()
.setError(
EmbeddingGateway.RerankingResponse.ErrorResponse.newBuilder()
.setErrorCode(apiException.code)
.setErrorBody(apiException.getMessage()))
.build();
when(rerankService.rerank(any())).thenReturn(Uni.createFrom().item(gatewayResponse));

RerankingEGWClient rerankEGWClient =
new RerankingEGWClient(
ModelProvider.NVIDIA,
MODEL_CONFIG,
testConstants.TENANT,
"default",
rerankService,
Map.of(),
TESTING_COMMAND_NAME);

Throwable result =
rerankEGWClient
.rerank(1, "apple", List.of("orange", "apple"), RERANK_CREDENTIALS)
.subscribe()
.withSubscriber(UniAssertSubscriber.create())
.awaitFailure()
.getFailure();

assertThat(result)
.isInstanceOf(ServerException.class)
.satisfies(
failure -> {
ServerException exception = (ServerException) failure;
assertThat(exception.code).isEqualTo(apiException.code);
assertThat(exception.getMessage()).isEqualTo(apiException.getMessage());
assertThat(exception.fullyQualifiedCode())
.isEqualTo("SERVER_UNEXPECTED_SERVER_ERROR");
});
}

@Test
void handleUnmappedErrorCode() {
RerankingService rerankService = mock(RerankingService.class);
final EmbeddingGateway.RerankingResponse gatewayResponse =
EmbeddingGateway.RerankingResponse.newBuilder()
.setError(
EmbeddingGateway.RerankingResponse.ErrorResponse.newBuilder()
.setErrorCode("RERANKING_PROVIDER_TIMEOUT")
.setErrorBody("Reranking provider timed out upstream"))
.build();
when(rerankService.rerank(any())).thenReturn(Uni.createFrom().item(gatewayResponse));

RerankingEGWClient rerankEGWClient =
new RerankingEGWClient(
ModelProvider.NVIDIA,
MODEL_CONFIG,
testConstants.TENANT,
"default",
rerankService,
Map.of(),
TESTING_COMMAND_NAME);

Throwable result =
rerankEGWClient
.rerank(1, "apple", List.of("orange", "apple"), RERANK_CREDENTIALS)
.subscribe()
.withSubscriber(UniAssertSubscriber.create())
.awaitFailure()
.getFailure();

assertThat(result)
.isInstanceOf(ServerException.class)
.satisfies(
failure -> {
ServerException exception = (ServerException) failure;
assertThat(exception.code)
.isEqualTo(ServerException.Code.UNEXPECTED_SERVER_ERROR.name());
assertThat(exception.getMessage()).isEqualTo("Reranking provider timed out upstream");
});
}
}
Loading