Skip to content

Commit 8e952e2

Browse files
authored
Cast unknowns to errors in catch blocks (#6362)
1 parent b091b02 commit 8e952e2

18 files changed

+48
-38
lines changed

packages/firestore/src/api/database.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -328,15 +328,16 @@ function setPersistenceProviders(
328328
await setOnlineComponentProvider(client, onlineComponentProvider);
329329
persistenceResult.resolve();
330330
} catch (e) {
331-
if (!canFallbackFromIndexedDbError(e)) {
332-
throw e;
331+
const error = e as FirestoreError | DOMException;
332+
if (!canFallbackFromIndexedDbError(error)) {
333+
throw error;
333334
}
334335
console.warn(
335336
'Error enabling offline persistence. Falling back to ' +
336337
'persistence disabled: ' +
337-
e
338+
error
338339
);
339-
persistenceResult.reject(e);
340+
persistenceResult.reject(error);
340341
}
341342
})
342343
.then(() => persistenceResult.promise);
@@ -419,7 +420,7 @@ export function clearIndexedDbPersistence(firestore: Firestore): Promise<void> {
419420
);
420421
deferred.resolve();
421422
} catch (e) {
422-
deferred.reject(e);
423+
deferred.reject(e as Error | undefined);
423424
}
424425
});
425426
return deferred.promise;

packages/firestore/src/api/index_configuration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ function tryParseJson(json: string): Record<string, unknown> {
205205
} catch (e) {
206206
throw new FirestoreError(
207207
Code.INVALID_ARGUMENT,
208-
'Failed to parse JSON:' + e.message
208+
'Failed to parse JSON:' + (e as Error)?.message
209209
);
210210
}
211211
}

packages/firestore/src/core/firestore_client.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ export class FirestoreClient {
188188
deferred.resolve();
189189
} catch (e) {
190190
const firestoreError = wrapInUserErrorIfRecoverable(
191-
e,
191+
e as Error,
192192
`Failed to shutdown persistence`
193193
);
194194
deferred.reject(firestoreError);
@@ -525,7 +525,7 @@ async function readDocumentFromCache(
525525
}
526526
} catch (e) {
527527
const firestoreError = wrapInUserErrorIfRecoverable(
528-
e,
528+
e as Error,
529529
`Failed to get document '${docKey} from cache`
530530
);
531531
result.reject(firestoreError);
@@ -623,7 +623,7 @@ async function executeQueryFromCache(
623623
result.resolve(viewChange.snapshot!);
624624
} catch (e) {
625625
const firestoreError = wrapInUserErrorIfRecoverable(
626-
e,
626+
e as Error,
627627
`Failed to execute query '${query} against cache`
628628
);
629629
result.reject(firestoreError);

packages/firestore/src/core/sync_engine_impl.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,10 @@ export async function syncEngineWrite(
473473
} catch (e) {
474474
// If we can't persist the mutation, we reject the user callback and
475475
// don't send the mutation. The user can then retry the write.
476-
const error = wrapInUserErrorIfRecoverable(e, `Failed to persist write`);
476+
const error = wrapInUserErrorIfRecoverable(
477+
e as Error,
478+
`Failed to persist write`
479+
);
477480
userCallback.reject(error);
478481
}
479482
}
@@ -532,7 +535,7 @@ export async function syncEngineApplyRemoteEvent(
532535
remoteEvent
533536
);
534537
} catch (error) {
535-
await ignoreIfPrimaryLeaseLoss(error);
538+
await ignoreIfPrimaryLeaseLoss(error as FirestoreError);
536539
}
537540
}
538541

@@ -684,7 +687,7 @@ export async function syncEngineApplySuccessfulWrite(
684687
);
685688
await syncEngineEmitNewSnapsAndNotifyLocalStore(syncEngineImpl, changes);
686689
} catch (error) {
687-
await ignoreIfPrimaryLeaseLoss(error);
690+
await ignoreIfPrimaryLeaseLoss(error as FirestoreError);
688691
}
689692
}
690693

@@ -715,7 +718,7 @@ export async function syncEngineRejectFailedWrite(
715718
);
716719
await syncEngineEmitNewSnapsAndNotifyLocalStore(syncEngineImpl, changes);
717720
} catch (error) {
718-
await ignoreIfPrimaryLeaseLoss(error);
721+
await ignoreIfPrimaryLeaseLoss(error as FirestoreError);
719722
}
720723
}
721724

