Skip to content

Commit b1622e4

Browse files
committed
Introduce DatabaseDeletedListenerError to fix infinite retries
1 parent 4083345 commit b1622e4

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

packages/firestore/src/core/firestore_client.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ import {
3333
localStoreReadDocument,
3434
localStoreSetIndexAutoCreationEnabled
3535
} from '../local/local_store_impl';
36-
import { Persistence } from '../local/persistence';
36+
import {
37+
DatabaseDeletedListenerError,
38+
Persistence
39+
} from '../local/persistence';
3740
import { Document } from '../model/document';
3841
import { DocumentKey } from '../model/document_key';
3942
import { FieldIndex } from '../model/field_index';
@@ -253,12 +256,11 @@ export async function setOfflineComponentProvider(
253256
});
254257

255258
if (event.type === 'ClearSiteDataDatabaseDeletedEvent') {
256-
throw new Error(
257-
`aborting refusing to open IndexedDB database due to potential ` +
258-
`corruption of the IndexedDB database data; this corruption could ` +
259-
`be caused by clicking the "clear site data" button in a web ` +
260-
`browser; try reloading the web page to re-initialize the ` +
261-
`IndexedDB database (` +
259+
throw new DatabaseDeletedListenerError(
260+
`Aborting due to potential IndexedDB database corruption. ` +
261+
`This situation could be caused by clicking the "Clear Site Data" ` +
262+
`button in a web browser. Try reloading the web page to ` +
263+
`re-initialize the IndexedDB database. (` +
262264
`lastClosedVersion=${event.data.lastClosedVersion}, ` +
263265
`eventOldVersion${event.data.eventOldVersion}, ` +
264266
`eventNewVersion${event.data.eventNewVersion}, ` +

packages/firestore/src/local/persistence.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,22 @@ export class ClearSiteDataDatabaseDeletedEvent {
152152
) {}
153153
}
154154

155+
/**
156+
* An exception that should be thrown from a {@link DatabaseDeletedListener}
157+
* to indicate that the caller should immediately abort their work.
158+
*/
159+
export class DatabaseDeletedListenerError extends Error {
160+
readonly name = 'DatabaseDeletedListenerError' as const;
161+
162+
constructor(message: string) {
163+
super(message);
164+
165+
// Ensure that instanceof semantics behave as expected.
166+
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#custom_error_types
167+
Object.setPrototypeOf(this, DatabaseDeletedListenerError.prototype);
168+
}
169+
}
170+
155171
/**
156172
* The type of the "event" parameter of {@link DatabaseDeletedListener}.
157173
*/

packages/firestore/src/local/simple_db.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { getGlobal, getUA, isIndexedDBAvailable } from '@firebase/util';
1919

2020
import { debugAssert } from '../util/assert';
2121
import { Code, FirestoreError } from '../util/error';
22-
import { logDebug, logError, logWarn } from '../util/log';
22+
import { logDebug, logError } from '../util/log';
2323
import { Deferred } from '../util/promise';
2424

2525
import {
@@ -341,6 +341,8 @@ export class SimpleDb {
341341
error
342342
)
343343
);
344+
} else if (error.name === 'DatabaseDeletedListenerError') {
345+
reject(new FirestoreError(Code.FAILED_PRECONDITION, error.message));
344346
} else {
345347
reject(new IndexedDbTransactionError(action, error));
346348
}
@@ -400,10 +402,6 @@ export class SimpleDb {
400402
// Notify the listener if another tab attempted to delete the IndexedDb
401403
// database, such as by calling clearIndexedDbPersistence().
402404
if (event.newVersion === null) {
403-
logWarn(
404-
`Received "versionchange" event with newVersion===null; ` +
405-
'notifying the registered DatabaseDeletedListener, if any'
406-
);
407405
this.databaseDeletedListener?.(
408406
new VersionChangeDatabaseDeletedEvent({
409407
eventNewVersion: event.newVersion

0 commit comments

Comments
 (0)