Skip to content

Commit b3219b6

Browse files
Using Transaction Result from last retry (#111)
1 parent 7cdc1c5 commit b3219b6

File tree

2 files changed

+25
-24
lines changed

2 files changed

+25
-24
lines changed

src/index.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,8 @@ class Firestore extends commonGrpc.Service {
338338
* 'Proto3 JSON' and 'Protobuf JS' encoded data.
339339
*
340340
* @private
341-
* @param {object} documentOrName - The Firestore 'Document' proto or the
342-
* resource name of a missing document.
341+
* @param {object|string} documentOrName - The Firestore 'Document' proto or
342+
* the resource name of a missing document.
343343
* @param {object=} readTime - A 'Timestamp' proto indicating the time this
344344
* document was read.
345345
* @param {string=} encoding - One of 'json' or 'protobufJS'. Applies to both
@@ -487,27 +487,27 @@ class Firestore extends commonGrpc.Service {
487487
});
488488
})
489489
.then(() => {
490-
return transaction.commit().catch(err => {
491-
if (attemptsRemaining > 0) {
490+
return transaction
491+
.commit()
492+
.then(() => result)
493+
.catch(err => {
494+
if (attemptsRemaining > 0) {
495+
Firestore.log(
496+
'Firestore.runTransaction',
497+
`Retrying transaction after error: ${JSON.stringify(err)}.`
498+
);
499+
return this.runTransaction(updateFunction, {
500+
previousTransaction: transaction,
501+
maxAttempts: attemptsRemaining,
502+
});
503+
}
492504
Firestore.log(
493505
'Firestore.runTransaction',
494-
`Retrying transaction after error: ${JSON.stringify(err)}.`
506+
'Exhausted transaction retries, returning error: %s',
507+
err
495508
);
496-
return this.runTransaction(updateFunction, {
497-
previousTransaction: transaction,
498-
maxAttempts: attemptsRemaining,
499-
});
500-
}
501-
Firestore.log(
502-
'Firestore.runTransaction',
503-
'Exhausted transaction retries, returning error: %s',
504-
err
505-
);
506-
return Promise.reject(err);
507-
});
508-
})
509-
.then(() => {
510-
return result;
509+
return Promise.reject(err);
510+
});
511511
});
512512
}
513513

test/transaction.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,16 +341,17 @@ describe('failed transactions', function() {
341341
});
342342

343343
it('retries on commit failure', function() {
344-
let err = new Error('Retryable error');
344+
let userResult = ['failure', 'failure', 'success'];
345+
let serverError = new Error('Retryable error');
345346

346347
return runTransaction(
347348
() => {
348-
return Promise.resolve('success');
349+
return Promise.resolve(userResult.shift());
349350
},
350351
begin('foo1'),
351-
commit('foo1', [], err),
352+
commit('foo1', [], serverError),
352353
begin('foo2', 'foo1'),
353-
commit('foo2', [], err),
354+
commit('foo2', [], serverError),
354355
begin('foo3', 'foo2'),
355356
commit('foo3')
356357
).then(red => {

0 commit comments

Comments
 (0)