Skip to content

Commit 00b3c6f

Browse files
authored
fix: add cause to transaction errors on transaction commit (#108)
1 parent 4f498b4 commit 00b3c6f

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ public final class FirestoreException extends BaseGrpcServiceException {
2828
private Status status;
2929

3030
private FirestoreException(String reason, Status status) {
31-
super(reason, null, status.getCode().value(), false);
31+
this(reason, status, null);
32+
}
33+
34+
private FirestoreException(String reason, Status status, @Nullable Throwable cause) {
35+
super(reason, cause, status.getCode().value(), false);
3236

3337
this.status = status;
3438
}
@@ -58,7 +62,18 @@ static FirestoreException invalidState(String message, Object... params) {
5862
* @return The FirestoreException
5963
*/
6064
static FirestoreException serverRejected(Status status, String message, Object... params) {
61-
return new FirestoreException(String.format(message, params), status);
65+
return serverRejected(status, null, message, params);
66+
}
67+
68+
/**
69+
* Creates a FirestoreException with the provided GRPC Status code and message in a nested
70+
* exception.
71+
*
72+
* @return The FirestoreException
73+
*/
74+
static FirestoreException serverRejected(
75+
Status status, @Nullable Throwable cause, String message, Object... params) {
76+
return new FirestoreException(String.format(message, params), status, cause);
6277
}
6378

6479
/**

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ public void onSuccess(final T userResult) {
360360
@Override
361361
public void onFailure(Throwable throwable) {
362362
// Retry failed commits.
363-
maybeRetry();
363+
maybeRetry(throwable);
364364
}
365365

366366
@Override
@@ -393,7 +393,7 @@ public void run() {
393393
return callbackResult;
394394
}
395395

396-
private void maybeRetry() {
396+
private void maybeRetry(Throwable throwable) {
397397
if (attemptsRemaining > 0) {
398398
span.addAnnotation("retrying");
399399
runTransactionAttempt(
@@ -406,7 +406,9 @@ private void maybeRetry() {
406406
span.setStatus(TOO_MANY_RETRIES_STATUS);
407407
rejectTransaction(
408408
FirestoreException.serverRejected(
409-
Status.ABORTED, "Transaction was cancelled because of too many retries."));
409+
Status.ABORTED,
410+
throwable,
411+
"Transaction was cancelled because of too many retries."));
410412
}
411413
}
412414

0 commit comments

Comments
 (0)