Skip to content

Commit 9693c7b

Browse files
fix: Better error message when Transaction/WriteBatch is modified after commit. (#1503)
* fix: Better error message when Transaction/WriteBatch is modified after commit. * Address feedback. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 7557d7c commit 9693c7b

File tree

4 files changed

+97
-1
lines changed

4 files changed

+97
-1
lines changed

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,10 @@ private T performCreate(
161161

162162
private void verifyNotCommitted() {
163163
Preconditions.checkState(
164-
!isCommitted(), "Cannot modify a WriteBatch that has already been committed.");
164+
!isCommitted(),
165+
String.format(
166+
"Cannot modify a %s that has already been committed.",
167+
this.getClass().getSimpleName()));
165168
}
166169

167170
/**

google-cloud-firestore/src/test/java/com/google/cloud/firestore/LocalFirestoreHelper.java

+16
Original file line numberDiff line numberDiff line change
@@ -1242,4 +1242,20 @@ public static List<String> bundleToElementList(ByteBuffer bundle) {
12421242

12431243
return result;
12441244
}
1245+
1246+
@FunctionalInterface
1247+
interface VoidFunction {
1248+
void apply();
1249+
}
1250+
1251+
static void assertException(VoidFunction voidFunction, String expectedErrorMessage) {
1252+
String errorMessage = "";
1253+
try {
1254+
voidFunction.apply();
1255+
} catch (Exception e) {
1256+
errorMessage = e.getMessage();
1257+
} finally {
1258+
assertEquals(errorMessage, expectedErrorMessage);
1259+
}
1260+
}
12451261
}

google-cloud-firestore/src/test/java/com/google/cloud/firestore/TransactionTest.java

+38
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import static com.google.cloud.firestore.LocalFirestoreHelper.rollbackResponse;
3737
import static com.google.cloud.firestore.LocalFirestoreHelper.set;
3838
import static com.google.cloud.firestore.LocalFirestoreHelper.update;
39+
import static com.google.cloud.firestore.it.ITQueryTest.map;
3940
import static com.google.common.truth.Truth.assertThat;
4041
import static org.junit.Assert.assertEquals;
4142
import static org.junit.Assert.assertNull;
@@ -959,6 +960,43 @@ public void getShouldThrowWhenInvokedAfterAWriteWithAnAggregateQuery() throws Ex
959960
assertThat(executionException.getCause()).isInstanceOf(IllegalStateException.class);
960961
}
961962

963+
@Test
964+
public void givesProperErrorMessageForCommittedTransaction() throws Exception {
965+
doReturn(beginResponse())
966+
.doReturn(commitResponse(0, 0))
967+
.when(firestoreMock)
968+
.sendRequest(
969+
requestCapture.capture(), ArgumentMatchers.<UnaryCallable<Message, Message>>any());
970+
String expectedErrorMessage = "Cannot modify a Transaction that has already been committed.";
971+
972+
DocumentReference docRef = firestoreMock.collection("foo").document("bar");
973+
974+
// Commit a transaction.
975+
Transaction t = firestoreMock.runTransaction(transaction -> transaction).get();
976+
977+
// Then run other operations in the same transaction.
978+
LocalFirestoreHelper.assertException(
979+
() -> {
980+
t.set(docRef, map("foo", "bar"));
981+
},
982+
expectedErrorMessage);
983+
LocalFirestoreHelper.assertException(
984+
() -> {
985+
t.update(docRef, map("foo", "bar"));
986+
},
987+
expectedErrorMessage);
988+
LocalFirestoreHelper.assertException(
989+
() -> {
990+
t.create(docRef, map("foo", "bar"));
991+
},
992+
expectedErrorMessage);
993+
LocalFirestoreHelper.assertException(
994+
() -> {
995+
t.delete(docRef);
996+
},
997+
expectedErrorMessage);
998+
}
999+
9621000
private ApiException exception(Status.Code code, boolean shouldRetry) {
9631001
return exception(code, "Test exception", shouldRetry);
9641002
}

google-cloud-firestore/src/test/java/com/google/cloud/firestore/WriteBatchTest.java

+39
Original file line numberDiff line numberDiff line change
@@ -341,4 +341,43 @@ public void deleteDocument() throws Exception {
341341
CommitRequest commitRequest = commitCapture.getValue();
342342
assertEquals(commit(writes.toArray(new Write[] {})), commitRequest);
343343
}
344+
345+
@Test
346+
public void throwsWhenModifyingACommittedWriteBatch() throws Exception {
347+
doReturn(commitResponse(0, 0))
348+
.when(firestoreMock)
349+
.sendRequest(
350+
commitCapture.capture(),
351+
ArgumentMatchers.<UnaryCallable<CommitRequest, CommitResponse>>any());
352+
353+
String expectedErrorMessage = "Cannot modify a WriteBatch that has already been committed.";
354+
355+
DocumentReference docRef = firestoreMock.collection("foo").document("bar");
356+
357+
// Commit a batch.
358+
WriteBatch batch = firestoreMock.batch();
359+
batch.commit().get();
360+
361+
// Then run other operations in the same batch.
362+
LocalFirestoreHelper.assertException(
363+
() -> {
364+
batch.set(docRef, map("foo", "bar"));
365+
},
366+
expectedErrorMessage);
367+
LocalFirestoreHelper.assertException(
368+
() -> {
369+
batch.update(docRef, map("foo", "bar"));
370+
},
371+
expectedErrorMessage);
372+
LocalFirestoreHelper.assertException(
373+
() -> {
374+
batch.create(docRef, map("foo", "bar"));
375+
},
376+
expectedErrorMessage);
377+
LocalFirestoreHelper.assertException(
378+
() -> {
379+
batch.delete(docRef);
380+
},
381+
expectedErrorMessage);
382+
}
344383
}

0 commit comments

Comments
 (0)