Skip to content

Commit 1ac6490

Browse files
committed
Fix surfacing of public getToken errors
1 parent a24a76a commit 1ac6490

File tree

5 files changed

+10
-27
lines changed

5 files changed

+10
-27
lines changed

packages/app-check/src/api.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ export async function getToken(
209209
if (result.error) {
210210
throw result.error;
211211
}
212+
if (result.internalError) {
213+
throw result.internalError;
214+
}
212215
return { token: result.token };
213216
}
214217

packages/app-check/src/errors.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ export const enum AppCheckError {
2727
STORAGE_GET = 'storage-get',
2828
STORAGE_WRITE = 'storage-set',
2929
RECAPTCHA_ERROR = 'recaptcha-error',
30-
THROTTLED = 'throttled'
30+
THROTTLED = 'throttled',
31+
INITIAL_THROTTLE = 'initial-throttle'
3132
}
3233

3334
const ERRORS: ErrorMap<AppCheckError> = {
@@ -54,7 +55,8 @@ const ERRORS: ErrorMap<AppCheckError> = {
5455
[AppCheckError.STORAGE_WRITE]:
5556
'Error thrown when writing to storage. Original error: {$originalErrorMessage}.',
5657
[AppCheckError.RECAPTCHA_ERROR]: 'ReCAPTCHA error.',
57-
[AppCheckError.THROTTLED]: `Requests throttled due to {$httpStatus} error. Attempts allowed again after {$time}`
58+
[AppCheckError.INITIAL_THROTTLE]: `{$httpStatus} error. Attempts allowed again after {$time}`,
59+
[AppCheckError.THROTTLED]: `Requests throttled due to previous {$httpStatus} error. Attempts allowed again after {$time}`
5860
};
5961

6062
interface ErrorParams {
@@ -67,6 +69,7 @@ interface ErrorParams {
6769
[AppCheckError.STORAGE_GET]: { originalErrorMessage?: string };
6870
[AppCheckError.STORAGE_WRITE]: { originalErrorMessage?: string };
6971
[AppCheckError.THROTTLED]: { time: string; httpStatus: number };
72+
[AppCheckError.INITIAL_THROTTLE]: { time: string; httpStatus: number };
7073
}
7174

7275
export const ERROR_FACTORY = new ErrorFactory<AppCheckError, ErrorParams>(

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

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ 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');
157156
const appCheck = initializeAppCheck(app, {
158157
provider: new ReCaptchaV3Provider(FAKE_SITE_KEY)
159158
});
@@ -170,14 +169,9 @@ describe('internal api', () => {
170169
token: formatDummyToken(defaultTokenErrorData),
171170
error
172171
});
173-
expect(errorStub.args[0][1].message).to.include(
174-
'oops, something went wrong'
175-
);
176-
errorStub.restore();
177172
});
178173

179174
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');
181175
window.FIREBASE_APPCHECK_DEBUG_TOKEN = true;
182176
const appCheck = initializeAppCheck(app, {
183177
provider: new ReCaptchaV3Provider(FAKE_SITE_KEY)
@@ -192,15 +186,10 @@ describe('internal api', () => {
192186
token: formatDummyToken(defaultTokenErrorData),
193187
error
194188
});
195-
expect(errorStub.args[0][1].message).to.include(
196-
'oops, something went wrong'
197-
);
198189
delete window.FIREBASE_APPCHECK_DEBUG_TOKEN;
199-
errorStub.restore();
200190
});
201191

202192
it('resolves with a dummy token and an error if recaptcha failed', async () => {
203-
const errorStub = stub(console, 'error');
204193
const appCheck = initializeAppCheck(app, {
205194
provider: new ReCaptchaV3Provider(FAKE_SITE_KEY)
206195
});
@@ -213,10 +202,6 @@ describe('internal api', () => {
213202
expect(reCAPTCHASpy).to.be.called;
214203
expect(exchangeTokenStub).to.not.be.called;
215204
expect(token.token).to.equal(formatDummyToken(defaultTokenErrorData));
216-
expect(errorStub.args[0][1].message).to.include(
217-
AppCheckError.RECAPTCHA_ERROR
218-
);
219-
errorStub.restore();
220205
});
221206

222207
it('notifies listeners using cached token', async () => {
@@ -290,7 +275,6 @@ describe('internal api', () => {
290275
});
291276

292277
it('calls 3P error handler if there is an error getting a token', async () => {
293-
stub(console, 'error');
294278
const appCheck = initializeAppCheck(app, {
295279
provider: new ReCaptchaV3Provider(FAKE_SITE_KEY),
296280
isTokenAutoRefreshEnabled: true
@@ -314,7 +298,6 @@ describe('internal api', () => {
314298
});
315299

316300
it('ignores listeners that throw', async () => {
317-
stub(console, 'error');
318301
const appCheck = initializeAppCheck(app, {
319302
provider: new ReCaptchaV3Provider(FAKE_SITE_KEY),
320303
isTokenAutoRefreshEnabled: true

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,6 @@ export async function getToken(
139139
if ((e as FirebaseError).code === `appCheck/${AppCheckError.THROTTLED}`) {
140140
// Warn if throttled, but do not treat it as an error.
141141
logger.warn((e as FirebaseError).message);
142-
} else {
143-
// `getToken()` should never throw, but logging error text to console will aid debugging.
144-
logger.error(e);
145142
}
146143
// Return dummy token and error
147144
return makeDummyTokenResult(e as FirebaseError);
@@ -170,9 +167,6 @@ export async function getToken(
170167
if ((e as FirebaseError).code === `appCheck/${AppCheckError.THROTTLED}`) {
171168
// Warn if throttled, but do not treat it as an error.
172169
logger.warn((e as FirebaseError).message);
173-
} else {
174-
// `getToken()` should never throw, but logging error text to console will aid debugging.
175-
logger.error(e);
176170
}
177171
// Always save error to be added to dummy token.
178172
error = e as FirebaseError;

packages/app-check/src/providers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export class ReCaptchaV3Provider implements AppCheckProvider {
9292
Number((e as FirebaseError).customData?.httpStatus),
9393
this._throttleData
9494
);
95-
throw ERROR_FACTORY.create(AppCheckError.THROTTLED, {
95+
throw ERROR_FACTORY.create(AppCheckError.INITIAL_THROTTLE, {
9696
time: getDurationString(
9797
this._throttleData.allowRequestsAfter - Date.now()
9898
),
@@ -185,7 +185,7 @@ export class ReCaptchaEnterpriseProvider implements AppCheckProvider {
185185
Number((e as FirebaseError).customData?.httpStatus),
186186
this._throttleData
187187
);
188-
throw ERROR_FACTORY.create(AppCheckError.THROTTLED, {
188+
throw ERROR_FACTORY.create(AppCheckError.INITIAL_THROTTLE, {
189189
time: getDurationString(
190190
this._throttleData.allowRequestsAfter - Date.now()
191191
),

0 commit comments

Comments
 (0)