Skip to content

Commit b05b923

Browse files
committed
ignore appcheck throttling errors
1 parent 9b63cd6 commit b05b923

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

packages/firestore/src/api/credentials.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import { User } from '../auth/user';
3232
import { debugAssert, hardAssert } from '../util/assert';
3333
import { AsyncQueue } from '../util/async_queue';
3434
import { Code, FirestoreError } from '../util/error';
35-
import { logDebug } from '../util/log';
35+
import { logDebug, logWarn } from '../util/log';
3636
import { Deferred } from '../util/promise';
3737

3838
// TODO(mikelehen): This should be split into multiple files and probably
@@ -527,12 +527,28 @@ export class FirebaseAppCheckTokenProvider
527527
const onTokenChanged: (
528528
tokenResult: AppCheckTokenResult
529529
) => Promise<void> = tokenResult => {
530-
if (tokenResult.error != null) {
531-
logDebug(
530+
if (tokenResult.error) {
531+
const code = getCode(tokenResult.error);
532+
logWarn(
532533
'FirebaseAppCheckTokenProvider',
533-
`Error getting App Check token; using placeholder token instead. Error: ${tokenResult.error.message}`
534+
`Error getting App Check token (code=${code}): ${tokenResult.error.message}`
534535
);
536+
537+
// Ignore errors due to the App Check provider throttling requests to
538+
// the provider's servers. Treat these errors as if nothing happened
539+
// because App Check will get the provider to try again after throttling
540+
// unblock it, at which point we will get a shiny, new, valid token.
541+
if (isAppCheckThrottleCode(code)) {
542+
logWarn(
543+
'FirebaseAppCheckTokenProvider',
544+
'ignoring failed App Check token retrieval since it is throttled ' +
545+
'and App Check will try again later. (code=${code})'
546+
);
547+
return Promise.resolve();
548+
}
549+
logWarn('FirebaseAppCheckTokenProvider', 'using placeholder token');
535550
}
551+
536552
const tokenUpdated = tokenResult.token !== this.latestAppCheckToken;
537553
this.latestAppCheckToken = tokenResult.token;
538554
logDebug(
@@ -718,3 +734,15 @@ export function makeAuthCredentialsProvider(
718734
);
719735
}
720736
}
737+
738+
function getCode(error: Error): string | undefined {
739+
return 'code' in error && typeof error.code === 'string'
740+
? error.code
741+
: undefined;
742+
}
743+
744+
type AppCheckThrottleCode = 'appCheck/initial-throttle' | 'appCheck/throttled';
745+
746+
function isAppCheckThrottleCode(code: unknown): code is AppCheckThrottleCode {
747+
return code === 'appCheck/initial-throttle' || code === 'appCheck/throttled';
748+
}

packages/firestore/src/remote/persistent_stream.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
import { debugAssert, hardAssert } from '../util/assert';
3131
import { AsyncQueue, DelayedOperation, TimerId } from '../util/async_queue';
3232
import { Code, FirestoreError } from '../util/error';
33-
import { logDebug, logError } from '../util/log';
33+
import { logDebug, logError, logWarn } from '../util/log';
3434
import { isNullOrUndefined } from '../util/types';
3535

3636
import { ExponentialBackoff } from './backoff';
@@ -400,6 +400,13 @@ export abstract class PersistentStream<
400400
// fail, however. In this case, we should get a Code.UNAUTHENTICATED error
401401
// before we received the first message and we need to invalidate the token
402402
// to ensure that we fetch a new token.
403+
logWarn(
404+
LOG_TAG,
405+
'Invalidating Auth and AppCheck tokens ' +
406+
'in response to UNAUTHENTICATED error ' +
407+
'on an otherwise-healthy connection ' +
408+
'so that new token(s) are retrieved upon reconnection'
409+
);
403410
this.authCredentialsProvider.invalidateToken();
404411
this.appCheckCredentialsProvider.invalidateToken();
405412
}

0 commit comments

Comments
 (0)