diff --git a/google-cloud-firestore/src/main/java/com/google/cloud/firestore/FirestoreException.java b/google-cloud-firestore/src/main/java/com/google/cloud/firestore/FirestoreException.java index da86be7d6..2aaefb0c5 100644 --- a/google-cloud-firestore/src/main/java/com/google/cloud/firestore/FirestoreException.java +++ b/google-cloud-firestore/src/main/java/com/google/cloud/firestore/FirestoreException.java @@ -28,7 +28,11 @@ public final class FirestoreException extends BaseGrpcServiceException { private Status status; private FirestoreException(String reason, Status status) { - super(reason, null, status.getCode().value(), false); + this(reason, status, null); + } + + private FirestoreException(String reason, Status status, @Nullable Throwable cause) { + super(reason, cause, status.getCode().value(), false); this.status = status; } @@ -58,7 +62,18 @@ static FirestoreException invalidState(String message, Object... params) { * @return The FirestoreException */ static FirestoreException serverRejected(Status status, String message, Object... params) { - return new FirestoreException(String.format(message, params), status); + return serverRejected(status, null, message, params); + } + + /** + * Creates a FirestoreException with the provided GRPC Status code and message in a nested + * exception. + * + * @return The FirestoreException + */ + static FirestoreException serverRejected( + Status status, @Nullable Throwable cause, String message, Object... params) { + return new FirestoreException(String.format(message, params), status, cause); } /** diff --git a/google-cloud-firestore/src/main/java/com/google/cloud/firestore/FirestoreImpl.java b/google-cloud-firestore/src/main/java/com/google/cloud/firestore/FirestoreImpl.java index cf9bfdb48..f70f28c9d 100644 --- a/google-cloud-firestore/src/main/java/com/google/cloud/firestore/FirestoreImpl.java +++ b/google-cloud-firestore/src/main/java/com/google/cloud/firestore/FirestoreImpl.java @@ -360,7 +360,7 @@ public void onSuccess(final T userResult) { @Override public void onFailure(Throwable throwable) { // Retry failed commits. - maybeRetry(); + maybeRetry(throwable); } @Override @@ -393,7 +393,7 @@ public void run() { return callbackResult; } - private void maybeRetry() { + private void maybeRetry(Throwable throwable) { if (attemptsRemaining > 0) { span.addAnnotation("retrying"); runTransactionAttempt( @@ -406,7 +406,9 @@ private void maybeRetry() { span.setStatus(TOO_MANY_RETRIES_STATUS); rejectTransaction( FirestoreException.serverRejected( - Status.ABORTED, "Transaction was cancelled because of too many retries.")); + Status.ABORTED, + throwable, + "Transaction was cancelled because of too many retries.")); } }