Skip to content

Commit

Permalink
Update error handling for TS v4.4 unknown variables (#6355)
Browse files Browse the repository at this point in the history
* Update error types in catch blocks

* Remove uncessary type cast
  • Loading branch information
dwyfrequency committed Jun 13, 2022
1 parent 835f1d4 commit 8f82660
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion packages/analytics/src/initialize-analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async function validateIndexedDB(): Promise<boolean> {
} catch (e) {
logger.warn(
ERROR_FACTORY.create(AnalyticsError.INDEXEDDB_UNAVAILABLE, {
errorInfo: e
errorInfo: (e as Error)?.toString()
}).message
);
return false;
Expand Down
2 changes: 1 addition & 1 deletion packages/auth-compat/src/user_credential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function attachExtraErrorFields(auth: exp.Auth, e: FirebaseError): void {
// actually match the underlying type
const response = (e.customData as exp.TaggedWithTokenResponse | undefined)
?._tokenResponse as unknown as Record<string, string>;
if (e.code === 'auth/multi-factor-auth-required') {
if ((e as FirebaseError)?.code === 'auth/multi-factor-auth-required') {
const mfaErr = e as compat.MultiFactorError;
mfaErr.resolver = new MultiFactorResolver(
auth,
Expand Down
3 changes: 2 additions & 1 deletion packages/auth/src/core/auth/auth_impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
import {
createSubscribe,
ErrorFactory,
FirebaseError,
getModularInstance,
Observer,
Subscribe
Expand Down Expand Up @@ -301,7 +302,7 @@ export class AuthImpl implements AuthInternal, _FirebaseService {
try {
await _reloadWithoutSaving(user);
} catch (e) {
if (e.code !== `auth/${AuthErrorCode.NETWORK_REQUEST_FAILED}`) {
if ((e as FirebaseError)?.code !== `auth/${AuthErrorCode.NETWORK_REQUEST_FAILED}`) {
// Something's wrong with the user's token. Log them out and remove
// them from storage
return this.directlySetCurrentUser(null);
Expand Down
2 changes: 1 addition & 1 deletion packages/auth/src/core/user/id_token_result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export function _parseToken(token: string): ParsedToken | null {
}
return JSON.parse(decoded);
} catch (e) {
_logError('Caught error parsing JWT payload as JSON', e);
_logError('Caught error parsing JWT payload as JSON', (e as Error)?.toString());
return null;
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/auth/src/core/user/proactive_refresh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* limitations under the License.
*/

import { FirebaseError } from '@firebase/util';
import { UserInternal } from '../../model/user';
import { AuthErrorCode } from '../errors';

Expand Down Expand Up @@ -92,7 +93,7 @@ export class ProactiveRefresh {
await this.user.getIdToken(true);
} catch (e) {
// Only retry on network errors
if (e.code === `auth/${AuthErrorCode.NETWORK_REQUEST_FAILED}`) {
if ((e as FirebaseError)?.code === `auth/${AuthErrorCode.NETWORK_REQUEST_FAILED}`) {
this.schedule(/* wasError */ true);
}

Expand Down
3 changes: 2 additions & 1 deletion packages/auth/src/core/user/reauthenticate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* limitations under the License.
*/

import { FirebaseError } from '@firebase/util';
import { _processCredentialSavingMfaContextIfNecessary } from '../../mfa/mfa_error';
import { OperationType } from '../../model/enums';
import { UserInternal } from '../../model/user';
Expand Down Expand Up @@ -54,7 +55,7 @@ export async function _reauthenticate(
return UserCredentialImpl._forOperation(user, operationType, response);
} catch (e) {
// Convert user deleted error into user mismatch
if (e?.code === `auth/${AuthErrorCode.USER_DELETED}`) {
if ((e as FirebaseError)?.code === `auth/${AuthErrorCode.USER_DELETED}`) {
_fail(auth, AuthErrorCode.USER_MISMATCH);
}
throw e;
Expand Down
4 changes: 2 additions & 2 deletions packages/auth/src/mfa/mfa_user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { UserInternal } from '../model/user';
import { MultiFactorAssertionImpl } from './mfa_assertion';
import { MultiFactorInfoImpl } from './mfa_info';
import { MultiFactorSessionImpl } from './mfa_session';
import { getModularInstance } from '@firebase/util';
import { FirebaseError, getModularInstance } from '@firebase/util';

export class MultiFactorUserImpl implements MultiFactorUser {
enrolledFactors: MultiFactorInfo[] = [];
Expand Down Expand Up @@ -94,7 +94,7 @@ export class MultiFactorUserImpl implements MultiFactorUser {
try {
await this.user.reload();
} catch (e) {
if (e.code !== `auth/${AuthErrorCode.TOKEN_EXPIRED}`) {
if ((e as FirebaseError)?.code !== `auth/${AuthErrorCode.TOKEN_EXPIRED}`) {
throw e;
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/auth/src/platform_browser/providers/phone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export class PhoneAuthProvider {
* auth.currentUser,
* PhoneAuthProvider.credential(verificationId, code));
* } catch (e) {
* if (e.code === 'auth/account-exists-with-different-credential') {
* if ((e as FirebaseError)?.code === 'auth/account-exists-with-different-credential') {
* const cred = PhoneAuthProvider.credentialFromError(e);
* await linkWithCredential(auth.currentUser, cred);
* }
Expand Down
2 changes: 1 addition & 1 deletion packages/util/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
*
* catch (e) {
* assert(e.message === "Could not find file: foo.txt.");
* if (e.code === 'service/file-not-found') {
* if ((e as FirebaseError)?.code === 'service/file-not-found') {
* console.log("Could not read file: " + e['file']);
* }
* }
Expand Down
8 changes: 4 additions & 4 deletions packages/util/test/errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ describe('FirebaseError', () => {
const e = ERROR_FACTORY.create('generic-error');
assert.instanceOf(e, Error);
assert.instanceOf(e, FirebaseError);
assert.equal(e.code, 'fake/generic-error');
assert.equal((e as FirebaseError)?.code, 'fake/generic-error');
assert.equal(e.message, 'Fake: Unknown error (fake/generic-error).');
});

it('replaces template values with data', () => {
const e = ERROR_FACTORY.create('file-not-found', { file: 'foo.txt' });
assert.equal(e.code, 'fake/file-not-found');
assert.equal((e as FirebaseError)?.code, 'fake/file-not-found');
assert.equal(
e.message,
"Fake: Could not find file: 'foo.txt' (fake/file-not-found)."
Expand All @@ -65,15 +65,15 @@ describe('FirebaseError', () => {
it('uses "Error" as template when template is missing', () => {
// Cast to avoid compile-time error.
const e = ERROR_FACTORY.create('no-such-code' as any as ErrorCode);
assert.equal(e.code, 'fake/no-such-code');
assert.equal((e as FirebaseError)?.code, 'fake/no-such-code');
assert.equal(e.message, 'Fake: Error (fake/no-such-code).');
});

it('uses the key in the template if the replacement is missing', () => {
const e = ERROR_FACTORY.create('file-not-found', {
fileX: 'foo.txt'
} as any);
assert.equal(e.code, 'fake/file-not-found');
assert.equal((e as FirebaseError)?.code, 'fake/file-not-found');
assert.equal(
e.message,
"Fake: Could not find file: '<file?>' (fake/file-not-found)."
Expand Down

0 comments on commit 8f82660

Please sign in to comment.