Skip to content

Commit 47460e7

Browse files
committed
Fix tests and clean up
1 parent 1ac6490 commit 47460e7

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

.changeset/fluffy-otters-whisper.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@firebase/app-check': patch
3+
---
4+
5+
Improve error handling in AppCheck. The publicly-exported `getToken()` will now throw `internalError` strings it was previously ignoring.

packages/app-check/src/internal-api.test.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ describe('internal api', () => {
153153
});
154154

155155
it('resolves with a dummy token and an error if failed to get a token', async () => {
156+
const errorStub = stub(console, 'error');
156157
const appCheck = initializeAppCheck(app, {
157158
provider: new ReCaptchaV3Provider(FAKE_SITE_KEY)
158159
});
@@ -162,16 +163,21 @@ describe('internal api', () => {
162163
const error = new Error('oops, something went wrong');
163164
stub(client, 'exchangeToken').returns(Promise.reject(error));
164165

165-
const token = await getToken(appCheck as AppCheckService);
166+
const token = await getToken(appCheck as AppCheckService, false, true);
166167

167168
expect(reCAPTCHASpy).to.be.called;
168169
expect(token).to.deep.equal({
169170
token: formatDummyToken(defaultTokenErrorData),
170171
error
171172
});
173+
expect(errorStub.args[0][1].message).to.include(
174+
'oops, something went wrong'
175+
);
176+
errorStub.restore();
172177
});
173178

174179
it('resolves with a dummy token and an error if failed to get a token in debug mode', async () => {
180+
const errorStub = stub(console, 'error');
175181
window.FIREBASE_APPCHECK_DEBUG_TOKEN = true;
176182
const appCheck = initializeAppCheck(app, {
177183
provider: new ReCaptchaV3Provider(FAKE_SITE_KEY)
@@ -180,28 +186,37 @@ describe('internal api', () => {
180186
const error = new Error('oops, something went wrong');
181187
stub(client, 'exchangeToken').returns(Promise.reject(error));
182188

183-
const token = await getToken(appCheck as AppCheckService);
189+
const token = await getToken(appCheck as AppCheckService, false, true);
184190

185191
expect(token).to.deep.equal({
186192
token: formatDummyToken(defaultTokenErrorData),
187193
error
188194
});
195+
expect(errorStub.args[0][1].message).to.include(
196+
'oops, something went wrong'
197+
);
189198
delete window.FIREBASE_APPCHECK_DEBUG_TOKEN;
199+
errorStub.restore();
190200
});
191201

192202
it('resolves with a dummy token and an error if recaptcha failed', async () => {
203+
const errorStub = stub(console, 'error');
193204
const appCheck = initializeAppCheck(app, {
194205
provider: new ReCaptchaV3Provider(FAKE_SITE_KEY)
195206
});
196207

197208
const reCAPTCHASpy = stubGetRecaptchaToken('', false);
198209
const exchangeTokenStub = stub(client, 'exchangeToken');
199210

200-
const token = await getToken(appCheck as AppCheckService);
211+
const token = await getToken(appCheck as AppCheckService, false, true);
201212

202213
expect(reCAPTCHASpy).to.be.called;
203214
expect(exchangeTokenStub).to.not.be.called;
204215
expect(token.token).to.equal(formatDummyToken(defaultTokenErrorData));
216+
expect(errorStub.args[0][1].message).to.include(
217+
AppCheckError.RECAPTCHA_ERROR
218+
);
219+
errorStub.restore();
205220
});
206221

207222
it('notifies listeners using cached token', async () => {

packages/app-check/src/internal-api.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ export function formatDummyToken(
6060
*/
6161
export async function getToken(
6262
appCheck: AppCheckService,
63-
forceRefresh = false
63+
forceRefresh = false,
64+
shouldLogErrors = false
6465
): Promise<AppCheckTokenResult> {
6566
const app = appCheck.app;
6667
ensureActivated(app);
@@ -136,9 +137,15 @@ export async function getToken(
136137
state.token = tokenFromDebugExchange;
137138
return { token: tokenFromDebugExchange.token };
138139
} catch (e) {
139-
if ((e as FirebaseError).code === `appCheck/${AppCheckError.THROTTLED}`) {
140+
if (
141+
(e as FirebaseError).code === `appCheck/${AppCheckError.THROTTLED}` ||
142+
(e as FirebaseError).code ===
143+
`appCheck/${AppCheckError.INITIAL_THROTTLE}`
144+
) {
140145
// Warn if throttled, but do not treat it as an error.
141146
logger.warn((e as FirebaseError).message);
147+
} else if (shouldLogErrors) {
148+
logger.error(e);
142149
}
143150
// Return dummy token and error
144151
return makeDummyTokenResult(e as FirebaseError);
@@ -164,9 +171,14 @@ export async function getToken(
164171
}
165172
token = await getStateReference(app).exchangeTokenPromise;
166173
} catch (e) {
167-
if ((e as FirebaseError).code === `appCheck/${AppCheckError.THROTTLED}`) {
174+
if (
175+
(e as FirebaseError).code === `appCheck/${AppCheckError.THROTTLED}` ||
176+
(e as FirebaseError).code === `appCheck/${AppCheckError.INITIAL_THROTTLE}`
177+
) {
168178
// Warn if throttled, but do not treat it as an error.
169179
logger.warn((e as FirebaseError).message);
180+
} else if (shouldLogErrors) {
181+
logger.error(e);
170182
}
171183
// Always save error to be added to dummy token.
172184
error = e as FirebaseError;

0 commit comments

Comments
 (0)