Skip to content

Commit

Permalink
fix: add cause to transaction errors on transaction commit (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
rockwotj committed Feb 18, 2020
1 parent 4f498b4 commit 00b3c6f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}

/**
Expand Down
Expand Up @@ -360,7 +360,7 @@ public void onSuccess(final T userResult) {
@Override
public void onFailure(Throwable throwable) {
// Retry failed commits.
maybeRetry();
maybeRetry(throwable);
}

@Override
Expand Down Expand Up @@ -393,7 +393,7 @@ public void run() {
return callbackResult;
}

private void maybeRetry() {
private void maybeRetry(Throwable throwable) {
if (attemptsRemaining > 0) {
span.addAnnotation("retrying");
runTransactionAttempt(
Expand All @@ -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."));
}
}

Expand Down

0 comments on commit 00b3c6f

Please sign in to comment.