@@ -752,7 +755,7 @@ export async function syncEngineRegisterPendingWritesCallback(
752755
syncEngineImpl.pendingWritesCallbacks.set(highestBatchId, callbacks);
753756
} catch (e) {
754757
const firestoreError = wrapInUserErrorIfRecoverable(
755-
e,
758+
e as Error,
756759
'Initialization of waitForPendingWrites() operation failed'
757760
);
758761
callback.reject(firestoreError);
@@ -1628,7 +1631,7 @@ async function loadBundleImpl(
16281631
return Promise.resolve(result.changedCollectionGroups);
16291632
} catch (e) {
16301633
logWarn(LOG_TAG, `Loading bundle failed with ${e}`);
1631-
task._failWith(e);
1634+
task._failWith(e as FirestoreError);
16321635
return Promise.resolve(new Set<string>());
16331636
}
16341637
}

packages/firestore/src/core/transaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export class Transaction {
8383
try {
8484
this.write(data.toMutation(key, this.preconditionForUpdate(key)));
8585
} catch (e) {
86-
this.lastWriteError = e;
86+
this.lastWriteError = e as FirestoreError | null;
8787
}
8888
this.writtenDocs.add(key.toString());
8989
}

packages/firestore/src/core/transaction_runner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export class TransactionRunner<T> {
9595
return userPromise;
9696
} catch (error) {
9797
// Do not retry errors thrown by user provided updateFunction.
98-
this.deferred.reject(error);
98+
this.deferred.reject(error as Error);
9999
return null;
100100
}
101101
}

packages/firestore/src/local/index_backfiller.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17+
import { FirestoreError } from '../api';
1718
import { DocumentMap } from '../model/collections';
1819
import {
1920
IndexOffset,
@@ -90,14 +91,14 @@ export class IndexBackfillerScheduler implements Scheduler {
9091
const documentsProcessed = await this.backfiller.backfill();
9192
logDebug(LOG_TAG, `Documents written: ${documentsProcessed}`);
9293
} catch (e) {
93-
if (isIndexedDbTransactionError(e)) {
94+
if (isIndexedDbTransactionError(e as Error)) {
9495
logDebug(
9596
LOG_TAG,
9697
'Ignoring IndexedDB error during index backfill: ',
9798
e
9899
);
99100
} else {
100-
await ignoreIfPrimaryLeaseLoss(e);
101+
await ignoreIfPrimaryLeaseLoss(e as FirestoreError);
101102
}
102103
}
103104
await this.schedule(REGULAR_BACKFILL_DELAY_MS);

packages/firestore/src/local/local_store_impl.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ export async function localStoreNotifyLocalViewChanges(
844844
}
845845
);
846846
} catch (e) {
847-
if (isIndexedDbTransactionError(e)) {
847+
if (isIndexedDbTransactionError(e as Error)) {
848848
// If `notifyLocalViewChanges` fails, we did not advance the sequence
849849
// number for the documents that were included in this transaction.
850850
// This might trigger them to be deleted earlier than they otherwise
@@ -1039,7 +1039,7 @@ export async function localStoreReleaseTarget(
10391039
);
10401040
}
10411041
} catch (e) {
1042-
if (isIndexedDbTransactionError(e)) {
1042+
if (isIndexedDbTransactionError(e as Error)) {
10431043
// All `releaseTarget` does is record the final metadata state for the
10441044
// target, but we've been recording this periodically during target
10451045
// activity. If we lose this write this could cause a very slight

packages/firestore/src/local/lru_garbage_collector_impl.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18+
import { FirestoreError } from '../api';
1819
import { ListenSequence } from '../core/listen_sequence';
1920
import { ListenSequenceNumber } from '../core/types';
2021
import { debugAssert } from '../util/assert';
@@ -159,14 +160,14 @@ export class LruScheduler implements Scheduler {
159160
try {
160161
await this.localStore.collectGarbage(this.garbageCollector);
161162
} catch (e) {
162-
if (isIndexedDbTransactionError(e)) {
163+
if (isIndexedDbTransactionError(e as Error)) {
163164
logDebug(
164165
LOG_TAG,
165166
'Ignoring IndexedDB error during garbage collection: ',
166167
e
167168
);
168169
} else {
169-
await ignoreIfPrimaryLeaseLoss(e);
170+
await ignoreIfPrimaryLeaseLoss(e as FirestoreError);
170171
}
171172
}
172173
await this.scheduleGC(REGULAR_GC_DELAY_MS);

packages/firestore/src/local/persistence_promise.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export class PersistencePromise<T> {
124124
return PersistencePromise.resolve(result);
125125
}
126126
} catch (e) {
127-
return PersistencePromise.reject<R>(e);
127+
return PersistencePromise.reject<R>(e as Error);
128128
}
129129
}
130130

packages/firestore/src/local/simple_db.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export class SimpleDbTransaction {
7171
db.transaction(objectStoreNames, mode)
7272
);
7373
} catch (e) {
74-
throw new IndexedDbTransactionError(action, e);
74+
throw new IndexedDbTransactionError(action, e as Error);
7575
}
7676
}
7777

@@ -434,7 +434,8 @@ export class SimpleDb {
434434
// caller.
435435
await transaction.completionPromise;
436436
return transactionFnResult;
437-
} catch (error) {
437+
} catch (e) {
438+
const error = e as Error;
438439
// TODO(schmidt-sebastian): We could probably be smarter about this and
439440
// not retry exceptions that are likely unrecoverable (such as quota
440441
// exceeded errors).

packages/firestore/src/remote/remote_store.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ async function onWatchStreamChange(
451451
watchChange.targetIds.join(','),
452452
e
453453
);
454-
await disableNetworkUntilRecovery(remoteStoreImpl, e);
454+
await disableNetworkUntilRecovery(remoteStoreImpl, e as FirestoreError);
455455
}
456456
return;
457457
}
@@ -481,7 +481,7 @@ async function onWatchStreamChange(
481481
}
482482
} catch (e) {
483483
logDebug(LOG_TAG, 'Failed to raise snapshot:', e);
484-
await disableNetworkUntilRecovery(remoteStoreImpl, e);
484+
await disableNetworkUntilRecovery(remoteStoreImpl, e as FirestoreError);
485485
}
486486
}
487487
}
@@ -675,7 +675,7 @@ export async function fillWritePipeline(
675675
addToWritePipeline(remoteStoreImpl, batch);
676676
}
677677
} catch (e) {
678-
await disableNetworkUntilRecovery(remoteStoreImpl, e);
678+
await disableNetworkUntilRecovery(remoteStoreImpl, e as FirestoreError);
679679
}
680680
}
681681

