Skip to content

Commit 4a518e7

Browse files
committed
Add ErrorParams to packages that use ErrorFactory
1 parent 3cb7836 commit 4a518e7

File tree

8 files changed

+49
-20
lines changed

8 files changed

+49
-20
lines changed

packages/app/src/errors.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ const ERRORS: ErrorMap<AppError> = {
4040
'Firebase App instance.'
4141
};
4242

43-
const appErrors = new ErrorFactory<AppError>('app', 'Firebase', ERRORS);
43+
type ErrorParams = { [key in AppError]: { name: string } };
4444

45-
export function error(code: AppError, args?: { [name: string]: any }) {
46-
throw appErrors.create(code, args);
47-
}
45+
export const ERROR_FACTORY = new ErrorFactory<AppError, ErrorParams>(
46+
'app',
47+
'Firebase',
48+
ERRORS
49+
);

packages/app/src/firebaseApp.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727
FirebaseAppInternals
2828
} from '@firebase/app-types/private';
2929
import { deepCopy, deepExtend } from '@firebase/util';
30-
import { error, AppError } from './errors';
30+
import { AppError, ERROR_FACTORY } from './errors';
3131
import { DEFAULT_ENTRY_NAME } from './constants';
3232

3333
interface ServicesCache {
@@ -202,7 +202,7 @@ export class FirebaseAppImpl implements FirebaseApp {
202202
*/
203203
private checkDestroyed_(): void {
204204
if (this.isDeleted_) {
205-
error(AppError.APP_DELETED, { name: this.name_ });
205+
throw ERROR_FACTORY.create(AppError.APP_DELETED, { name: this.name_ });
206206
}
207207
}
208208
}

packages/app/src/firebaseNamespaceCore.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {
3131
} from '@firebase/app-types/private';
3232
import { deepExtend, patchProperty } from '@firebase/util';
3333
import { FirebaseAppImpl } from './firebaseApp';
34-
import { error, AppError } from './errors';
34+
import { ERROR_FACTORY, AppError } from './errors';
3535
import { FirebaseAppLiteImpl } from './lite/firebaseAppLite';
3636
import { DEFAULT_ENTRY_NAME } from './constants';
3737

@@ -104,7 +104,7 @@ export function createFirebaseNamespaceCore(
104104
function app(name?: string): FirebaseApp {
105105
name = name || DEFAULT_ENTRY_NAME;
106106
if (!contains(apps, name)) {
107-
error(AppError.NO_APP, { name: name });
107+
throw ERROR_FACTORY.create(AppError.NO_APP, { name: name });
108108
}
109109
return apps[name];
110110
}
@@ -133,11 +133,11 @@ export function createFirebaseNamespaceCore(
133133
const { name } = config;
134134

135135
if (typeof name !== 'string' || !name) {
136-
error(AppError.BAD_APP_NAME, { name: String(name) });
136+
throw ERROR_FACTORY.create(AppError.BAD_APP_NAME, { name: String(name) });
137137
}
138138

139139
if (contains(apps, name)) {
140-
error(AppError.DUPLICATE_APP, { name: name });
140+
throw ERROR_FACTORY.create(AppError.DUPLICATE_APP, { name: name });
141141
}
142142

143143
const app = new firebaseAppImpl(
@@ -176,7 +176,7 @@ export function createFirebaseNamespaceCore(
176176
): FirebaseServiceNamespace<FirebaseService> {
177177
// Cannot re-register a service that already exists
178178
if (factories[name]) {
179-
error(AppError.DUPLICATE_SERVICE, { name: name });
179+
throw ERROR_FACTORY.create(AppError.DUPLICATE_SERVICE, { name: name });
180180
}
181181

182182
// Capture the service factory for later service instantiation
@@ -197,7 +197,9 @@ export function createFirebaseNamespaceCore(
197197
if (typeof appArg[name] !== 'function') {
198198
// Invalid argument.
199199
// This happens in the following case: firebase.storage('gs:/')
200-
error(AppError.INVALID_APP_ARGUMENT, { name: name });
200+
throw ERROR_FACTORY.create(AppError.INVALID_APP_ARGUMENT, {
201+
name: name
202+
});
201203
}
202204

203205
// Forward service instance lookup to the FirebaseApp.

packages/app/src/lite/firebaseAppLite.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
FirebaseService
2727
} from '@firebase/app-types/private';
2828
import { deepCopy, deepExtend } from '@firebase/util';
29-
import { error, AppError } from '../errors';
29+
import { ERROR_FACTORY, AppError } from '../errors';
3030
import { DEFAULT_ENTRY_NAME } from '../constants';
3131

3232
interface ServicesCache {
@@ -168,7 +168,7 @@ export class FirebaseAppLiteImpl implements FirebaseApp {
168168
*/
169169
private checkDestroyed_(): void {
170170
if (this.isDeleted_) {
171-
error(AppError.APP_DELETED, { name: this.name_ });
171+
throw ERROR_FACTORY.create(AppError.APP_DELETED, { name: this.name_ });
172172
}
173173
}
174174
}

packages/installations/src/util/errors.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,13 @@ const ERROR_DESCRIPTION_MAP: { readonly [key in ErrorCode]: string } = {
4343
"Can't delete installation while there is a pending registration request."
4444
};
4545

46-
export const ERROR_FACTORY = new ErrorFactory(
46+
interface ErrorParams {
47+
[ErrorCode.REQUEST_FAILED]: {
48+
requestName: string;
49+
} & ServerErrorData;
50+
}
51+
52+
export const ERROR_FACTORY = new ErrorFactory<ErrorCode, ErrorParams>(
4753
SERVICE,
4854
SERVICE_NAME,
4955
ERROR_DESCRIPTION_MAP

packages/messaging/src/models/errors.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,15 @@ export const ERROR_MAP: ErrorMap<ErrorCode> = {
157157
'The public VAPID key did not equal 65 bytes when decrypted.'
158158
};
159159

160-
export const errorFactory = new ErrorFactory(
160+
interface ErrorParams {
161+
[ErrorCode.FAILED_DEFAULT_REGISTRATION]: { browserErrorMessage: string };
162+
[ErrorCode.TOKEN_SUBSCRIBE_FAILED]: { errorInfo: string };
163+
[ErrorCode.TOKEN_UNSUBSCRIBE_FAILED]: { errorInfo: string };
164+
[ErrorCode.TOKEN_UPDATE_FAILED]: { errorInfo: string };
165+
[ErrorCode.UNABLE_TO_RESUBSCRIBE]: { errorInfo: string };
166+
}
167+
168+
export const errorFactory = new ErrorFactory<ErrorCode, ErrorParams>(
161169
'messaging',
162170
'Messaging',
163171
ERROR_MAP

packages/messaging/src/models/iid-model.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ export class IidModel {
7272

7373
responseData = await response.json();
7474
} catch (err) {
75-
throw errorFactory.create(ErrorCode.TOKEN_SUBSCRIBE_FAILED);
75+
throw errorFactory.create(ErrorCode.TOKEN_SUBSCRIBE_FAILED, {
76+
errorInfo: err
77+
});
7678
}
7779

7880
if (responseData.error) {
@@ -144,7 +146,9 @@ export class IidModel {
144146
);
145147
responseData = await response.json();
146148
} catch (err) {
147-
throw errorFactory.create(ErrorCode.TOKEN_UPDATE_FAILED);
149+
throw errorFactory.create(ErrorCode.TOKEN_UPDATE_FAILED, {
150+
errorInfo: err
151+
});
148152
}
149153

150154
if (responseData.error) {
@@ -196,7 +200,9 @@ export class IidModel {
196200
});
197201
}
198202
} catch (err) {
199-
throw errorFactory.create(ErrorCode.TOKEN_UNSUBSCRIBE_FAILED);
203+
throw errorFactory.create(ErrorCode.TOKEN_UNSUBSCRIBE_FAILED, {
204+
errorInfo: err
205+
});
200206
}
201207
}
202208
}

packages/performance/src/utils/errors.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@ const ERROR_DESCRIPTION_MAP: { readonly [key in ErrorCode]: string } = {
4343
[ErrorCode.RC_NOT_OK]: 'RC response is not ok'
4444
};
4545

46-
export const ERROR_FACTORY = new ErrorFactory(
46+
interface ErrorParams {
47+
[ErrorCode.TRACE_STARTED_BEFORE]: { traceName: string };
48+
[ErrorCode.TRACE_STOPPED_BEFORE]: { traceName: string };
49+
}
50+
51+
export const ERROR_FACTORY = new ErrorFactory<ErrorCode, ErrorParams>(
4752
SERVICE,
4853
SERVICE_NAME,
4954
ERROR_DESCRIPTION_MAP

0 commit comments

Comments
 (0)