Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add cause to transaction errors on transaction commit #108

Merged
merged 2 commits into from Feb 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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