|
16 | 16 |
|
17 | 17 | package com.google.cloud.firestore; |
18 | 18 |
|
19 | | -import com.google.cloud.http.BaseHttpServiceException; |
20 | | -import com.google.common.collect.ImmutableSet; |
| 19 | +import com.google.api.gax.rpc.ApiException; |
| 20 | +import com.google.cloud.grpc.BaseGrpcServiceException; |
| 21 | +import io.grpc.Status; |
21 | 22 | import java.io.IOException; |
22 | | -import java.util.Set; |
23 | 23 |
|
24 | 24 | /** A Firestore Service exception. */ |
25 | | -public final class FirestoreException extends BaseHttpServiceException { |
| 25 | +public final class FirestoreException extends BaseGrpcServiceException { |
26 | 26 |
|
27 | | - private static final Set<Error> RETRYABLE_ERRORS = |
28 | | - ImmutableSet.of( |
29 | | - new Error(10, "ABORTED", false), |
30 | | - new Error(4, "DEADLINE_EXCEEDED", false), |
31 | | - new Error(14, "UNAVAILABLE", true)); |
32 | | - private static final long serialVersionUID = 9100921023984662143L; |
33 | | - |
34 | | - private FirestoreException(int code, String message, String reason) { |
35 | | - this(code, message, reason, true, null); |
| 27 | + private FirestoreException(String reason, Status status) { |
| 28 | + super(reason, null, status.getCode().value(), false); |
36 | 29 | } |
37 | 30 |
|
38 | | - private FirestoreException( |
39 | | - int code, String message, String reason, boolean idempotent, Throwable cause) { |
40 | | - super(code, message, reason, idempotent, RETRYABLE_ERRORS, cause); |
| 31 | + private FirestoreException(IOException exception, boolean retryable) { |
| 32 | + super(exception, retryable); |
41 | 33 | } |
42 | 34 |
|
43 | | - private FirestoreException(IOException exception) { |
44 | | - super(exception, true, RETRYABLE_ERRORS); |
| 35 | + private FirestoreException(ApiException exception) { |
| 36 | + super(exception); |
45 | 37 | } |
46 | 38 |
|
47 | 39 | /** |
48 | | - * Create a FirestoreException with {@code FAILED_PRECONDITION} reason and the {@code message} in |
49 | | - * a nested exception. |
| 40 | + * Creates a FirestoreException with an {@code INVALID_ARGUMENT} status code and the provided |
| 41 | + * message in a nested exception. |
50 | 42 | * |
51 | 43 | * @return The FirestoreException |
52 | 44 | */ |
53 | 45 | static FirestoreException invalidState(String message, Object... params) { |
54 | | - return new FirestoreException( |
55 | | - UNKNOWN_CODE, String.format(message, params), "FAILED_PRECONDITION"); |
| 46 | + return new FirestoreException(String.format(message, params), Status.INVALID_ARGUMENT); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Creates a FirestoreException with the provided GRPC Status code and message in a nested |
| 51 | + * exception. |
| 52 | + * |
| 53 | + * @return The FirestoreException |
| 54 | + */ |
| 55 | + static FirestoreException serverRejected(Status status, String message, Object... params) { |
| 56 | + return new FirestoreException(String.format(message, params), status); |
56 | 57 | } |
57 | 58 |
|
58 | 59 | /** |
59 | | - * Create a FirestoreException with {@code FAILED_PRECONDITION} reason and the {@code message} in |
60 | | - * a nested exception. |
| 60 | + * Creates a FirestoreException from an IOException. |
61 | 61 | * |
62 | 62 | * @return The FirestoreException |
63 | 63 | */ |
64 | | - static FirestoreException serverRejected(String message, Object... params) { |
65 | | - return new FirestoreException(UNKNOWN_CODE, String.format(message, params), "CANCELLED"); |
| 64 | + static FirestoreException networkException(IOException exception, boolean retryable) { |
| 65 | + return new FirestoreException(exception, retryable); |
66 | 66 | } |
67 | 67 |
|
68 | 68 | /** |
69 | | - * Create a FirestoreException with {@code FAILED_PRECONDITION} reason and the {@code message} in |
70 | | - * a nested exception. |
| 69 | + * Creates a FirestoreException from an ApiException. |
71 | 70 | * |
72 | 71 | * @return The FirestoreException |
73 | 72 | */ |
74 | | - static FirestoreException networkException(IOException exception) { |
| 73 | + static FirestoreException apiException(ApiException exception) { |
75 | 74 | return new FirestoreException(exception); |
76 | 75 | } |
77 | 76 | } |
0 commit comments