Skip to content

Commit 23d5415

Browse files
authored
chore: make FirestoreException factory methods public to allow construction for testing (#477)
* Expose each factory method at @BetaApi level ensuring no guarantee about api surface * Expose getStatus() at @BetaApi up from package private @internalapi
1 parent d17e17c commit 23d5415

File tree

15 files changed

+51
-39
lines changed

15 files changed

+51
-39
lines changed

google-cloud-firestore/src/main/java/com/google/cloud/firestore/BulkCommitBatch.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public List<BatchWriteResult> apply(BatchWriteResponse batchWriteResponse) {
117117
if (code == Status.OK) {
118118
updateTime = Timestamp.fromProto(writeResult.getUpdateTime());
119119
} else {
120-
exception = FirestoreException.serverRejected(code, status.getMessage());
120+
exception = FirestoreException.forServerRejection(code, status.getMessage());
121121
}
122122
result.add(new BatchWriteResult(updateTime, exception));
123123
}

google-cloud-firestore/src/main/java/com/google/cloud/firestore/BulkWriterOptions.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,24 +130,24 @@ BulkWriterOptions build() {
130130
Double maxRate = options.getMaxOpsPerSecond();
131131

132132
if (initialRate != null && initialRate < 1) {
133-
throw FirestoreException.invalidState(
133+
throw FirestoreException.forInvalidArgument(
134134
"Value for argument 'initialOpsPerSecond' must be greater than 1, but was: "
135135
+ initialRate.intValue());
136136
}
137137

138138
if (maxRate != null && maxRate < 1) {
139-
throw FirestoreException.invalidState(
139+
throw FirestoreException.forInvalidArgument(
140140
"Value for argument 'maxOpsPerSecond' must be greater than 1, but was: "
141141
+ maxRate.intValue());
142142
}
143143

144144
if (maxRate != null && initialRate != null && initialRate > maxRate) {
145-
throw FirestoreException.invalidState(
145+
throw FirestoreException.forInvalidArgument(
146146
"'maxOpsPerSecond' cannot be less than 'initialOpsPerSecond'.");
147147
}
148148

149149
if (!options.getThrottlingEnabled() && (maxRate != null || initialRate != null)) {
150-
throw FirestoreException.invalidState(
150+
throw FirestoreException.forInvalidArgument(
151151
"Cannot set 'initialOpsPerSecond' or 'maxOpsPerSecond' when 'throttlingEnabled' is set to false.");
152152
}
153153
return options;

google-cloud-firestore/src/main/java/com/google/cloud/firestore/CollectionGroup.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public void getPartitions(
8282
request.build(), rpcContext.getClient().partitionQueryPagedCallable()));
8383
} catch (ApiException exception) {
8484
span.setStatus(Status.UNKNOWN.withDescription(exception.getMessage()));
85-
throw FirestoreException.apiException(exception);
85+
throw FirestoreException.forApiException(exception);
8686
} finally {
8787
span.end(TraceUtil.END_SPAN_OPTIONS);
8888
}

google-cloud-firestore/src/main/java/com/google/cloud/firestore/CollectionReference.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public Iterable<DocumentReference> listDocuments() {
149149
response = ApiExceptions.callAndTranslateApiException(future);
150150
} catch (ApiException exception) {
151151
span.setStatus(Status.UNKNOWN.withDescription(exception.getMessage()));
152-
throw FirestoreException.apiException(exception);
152+
throw FirestoreException.forApiException(exception);
153153
} finally {
154154
span.end(TraceUtil.END_SPAN_OPTIONS);
155155
}
@@ -217,7 +217,8 @@ public DocumentReference apply(WriteResult writeResult) {
217217
public ApiFuture<DocumentReference> add(Object pojo) {
218218
Object converted = CustomClassMapper.convertToPlainJavaTypes(pojo);
219219
if (!(converted instanceof Map)) {
220-
throw FirestoreException.invalidState("Can't set a document's data to an array or primitive");
220+
throw FirestoreException.forInvalidArgument(
221+
"Can't set a document's data to an array or primitive");
221222
}
222223
return add((Map<String, Object>) converted);
223224
}

google-cloud-firestore/src/main/java/com/google/cloud/firestore/DocumentReference.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ public Iterable<CollectionReference> listCollections() {
393393
request.build(), rpcContext.getClient().listCollectionIdsPagedCallable()));
394394
} catch (ApiException exception) {
395395
span.setStatus(Status.UNKNOWN.withDescription(exception.getMessage()));
396-
throw FirestoreException.apiException(exception);
396+
throw FirestoreException.forApiException(exception);
397397
} finally {
398398
span.end(TraceUtil.END_SPAN_OPTIONS);
399399
}

google-cloud-firestore/src/main/java/com/google/cloud/firestore/DocumentTransform.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ private static SortedMap<FieldPath, FieldTransform> extractFromMap(
7575
transforms.put(path, fieldValue.toProto(path));
7676
}
7777
} else {
78-
throw FirestoreException.invalidState(
78+
throw FirestoreException.forInvalidArgument(
7979
fieldValue.getMethodName() + " is not supported inside of an array.");
8080
}
8181
} else if (value instanceof Map) {
@@ -94,7 +94,7 @@ private static void validateArray(List<Object> values, FieldPath path) {
9494
Object value = values.get(i);
9595
path = path.append(FieldPath.of(Integer.toString(i)));
9696
if (value instanceof FieldValue) {
97-
throw FirestoreException.invalidState(
97+
throw FirestoreException.forInvalidArgument(
9898
((FieldValue) value).getMethodName() + " is not supported inside of an array.");
9999
} else if (value instanceof Map) {
100100
extractFromMap((Map<String, Object>) value, path, false);

google-cloud-firestore/src/main/java/com/google/cloud/firestore/FirestoreException.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package com.google.cloud.firestore;
1818

19-
import com.google.api.core.InternalApi;
19+
import com.google.api.core.BetaApi;
2020
import com.google.api.gax.rpc.ApiException;
2121
import com.google.cloud.grpc.BaseGrpcServiceException;
2222
import io.grpc.Status;
@@ -55,7 +55,8 @@ private FirestoreException(IOException exception, boolean retryable) {
5555
*
5656
* @return The FirestoreException
5757
*/
58-
static FirestoreException invalidState(String message, Object... params) {
58+
@BetaApi
59+
public static FirestoreException forInvalidArgument(String message, Object... params) {
5960
return new FirestoreException(String.format(message, params), Status.INVALID_ARGUMENT);
6061
}
6162

@@ -65,8 +66,10 @@ static FirestoreException invalidState(String message, Object... params) {
6566
*
6667
* @return The FirestoreException
6768
*/
68-
static FirestoreException serverRejected(Status status, String message, Object... params) {
69-
return serverRejected(status, null, message, params);
69+
@BetaApi
70+
public static FirestoreException forServerRejection(
71+
Status status, String message, Object... params) {
72+
return forServerRejection(status, null, message, params);
7073
}
7174

7275
/**
@@ -75,7 +78,8 @@ static FirestoreException serverRejected(Status status, String message, Object..
7578
*
7679
* @return The FirestoreException
7780
*/
78-
static FirestoreException serverRejected(
81+
@BetaApi
82+
public static FirestoreException forServerRejection(
7983
Status status, @Nullable Throwable cause, String message, Object... params) {
8084
return new FirestoreException(String.format(message, params), status, cause);
8185
}
@@ -85,7 +89,8 @@ static FirestoreException serverRejected(
8589
*
8690
* @return The FirestoreException
8791
*/
88-
static FirestoreException networkException(IOException exception, boolean retryable) {
92+
@BetaApi
93+
public static FirestoreException forIOException(IOException exception, boolean retryable) {
8994
return new FirestoreException(exception, retryable);
9095
}
9196

@@ -94,7 +99,8 @@ static FirestoreException networkException(IOException exception, boolean retrya
9499
*
95100
* @return The FirestoreException
96101
*/
97-
static FirestoreException apiException(ApiException exception) {
102+
@BetaApi
103+
public static FirestoreException forApiException(ApiException exception) {
98104
return new FirestoreException(exception.getMessage(), exception);
99105
}
100106

@@ -103,13 +109,14 @@ static FirestoreException apiException(ApiException exception) {
103109
*
104110
* @return The FirestoreException
105111
*/
106-
static FirestoreException apiException(ApiException exception, String message) {
112+
@BetaApi
113+
public static FirestoreException forApiException(ApiException exception, String message) {
107114
return new FirestoreException(message, exception);
108115
}
109116

110-
@InternalApi
117+
@BetaApi
111118
@Nullable
112-
Status getStatus() {
119+
public Status getStatus() {
113120
return status;
114121
}
115122
}

google-cloud-firestore/src/main/java/com/google/cloud/firestore/FirestoreOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public FirestoreRpc create(@Nonnull FirestoreOptions options) {
8888
try {
8989
return new GrpcFirestoreRpc(options);
9090
} catch (IOException e) {
91-
throw FirestoreException.networkException(e, false);
91+
throw FirestoreException.forIOException(e, false);
9292
}
9393
}
9494
}

google-cloud-firestore/src/main/java/com/google/cloud/firestore/Query.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ private Cursor createCursor(List<FieldOrder> order, Object[] fieldValues, boolea
406406
Value encodedValue = encodeValue(fieldReference, sanitizedValue);
407407

408408
if (encodedValue == null) {
409-
throw FirestoreException.invalidState(
409+
throw FirestoreException.forInvalidArgument(
410410
"Cannot use FieldValue.delete() or FieldValue.serverTimestamp() in a query boundary");
411411
}
412412
result.addValues(encodedValue);
@@ -1434,7 +1434,7 @@ private Value encodeValue(FieldPath fieldPath, Object value) {
14341434
Value encodedValue =
14351435
UserDataConverter.encodeValue(fieldPath, sanitizedObject, UserDataConverter.ARGUMENT);
14361436
if (encodedValue == null) {
1437-
throw FirestoreException.invalidState(
1437+
throw FirestoreException.forInvalidArgument(
14381438
"Cannot use Firestore sentinels in FieldFilter or cursors");
14391439
}
14401440
return encodedValue;

google-cloud-firestore/src/main/java/com/google/cloud/firestore/TransactionRunner.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,14 +236,14 @@ public ApiFuture<T> apply(Throwable throwable) {
236236
} else {
237237
span.setStatus(TOO_MANY_RETRIES_STATUS);
238238
final FirestoreException firestoreException =
239-
FirestoreException.apiException(
239+
FirestoreException.forApiException(
240240
apiException, "Transaction was cancelled because of too many retries.");
241241
return rollbackAndReject(firestoreException);
242242
}
243243
} else {
244244
span.setStatus(TraceUtil.statusFromApiException(apiException));
245245
final FirestoreException firestoreException =
246-
FirestoreException.apiException(
246+
FirestoreException.forApiException(
247247
apiException, "Transaction failed with non-retryable error");
248248
return rollbackAndReject(firestoreException);
249249
}

0 commit comments

Comments
 (0)