Skip to content

Retry all non-Firestore exceptions #2288

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

Merged
merged 3 commits into from
Oct 22, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions packages/firestore/src/local/simple_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,23 +294,26 @@ export class SimpleDb {
// caller.
await transaction.completionPromise;
return transactionFnResult;
} catch (e) {
} catch (error) {
// TODO(schmidt-sebastian): We could probably be smarter about this and
// not retry exceptions that are likely unrecoverable (such as quota
// exceeded errors).

// Note: We cannot use an instanceof check for FirestoreException, since the
// exception is wrapped in a generic error by our async/await handling.
const retryable =
idempotent &&
isDomException(e) &&
error.name !== 'FirebaseError' &&
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the instanceof check doesn't work here since the Async/Await wraps our FirestoreError in a generic Error.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be worthwhile to add as an actual comment to the code rather than just the PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

attemptNumber < TRANSACTION_RETRY_COUNT;
debug(
LOG_TAG,
'Transaction failed with error: %s. Retrying: %s.',
e.message,
error.message,
retryable
);

if (!retryable) {
return Promise.reject(e);
return Promise.reject(error);
}
}
}
Expand Down Expand Up @@ -801,13 +804,3 @@ function checkForAndReportiOSError(error: DOMException): Error {
}
return error;
}

/** Checks whether an error is a DOMException (e.g. as thrown by IndexedDb). */
function isDomException(error: Error): boolean {
// DOMException is not a global type in Node with persistence, and hence we
// check the constructor name if the type in unknown.
return (
(typeof DOMException !== 'undefined' && error instanceof DOMException) ||
error.constructor.name === 'DOMException'
);
}
5 changes: 3 additions & 2 deletions packages/firestore/test/unit/local/simple_db.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
import { PersistencePromise } from '../../../src/local/persistence_promise';

import { fail } from '../../../src/util/assert';
import { Code, FirestoreError } from '../../../src/util/error';

use(chaiAsPromised);

Expand Down Expand Up @@ -551,8 +552,8 @@ describe('SimpleDb', () => {
await expect(
db.runTransaction('readwrite-idempotent', ['users'], txn => {
++attemptCount;
txn.abort();
return PersistencePromise.reject(new Error('Aborted'));
txn.abort(new FirestoreError(Code.ABORTED, 'Aborted'));
return PersistencePromise.reject(new Error());
})
).to.eventually.be.rejected;

Expand Down