|
6 | 6 | * a bad request or a server error. |
7 | 7 | */ |
8 | 8 | public class WeaviateApiException extends RuntimeException { |
| 9 | + private final String errorMessage; |
| 10 | + private final Source source; |
9 | 11 | private final String endpoint; |
10 | | - private final int statusCode; |
| 12 | + private final Integer statusCode; |
| 13 | + private final String grpcStatus; |
11 | 14 |
|
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) { |
13 | 38 | super("HTTP %d: %s %s: %s".formatted(statusCode, method, endpoint, errorMessage)); |
| 39 | + this.source = Source.HTTP; |
| 40 | + this.errorMessage = errorMessage; |
14 | 41 | this.endpoint = endpoint; |
15 | 42 | this.statusCode = statusCode; |
| 43 | + this.grpcStatus = null; |
16 | 44 | } |
17 | 45 |
|
18 | 46 | public String endpoint() { |
19 | 47 | return endpoint; |
20 | 48 | } |
21 | 49 |
|
22 | | - public int statusCode() { |
| 50 | + public Integer statusCode() { |
23 | 51 | return statusCode; |
24 | 52 | } |
25 | 53 | } |
0 commit comments