Skip to content

Commit a667584

Browse files
committed
feat: provide base WeaviateException class for ergonomics
1 parent bbb4377 commit a667584

File tree

6 files changed

+52
-11
lines changed

6 files changed

+52
-11
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616

1717
import io.weaviate.ConcurrentTest;
1818
import io.weaviate.client6.v1.api.WeaviateClient;
19+
import io.weaviate.client6.v1.api.WeaviateException;
1920
import io.weaviate.client6.v1.api.collections.Property;
2021
import io.weaviate.client6.v1.api.collections.WeaviateMetadata;
2122
import io.weaviate.client6.v1.api.collections.WeaviateObject;
22-
import io.weaviate.client6.v1.api.collections.pagination.WeaviatePaginationException;
23+
import io.weaviate.client6.v1.api.collections.pagination.PaginationException;
2324
import io.weaviate.containers.Container;
2425

2526
public class PaginationITest extends ConcurrentTest {
@@ -160,13 +161,13 @@ public void testAsyncPaginator() throws IOException, InterruptedException, Execu
160161
}
161162
}
162163

163-
@Test(expected = WeaviatePaginationException.class)
164+
@Test(expected = PaginationException.class)
164165
public void testFailedPagination() throws IOException {
165166
var things = client.collections.use("Unknown");
166167
things.paginate().forEach(System.out::println);
167168
}
168169

169-
@Test(expected = WeaviatePaginationException.class)
170+
@Test(expected = PaginationException.class)
170171
public void testFailedAsyncPagination_forEach() throws Throwable {
171172
try (final var async = client.async()) {
172173
var things = async.collections.use("Unknown");
@@ -178,7 +179,7 @@ public void testFailedAsyncPagination_forEach() throws Throwable {
178179
}
179180
}
180181

181-
@Test(expected = WeaviatePaginationException.class)
182+
@Test(expected = WeaviateException.class)
182183
public void testFailedAsyncPagination_forPage() throws Throwable {
183184
try (final var async = client.async()) {
184185
var things = async.collections.use("Unknown");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* server, but the operation did not complete successfully either either due to
66
* a bad request or a server error.
77
*/
8-
public class WeaviateApiException extends RuntimeException {
8+
public class WeaviateApiException extends WeaviateException {
99
private final String errorMessage;
1010
private final Source source;
1111
private final String endpoint;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package io.weaviate.client6.v1.api;
2+
3+
/**
4+
* WeaviateException is the base class for other library exceptions
5+
* to provide an ergonomic way of handling any Weaviate-related exceptions.
6+
*
7+
* <p>
8+
* Some parts of the API may still throw other standard exceptions, like
9+
* {@link java.io.IOException} or {@link java.lang.IllegalArgumentException},
10+
* which will not be wrapped into a WeaviateException.
11+
*
12+
* <p>
13+
* Usage:
14+
*
15+
* <pre>{@code
16+
* var thigns = client.collections.use("Things");
17+
* try {
18+
* things.paginate(...)
19+
* things.query.bm25(...);
20+
* things.aggregate.overAll(...);
21+
* } catch (WeaviateException e) {
22+
* System.out.println(e);
23+
* }
24+
* }</pre>
25+
*/
26+
public abstract class WeaviateException extends RuntimeException {
27+
public WeaviateException(String message) {
28+
super(message);
29+
}
30+
31+
public WeaviateException(Throwable cause) {
32+
super(cause);
33+
}
34+
35+
public WeaviateException(String message, Throwable cause) {
36+
super(message, cause);
37+
}
38+
}

src/main/java/io/weaviate/client6/v1/api/collections/pagination/AsyncPaginator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public AsyncPaginator(Builder<PropertiesT> builder) {
3636
return this.query.fetchObjects(fn)
3737
.handle((response, ex) -> {
3838
if (ex != null) {
39-
throw WeaviatePaginationException.after(cursor, pageSize, ex);
39+
throw PaginationException.after(cursor, pageSize, ex);
4040
}
4141
return response;
4242
})
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
package io.weaviate.client6.v1.api.collections.pagination;
22

3+
import io.weaviate.client6.v1.api.WeaviateException;
4+
35
/**
46
* WeaviatePaginationException is thrown then the client encouters an exception
57
* while fetching the next page. This exception preserves the original exception
68
* (see {@link #getCause} and the information about the last cursor and page
79
* size used (see {@link #cursor()} and {@link #pageSize()} respectively).
810
*/
9-
public class WeaviatePaginationException extends RuntimeException {
11+
public class PaginationException extends WeaviateException {
1012
private final String cursor;
1113
private final int pageSize;
1214

13-
public static WeaviatePaginationException after(String cursor, int pageSize, Throwable cause) {
14-
return new WeaviatePaginationException(cursor, pageSize, cause);
15+
public static PaginationException after(String cursor, int pageSize, Throwable cause) {
16+
return new PaginationException(cursor, pageSize, cause);
1517
}
1618

17-
private WeaviatePaginationException(String cursor, int pageSize, Throwable cause) {
19+
private PaginationException(String cursor, int pageSize, Throwable cause) {
1820
super("fetch next page, page_size=%d cursor=%s".formatted(pageSize, cursor), cause);
1921
this.cursor = cursor;
2022
this.pageSize = pageSize;

src/main/java/io/weaviate/client6/v1/api/collections/pagination/Paginator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public Spliterator<WeaviateObject<PropertiesT, Object, QueryMetadata>> spliterat
4040
try {
4141
return query.fetchObjects(fn).objects();
4242
} catch (Exception e) {
43-
throw WeaviatePaginationException.after(cursor, pageSize, e);
43+
throw PaginationException.after(cursor, pageSize, e);
4444
}
4545
});
4646
}

0 commit comments

Comments
 (0)