|
| 1 | +package io.weaviate.client6.v1.api; |
| 2 | + |
| 3 | +/** |
| 4 | + * Exception class thrown by client API message when the request's reached the |
| 5 | + * server, but the operation did not complete successfully either either due to |
| 6 | + * a bad request or a server error. |
| 7 | + */ |
| 8 | +public class WeaviateApiException extends WeaviateException { |
| 9 | + private final String errorMessage; |
| 10 | + private final Source source; |
| 11 | + private final String endpoint; |
| 12 | + private final Integer httpStatusCode; |
| 13 | + private final io.grpc.Status.Code grpcStatusCode; |
| 14 | + |
| 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(), status.getDescription()); |
| 26 | + } |
| 27 | + |
| 28 | + private WeaviateApiException(io.grpc.Status.Code code, String errorMessage) { |
| 29 | + super("%s: %s".formatted(code, errorMessage)); |
| 30 | + this.source = Source.GRPC; |
| 31 | + this.errorMessage = errorMessage; |
| 32 | + this.grpcStatusCode = code; |
| 33 | + this.endpoint = null; |
| 34 | + this.httpStatusCode = null; |
| 35 | + } |
| 36 | + |
| 37 | + private WeaviateApiException(String method, String endpoint, int statusCode, String errorMessage) { |
| 38 | + super("HTTP %d: %s %s: %s".formatted(statusCode, method, endpoint, errorMessage)); |
| 39 | + this.source = Source.HTTP; |
| 40 | + this.errorMessage = errorMessage; |
| 41 | + this.endpoint = endpoint; |
| 42 | + this.httpStatusCode = statusCode; |
| 43 | + this.grpcStatusCode = null; |
| 44 | + } |
| 45 | + |
| 46 | + public boolean isGPRC() { |
| 47 | + return source == Source.GRPC; |
| 48 | + } |
| 49 | + |
| 50 | + public String grpcStatusCode() { |
| 51 | + return grpcStatusCode.toString(); |
| 52 | + } |
| 53 | + |
| 54 | + public boolean isHTTP() { |
| 55 | + return source == Source.HTTP; |
| 56 | + } |
| 57 | + |
| 58 | + public String endpoint() { |
| 59 | + return endpoint; |
| 60 | + } |
| 61 | + |
| 62 | + public Integer httpStatusCode() { |
| 63 | + return httpStatusCode; |
| 64 | + } |
| 65 | + |
| 66 | + public String getError() { |
| 67 | + return errorMessage; |
| 68 | + } |
| 69 | +} |
0 commit comments