Skip to content

Commit a10c18f

Browse files
Add IndexedDB action to error message (#3871)
* Add IndexedDB action to error message * Create gentle-doors-play.md * Feedback * Fix big
1 parent 41467b5 commit a10c18f

10 files changed

+604
-442
lines changed

.changeset/gentle-doors-play.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@firebase/firestore": patch
3+
---
4+
5+
The SDK now include more information in the error message for failed IndexedDB transactions.

packages/firestore/src/local/indexeddb_persistence.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,7 @@ export class IndexedDbPersistence implements Persistence {
684684
// Use `SimpleDb.runTransaction` directly to avoid failing if another tab
685685
// has obtained the primary lease.
686686
await this.simpleDb.runTransaction(
687+
'shutdown',
687688
'readwrite',
688689
[DbPrimaryClient.store, DbClientMetadata.store],
689690
simpleDbTxn => {
@@ -794,7 +795,7 @@ export class IndexedDbPersistence implements Persistence {
794795
// Do all transactions as readwrite against all object stores, since we
795796
// are the only reader/writer.
796797
return this.simpleDb
797-
.runTransaction(simpleDbMode, ALL_STORES, simpleDbTxn => {
798+
.runTransaction(action, simpleDbMode, ALL_STORES, simpleDbTxn => {
798799
persistenceTransaction = new IndexedDbTransaction(
799800
simpleDbTxn,
800801
this.listenSequence

packages/firestore/src/local/indexeddb_schema.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export class SchemaConverter implements SimpleDbSchemaConverter {
8888
`Unexpected schema upgrade from v${fromVersion} to v${toVersion}.`
8989
);
9090

91-
const simpleDbTransaction = new SimpleDbTransaction(txn);
91+
const simpleDbTransaction = new SimpleDbTransaction('createOrUpgrade', txn);
9292

9393
if (fromVersion < 1 && toVersion >= 1) {
9494
createPrimaryClientStore(db);

packages/firestore/src/local/simple_db.ts

+25-10
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ export class SimpleDb {
189189
/**
190190
* Opens the specified database, creating or upgrading it if necessary.
191191
*/
192-
async ensureDb(): Promise<IDBDatabase> {
192+
async ensureDb(action: string): Promise<IDBDatabase> {
193193
if (!this.db) {
194194
logDebug(LOG_TAG, 'Opening database:', this.name);
195195
this.db = await new Promise<IDBDatabase>((resolve, reject) => {
@@ -208,6 +208,7 @@ export class SimpleDb {
208208
request.onblocked = () => {
209209
reject(
210210
new IndexedDbTransactionError(
211+
action,
211212
'Cannot upgrade IndexedDB schema while another tab is open. ' +
212213
'Close all tabs that access Firestore and reload this page to proceed.'
213214
)
@@ -228,7 +229,7 @@ export class SimpleDb {
228229
)
229230
);
230231
} else {
231-
reject(new IndexedDbTransactionError(error));
232+
reject(new IndexedDbTransactionError(action, error));
232233
}
233234
};
234235

@@ -274,6 +275,7 @@ export class SimpleDb {
274275
}
275276

276277
async runTransaction<T>(
278+
action: string,
277279
mode: SimpleDbTransactionMode,
278280
objectStores: string[],
279281
transactionFn: (transaction: SimpleDbTransaction) => PersistencePromise<T>
@@ -285,10 +287,11 @@ export class SimpleDb {
285287
++attemptNumber;
286288

287289
try {
288-
this.db = await this.ensureDb();
290+
this.db = await this.ensureDb(action);
289291

290292
const transaction = SimpleDbTransaction.open(
291293
this.db,
294+
action,
292295
readonly ? 'readonly' : 'readwrite',
293296
objectStores
294297
);
@@ -424,8 +427,11 @@ export interface IterateOptions {
424427
export class IndexedDbTransactionError extends FirestoreError {
425428
name = 'IndexedDbTransactionError';
426429

427-
constructor(cause: Error | string) {
428-
super(Code.UNAVAILABLE, 'IndexedDB transaction failed: ' + cause);
430+
constructor(actionName: string, cause: Error | string) {
431+
super(
432+
Code.UNAVAILABLE,
433+
`IndexedDB transaction '${actionName}' failed: ${cause}`
434+
);
429435
}
430436
}
431437

@@ -450,24 +456,31 @@ export class SimpleDbTransaction {
450456

451457
static open(
452458
db: IDBDatabase,
459+
action: string,
453460
mode: IDBTransactionMode,
454461
objectStoreNames: string[]
455462
): SimpleDbTransaction {
456463
try {
457-
return new SimpleDbTransaction(db.transaction(objectStoreNames, mode));
464+
return new SimpleDbTransaction(
465+
action,
466+
db.transaction(objectStoreNames, mode)
467+
);
458468
} catch (e) {
459-
throw new IndexedDbTransactionError(e);
469+
throw new IndexedDbTransactionError(action, e);
460470
}
461471
}
462472

463-
constructor(private readonly transaction: IDBTransaction) {
473+
constructor(
474+
private readonly action: string,
475+
private readonly transaction: IDBTransaction
476+
) {
464477
this.transaction.oncomplete = () => {
465478
this.completionDeferred.resolve();
466479
};
467480
this.transaction.onabort = () => {
468481
if (transaction.error) {
469482
this.completionDeferred.reject(
470-
new IndexedDbTransactionError(transaction.error)
483+
new IndexedDbTransactionError(action, transaction.error)
471484
);
472485
} else {
473486
this.completionDeferred.resolve();
@@ -477,7 +490,9 @@ export class SimpleDbTransaction {
477490
const error = checkForAndReportiOSError(
478491
(event.target as IDBRequest).error!
479492
);
480-
this.completionDeferred.reject(new IndexedDbTransactionError(error));
493+
this.completionDeferred.reject(
494+
new IndexedDbTransactionError(action, error)
495+
);
481496
};
482497
}
483498

packages/firestore/test/unit/local/encoded_resource_path.test.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,12 @@ function runTransaction<T>(
195195
transaction: SimpleDbTransaction
196196
) => PersistencePromise<T>
197197
): Promise<T> {
198-
return db.runTransaction<T>('readwrite', ['test'], txn => {
199-
return fn(txn.store<string, boolean>('test'), txn);
200-
});
198+
return db.runTransaction<T>(
199+
'EncodedResourcePathTests',
200+
'readwrite',
201+
['test'],
202+
txn => {
203+
return fn(txn.store<string, boolean>('test'), txn);
204+
}
205+
);
201206
}

0 commit comments

Comments
 (0)