packages/firestore/src/util/async_queue_impl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ export class AsyncQueueImpl implements AsyncQueue {
158158
this.retryableOps.shift();
159159
this.backoff.reset();
160160
} catch (e) {
161-
if (isIndexedDbTransactionError(e)) {
161+
if (isIndexedDbTransactionError(e as Error)) {
162162
logDebug(LOG_TAG, 'Operation failed with retryable error: ' + e);
163163
} else {
164164
throw e; // Failure will be handled by AsyncQueue

packages/firestore/test/integration/api/array_transforms.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ import {
2929
setDoc,
3030
updateDoc,
3131
arrayRemove,
32-
arrayUnion
32+
arrayUnion,
33+
FirestoreError
3334
} from '../util/firebase_export';
3435
import { apiDescribe, withTestDb, withTestDoc } from '../util/helpers';
3536

@@ -191,7 +192,7 @@ apiDescribe('Array Transforms:', (persistence: boolean) => {
191192
try {
192193
await getDocFromCache(docRef);
193194
} catch (err) {
194-
expect(err.code).to.equal('unavailable');
195+
expect((err as FirestoreError).code).to.equal('unavailable');
195196
errCaught = true;
196197
}
197198
expect(errCaught).to.be.true;

packages/firestore/test/lite/integration.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,9 @@ describe('Firestore', () => {
148148
getDoc(doc(firestore, 'coll/doc'));
149149
expect.fail();
150150
} catch (e) {
151-
expect(e.message).to.equal('The client has already been terminated.');
151+
expect((e as Error)?.message).to.equal(
152+
'The client has already been terminated.'
153+
);
152154
}
153155
});
154156

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,9 +1246,9 @@ describe('IndexedDbSchema: createOrUpgradeDb', () => {
12461246
);
12471247
await db.ensureDb(this.test!.fullTitle());
12481248
} catch (e) {
1249-
error = e;
1249+
error = e as FirestoreError;
12501250
expect(
1251-
e.message.indexOf('A newer version of the Firestore SDK')
1251+
error?.message?.indexOf('A newer version of the Firestore SDK')
12521252
).to.not.equal(-1);
12531253
}
12541254
expect(error).to.not.be.null;

packages/firestore/test/unit/util/async_queue.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ function defer<T>(op: () => T): Promise<T> {
441441
try {
442442
resolve(op());
443443
} catch (e) {
444-
reject(e);
444+
reject(e as Error);
445445
}
446446
}, 0);
447447
});

packages/messaging/src/internals/requests.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export async function requestGetToken(
5858
responseData = await response.json();
5959
} catch (err) {
6060
throw ERROR_FACTORY.create(ErrorCode.TOKEN_SUBSCRIBE_FAILED, {
61-
errorInfo: err
61+
errorInfo: (err as Error)?.toString()
6262
});
6363
}
6464

@@ -98,7 +98,7 @@ export async function requestUpdateToken(
9898
responseData = await response.json();
9999
} catch (err) {
100100
throw ERROR_FACTORY.create(ErrorCode.TOKEN_UPDATE_FAILED, {
101-
errorInfo: err
101+
errorInfo: (err as Error)?.toString()
102102
});
103103
}
104104

0 commit comments

Comments
 (0)