Skip to content

Commit 33a194e

Browse files
committed
feat: rethrow io.grpc.StatusRuntimeException as WeaviateApiException
1 parent 5ce4736 commit 33a194e

4 files changed

Lines changed: 84 additions & 6 deletions

File tree

src/it/java/io/weaviate/integration/SearchITest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.junit.rules.TestRule;
1717

1818
import io.weaviate.ConcurrentTest;
19+
import io.weaviate.client6.v1.api.WeaviateApiException;
1920
import io.weaviate.client6.v1.api.WeaviateClient;
2021
import io.weaviate.client6.v1.api.collections.Property;
2122
import io.weaviate.client6.v1.api.collections.Vectorizers;
@@ -374,4 +375,43 @@ public void testHybrid() throws IOException {
374375
Assertions.assertThat(first.metadata().explainScore())
375376
.as("metadata::explainScore").isNotNull();
376377
}
378+
379+
@Test(expected = WeaviateApiException.class)
380+
public void testBadRequest() throws IOException {
381+
// Arrange
382+
var nsThings = ns("Things");
383+
384+
client.collections.create(nsThings,
385+
collection -> collection
386+
.properties(Property.text("name"))
387+
.vectors(Vectorizers.text2vecContextionary()));
388+
389+
var things = client.collections.use(nsThings);
390+
var balloon = things.data.insert(Map.of("name", "balloon"));
391+
392+
things.query.nearObject(balloon.uuid(), q -> q.limit(-1));
393+
}
394+
395+
@Test(expected = WeaviateApiException.class)
396+
public void testBadRequest_async() throws Throwable {
397+
// Arrange
398+
var nsThings = ns("Things");
399+
400+
try (final var async = client.async()) {
401+
async.collections.create(nsThings,
402+
collection -> collection
403+
.properties(Property.text("name"))
404+
.vectors(Vectorizers.text2vecContextionary()))
405+
.get();
406+
407+
var things = async.collections.use(nsThings);
408+
var balloon = things.data.insert(Map.of("name", "balloon")).get();
409+
410+
try {
411+
things.query.nearObject(balloon.uuid(), q -> q.limit(-1)).get();
412+
} catch (ExecutionException e) {
413+
throw e.getCause();
414+
}
415+
}
416+
}
377417
}

src/main/java/io/weaviate/client6/v1/api/WeaviateApiException.java

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,48 @@
66
* a bad request or a server error.
77
*/
88
public class WeaviateApiException extends RuntimeException {
9+
private final String errorMessage;
10+
private final Source source;
911
private final String endpoint;
10-
private final int statusCode;
12+
private final Integer statusCode;
13+
private final String grpcStatus;
1114

12-
public WeaviateApiException(String method, String endpoint, int statusCode, String errorMessage) {
15+
private enum Source {
16+
HTTP, GRPC;
17+
};
18+
19+
public static WeaviateApiException http(String method, String endpoint, int statusCode, String errorMessage) {
20+
return new WeaviateApiException(method, endpoint, statusCode, errorMessage);
21+
}
22+
23+
public static WeaviateApiException gRPC(io.grpc.StatusRuntimeException ex) {
24+
var status = ex.getStatus();
25+
return new WeaviateApiException(status.getCode().toString(), status.getDescription());
26+
}
27+
28+
private WeaviateApiException(String status, String errorMessage) {
29+
super("%s: %s".formatted(status, errorMessage));
30+
this.source = Source.GRPC;
31+
this.errorMessage = errorMessage;
32+
this.grpcStatus = status;
33+
this.endpoint = null;
34+
this.statusCode = null;
35+
}
36+
37+
private WeaviateApiException(String method, String endpoint, int statusCode, String errorMessage) {
1338
super("HTTP %d: %s %s: %s".formatted(statusCode, method, endpoint, errorMessage));
39+
this.source = Source.HTTP;
40+
this.errorMessage = errorMessage;
1441
this.endpoint = endpoint;
1542
this.statusCode = statusCode;
43+
this.grpcStatus = null;
1644
}
1745

1846
public String endpoint() {
1947
return endpoint;
2048
}
2149

22-
public int statusCode() {
50+
public Integer statusCode() {
2351
return statusCode;
2452
}
2553
}

src/main/java/io/weaviate/client6/v1/internal/grpc/DefaultGrpcTransport.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
import com.google.common.util.concurrent.ListenableFuture;
1111

1212
import io.grpc.ManagedChannel;
13+
import io.grpc.StatusRuntimeException;
1314
import io.grpc.netty.shaded.io.grpc.netty.GrpcSslContexts;
1415
import io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder;
1516
import io.grpc.netty.shaded.io.netty.handler.ssl.SslContext;
1617
import io.grpc.stub.MetadataUtils;
18+
import io.weaviate.client6.v1.api.WeaviateApiException;
1719
import io.weaviate.client6.v1.internal.grpc.protocol.WeaviateGrpc;
1820
import io.weaviate.client6.v1.internal.grpc.protocol.WeaviateGrpc.WeaviateBlockingStub;
1921
import io.weaviate.client6.v1.internal.grpc.protocol.WeaviateGrpc.WeaviateFutureStub;
@@ -48,8 +50,12 @@ public <RequestT, RequestM, ReplyM, ResponseT> ResponseT performRequest(RequestT
4850
Rpc<RequestT, RequestM, ResponseT, ReplyM> rpc) {
4951
var message = rpc.marshal(request);
5052
var method = rpc.method();
51-
var reply = method.apply(blockingStub, message);
52-
return rpc.unmarshal(reply);
53+
try {
54+
var reply = method.apply(blockingStub, message);
55+
return rpc.unmarshal(reply);
56+
} catch (io.grpc.StatusRuntimeException e) {
57+
throw WeaviateApiException.gRPC(e);
58+
}
5359
}
5460

5561
@Override
@@ -76,6 +82,10 @@ public void onSuccess(T result) {
7682

7783
@Override
7884
public void onFailure(Throwable t) {
85+
if (t instanceof StatusRuntimeException e) {
86+
completable.completeExceptionally(WeaviateApiException.gRPC(e));
87+
return;
88+
}
7989
completable.completeExceptionally(t);
8090
}
8191

src/main/java/io/weaviate/client6/v1/internal/rest/DefaultRestTransport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ private <ResponseT> ResponseT _handleResponse(Endpoint<?, ResponseT> endpoint, S
164164
int statusCode, String body) {
165165
if (endpoint.isError(statusCode)) {
166166
var message = endpoint.deserializeError(statusCode, body);
167-
throw new WeaviateApiException(method, url, statusCode, message);
167+
throw WeaviateApiException.http(method, url, statusCode, message);
168168
}
169169

170170
if (endpoint instanceof JsonEndpoint json) {

0 commit comments

Comments
 (0)