Skip to content

Add IndexedDB action to error message #3871

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 5 commits into from
Oct 1, 2020
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
5 changes: 5 additions & 0 deletions .changeset/gentle-doors-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@firebase/firestore": patch
---

The SDK now include more information in the error message for failed IndexedDB transactions.
3 changes: 2 additions & 1 deletion packages/firestore/src/local/indexeddb_persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,7 @@ export class IndexedDbPersistence implements Persistence {
// Use `SimpleDb.runTransaction` directly to avoid failing if another tab
// has obtained the primary lease.
await this.simpleDb.runTransaction(
'shutdown',
'readwrite',
[DbPrimaryClient.store, DbClientMetadata.store],
simpleDbTxn => {
Expand Down Expand Up @@ -794,7 +795,7 @@ export class IndexedDbPersistence implements Persistence {
// Do all transactions as readwrite against all object stores, since we
// are the only reader/writer.
return this.simpleDb
.runTransaction(simpleDbMode, ALL_STORES, simpleDbTxn => {
.runTransaction(action, simpleDbMode, ALL_STORES, simpleDbTxn => {
persistenceTransaction = new IndexedDbTransaction(
simpleDbTxn,
this.listenSequence
Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/src/local/indexeddb_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class SchemaConverter implements SimpleDbSchemaConverter {
`Unexpected schema upgrade from v${fromVersion} to v${toVersion}.`
);

const simpleDbTransaction = new SimpleDbTransaction(txn);
const simpleDbTransaction = new SimpleDbTransaction('createOrUpgrade', txn);

if (fromVersion < 1 && toVersion >= 1) {
createPrimaryClientStore(db);
Expand Down
35 changes: 25 additions & 10 deletions packages/firestore/src/local/simple_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export class SimpleDb {
/**
* Opens the specified database, creating or upgrading it if necessary.
*/
async ensureDb(): Promise<IDBDatabase> {
async ensureDb(action: string): Promise<IDBDatabase> {
if (!this.db) {
logDebug(LOG_TAG, 'Opening database:', this.name);
this.db = await new Promise<IDBDatabase>((resolve, reject) => {
Expand All @@ -208,6 +208,7 @@ export class SimpleDb {
request.onblocked = () => {
reject(
new IndexedDbTransactionError(
action,
'Cannot upgrade IndexedDB schema while another tab is open. ' +
'Close all tabs that access Firestore and reload this page to proceed.'
)
Expand All @@ -228,7 +229,7 @@ export class SimpleDb {
)
);
} else {
reject(new IndexedDbTransactionError(error));
reject(new IndexedDbTransactionError(action, error));
}
};

Expand Down Expand Up @@ -274,6 +275,7 @@ export class SimpleDb {
}

async runTransaction<T>(
action: string,
mode: SimpleDbTransactionMode,
objectStores: string[],
transactionFn: (transaction: SimpleDbTransaction) => PersistencePromise<T>
Expand All @@ -285,10 +287,11 @@ export class SimpleDb {
++attemptNumber;

try {
this.db = await this.ensureDb();
this.db = await this.ensureDb(action);

const transaction = SimpleDbTransaction.open(
this.db,
action,
readonly ? 'readonly' : 'readwrite',
objectStores
);
Expand Down Expand Up @@ -424,8 +427,11 @@ export interface IterateOptions {
export class IndexedDbTransactionError extends FirestoreError {
name = 'IndexedDbTransactionError';

constructor(cause: Error | string) {
super(Code.UNAVAILABLE, 'IndexedDB transaction failed: ' + cause);
constructor(actionName: string, cause: Error | string) {
super(
Code.UNAVAILABLE,
`IndexedDB transaction '${actionName}' failed: ${cause}`
);
}
}

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

static open(
db: IDBDatabase,
action: string,
mode: IDBTransactionMode,
objectStoreNames: string[]
): SimpleDbTransaction {
try {
return new SimpleDbTransaction(db.transaction(objectStoreNames, mode));
return new SimpleDbTransaction(
action,
db.transaction(objectStoreNames, mode)
);
} catch (e) {
throw new IndexedDbTransactionError(e);
throw new IndexedDbTransactionError(action, e);
}
}

constructor(private readonly transaction: IDBTransaction) {
constructor(
private readonly action: string,
private readonly transaction: IDBTransaction
) {
this.transaction.oncomplete = () => {
this.completionDeferred.resolve();
};
this.transaction.onabort = () => {
if (transaction.error) {
this.completionDeferred.reject(
new IndexedDbTransactionError(transaction.error)
new IndexedDbTransactionError(action, transaction.error)
);
} else {
this.completionDeferred.resolve();
Expand All @@ -477,7 +490,9 @@ export class SimpleDbTransaction {
const error = checkForAndReportiOSError(
(event.target as IDBRequest).error!
);
this.completionDeferred.reject(new IndexedDbTransactionError(error));
this.completionDeferred.reject(
new IndexedDbTransactionError(action, error)
);
};
}

Expand Down
11 changes: 8 additions & 3 deletions packages/firestore/test/unit/local/encoded_resource_path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,12 @@ function runTransaction<T>(
transaction: SimpleDbTransaction
) => PersistencePromise<T>
): Promise<T> {
return db.runTransaction<T>('readwrite', ['test'], txn => {
return fn(txn.store<string, boolean>('test'), txn);
});
return db.runTransaction<T>(
'EncodedResourcePathTests',
'readwrite',
['test'],
txn => {
return fn(txn.store<string, boolean>('test'), txn);
}
);
}
Loading