Skip to content

Commit d97b62c

Browse files
authored
Use a const enum for ErrorCode (#1646)
1 parent c2d7498 commit d97b62c

16 files changed

+199
-200
lines changed

packages/messaging/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ import { FirebaseMessaging } from '@firebase/messaging-types';
2424

2525
import { SwController } from './src/controllers/sw-controller';
2626
import { WindowController } from './src/controllers/window-controller';
27-
import { ERROR_CODES, errorFactory } from './src/models/errors';
27+
import { ErrorCode, errorFactory } from './src/models/errors';
2828

2929
export function registerMessaging(instance: _FirebaseNamespace): void {
3030
const messagingName = 'messaging';
3131

3232
const factoryMethod: FirebaseServiceFactory = app => {
3333
if (!isSupported()) {
34-
throw errorFactory.create(ERROR_CODES.UNSUPPORTED_BROWSER);
34+
throw errorFactory.create(ErrorCode.UNSUPPORTED_BROWSER);
3535
}
3636

3737
if (self && 'ServiceWorkerGlobalScope' in self) {

packages/messaging/src/controllers/base-controller.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929
import { isArrayBufferEqual } from '../helpers/is-array-buffer-equal';
3030
import { MessagePayload } from '../interfaces/message-payload';
3131
import { TokenDetails } from '../interfaces/token-details';
32-
import { ERROR_CODES, errorFactory } from '../models/errors';
32+
import { ErrorCode, errorFactory } from '../models/errors';
3333
import { IidModel } from '../models/iid-model';
3434
import { TokenDetailsModel } from '../models/token-details-model';
3535
import { VapidDetailsModel } from '../models/vapid-details-model';
@@ -57,7 +57,7 @@ export abstract class BaseController implements FirebaseMessaging {
5757
!app.options[SENDER_ID_OPTION_NAME] ||
5858
typeof app.options[SENDER_ID_OPTION_NAME] !== 'string'
5959
) {
60-
throw errorFactory.create(ERROR_CODES.BAD_SENDER_ID);
60+
throw errorFactory.create(ErrorCode.BAD_SENDER_ID);
6161
}
6262

6363
this.messagingSenderId = app.options[SENDER_ID_OPTION_NAME]!;
@@ -79,7 +79,7 @@ export abstract class BaseController implements FirebaseMessaging {
7979
// Check with permissions
8080
const currentPermission = this.getNotificationPermission_();
8181
if (currentPermission === 'denied') {
82-
throw errorFactory.create(ERROR_CODES.NOTIFICATIONS_BLOCKED);
82+
throw errorFactory.create(ErrorCode.NOTIFICATIONS_BLOCKED);
8383
} else if (currentPermission !== 'granted') {
8484
// We must wait for permission to be granted
8585
return null;
@@ -282,39 +282,39 @@ export abstract class BaseController implements FirebaseMessaging {
282282
//
283283

284284
requestPermission(): Promise<void> {
285-
throw errorFactory.create(ERROR_CODES.AVAILABLE_IN_WINDOW);
285+
throw errorFactory.create(ErrorCode.AVAILABLE_IN_WINDOW);
286286
}
287287

288288
useServiceWorker(registration: ServiceWorkerRegistration): void {
289-
throw errorFactory.create(ERROR_CODES.AVAILABLE_IN_WINDOW);
289+
throw errorFactory.create(ErrorCode.AVAILABLE_IN_WINDOW);
290290
}
291291

292292
usePublicVapidKey(b64PublicKey: string): void {
293-
throw errorFactory.create(ERROR_CODES.AVAILABLE_IN_WINDOW);
293+
throw errorFactory.create(ErrorCode.AVAILABLE_IN_WINDOW);
294294
}
295295

296296
onMessage(
297297
nextOrObserver: NextFn<object> | Observer<object>,
298298
error?: ErrorFn,
299299
completed?: CompleteFn
300300
): Unsubscribe {
301-
throw errorFactory.create(ERROR_CODES.AVAILABLE_IN_WINDOW);
301+
throw errorFactory.create(ErrorCode.AVAILABLE_IN_WINDOW);
302302
}
303303

304304
onTokenRefresh(
305305
nextOrObserver: NextFn<object> | Observer<object>,
306306
error?: ErrorFn,
307307
completed?: CompleteFn
308308
): Unsubscribe {
309-
throw errorFactory.create(ERROR_CODES.AVAILABLE_IN_WINDOW);
309+
throw errorFactory.create(ErrorCode.AVAILABLE_IN_WINDOW);
310310
}
311311

312312
//
313313
// The following methods are used by the service worker only.
314314
//
315315

316316
setBackgroundMessageHandler(callback: BgMessageHandler): void {
317-
throw errorFactory.create(ERROR_CODES.AVAILABLE_IN_SW);
317+
throw errorFactory.create(ErrorCode.AVAILABLE_IN_SW);
318318
}
319319

320320
//

packages/messaging/src/controllers/sw-controller.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
MessagePayload,
2424
NotificationDetails
2525
} from '../interfaces/message-payload';
26-
import { ERROR_CODES, errorFactory } from '../models/errors';
26+
import { ErrorCode, errorFactory } from '../models/errors';
2727
import { DEFAULT_PUBLIC_VAPID_KEY } from '../models/fcm-details';
2828
import {
2929
InternalMessage,
@@ -135,7 +135,7 @@ export class SwController extends BaseController {
135135
try {
136136
registration = await this.getSWRegistration_();
137137
} catch (err) {
138-
throw errorFactory.create(ERROR_CODES.UNABLE_TO_RESUBSCRIBE, {
138+
throw errorFactory.create(ErrorCode.UNABLE_TO_RESUBSCRIBE, {
139139
message: err
140140
});
141141
}
@@ -269,7 +269,7 @@ export class SwController extends BaseController {
269269
*/
270270
setBackgroundMessageHandler(callback: BgMessageHandler): void {
271271
if (!callback || typeof callback !== 'function') {
272-
throw errorFactory.create(ERROR_CODES.BG_HANDLER_FUNCTION_EXPECTED);
272+
throw errorFactory.create(ErrorCode.BG_HANDLER_FUNCTION_EXPECTED);
273273
}
274274

275275
this.bgMessageHandler = callback;
@@ -317,7 +317,7 @@ export class SwController extends BaseController {
317317
// NOTE: This returns a promise in case this API is abstracted later on to
318318
// do additional work
319319
if (!client) {
320-
throw errorFactory.create(ERROR_CODES.NO_WINDOW_CLIENT_TO_MSG);
320+
throw errorFactory.create(ErrorCode.NO_WINDOW_CLIENT_TO_MSG);
321321
}
322322

323323
client.postMessage(message);
@@ -376,7 +376,7 @@ export class SwController extends BaseController {
376376
async getPublicVapidKey_(): Promise<Uint8Array> {
377377
const swReg = await this.getSWRegistration_();
378378
if (!swReg) {
379-
throw errorFactory.create(ERROR_CODES.SW_REGISTRATION_EXPECTED);
379+
throw errorFactory.create(ErrorCode.SW_REGISTRATION_EXPECTED);
380380
}
381381

382382
const vapidKeyFromDatabase = await this.getVapidDetailsModel().getVapidFromSWScope(

packages/messaging/src/controllers/window-controller.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828

2929
import { base64ToArrayBuffer } from '../helpers/base64-to-array-buffer';
3030
import { DEFAULT_SW_PATH, DEFAULT_SW_SCOPE } from '../models/default-sw';
31-
import { ERROR_CODES, errorFactory } from '../models/errors';
31+
import { ErrorCode, errorFactory } from '../models/errors';
3232
import { DEFAULT_PUBLIC_VAPID_KEY } from '../models/fcm-details';
3333
import {
3434
InternalMessage,
@@ -102,9 +102,9 @@ export class WindowController extends BaseController {
102102
if (permissionResult === 'granted') {
103103
return;
104104
} else if (permissionResult === 'denied') {
105-
throw errorFactory.create(ERROR_CODES.PERMISSION_BLOCKED);
105+
throw errorFactory.create(ErrorCode.PERMISSION_BLOCKED);
106106
} else {
107-
throw errorFactory.create(ERROR_CODES.PERMISSION_DEFAULT);
107+
throw errorFactory.create(ErrorCode.PERMISSION_DEFAULT);
108108
}
109109
}
110110

@@ -117,11 +117,11 @@ export class WindowController extends BaseController {
117117
*/
118118
useServiceWorker(registration: ServiceWorkerRegistration): void {
119119
if (!(registration instanceof ServiceWorkerRegistration)) {
120-
throw errorFactory.create(ERROR_CODES.SW_REGISTRATION_EXPECTED);
120+
throw errorFactory.create(ErrorCode.SW_REGISTRATION_EXPECTED);
121121
}
122122

123123
if (this.registrationToUse != null) {
124-
throw errorFactory.create(ERROR_CODES.USE_SW_BEFORE_GET_TOKEN);
124+
throw errorFactory.create(ErrorCode.USE_SW_BEFORE_GET_TOKEN);
125125
}
126126

127127
this.registrationToUse = registration;
@@ -135,17 +135,17 @@ export class WindowController extends BaseController {
135135
*/
136136
usePublicVapidKey(publicKey: string): void {
137137
if (typeof publicKey !== 'string') {
138-
throw errorFactory.create(ERROR_CODES.INVALID_PUBLIC_VAPID_KEY);
138+
throw errorFactory.create(ErrorCode.INVALID_PUBLIC_VAPID_KEY);
139139
}
140140

141141
if (this.publicVapidKeyToUse != null) {
142-
throw errorFactory.create(ERROR_CODES.USE_PUBLIC_KEY_BEFORE_GET_TOKEN);
142+
throw errorFactory.create(ErrorCode.USE_PUBLIC_KEY_BEFORE_GET_TOKEN);
143143
}
144144

145145
const parsedKey = base64ToArrayBuffer(publicKey);
146146

147147
if (parsedKey.length !== 65) {
148-
throw errorFactory.create(ERROR_CODES.PUBLIC_KEY_DECRYPTION_FAILED);
148+
throw errorFactory.create(ErrorCode.PUBLIC_KEY_DECRYPTION_FAILED);
149149
}
150150

151151
this.publicVapidKeyToUse = parsedKey;
@@ -207,7 +207,7 @@ export class WindowController extends BaseController {
207207
return new Promise<ServiceWorkerRegistration>((resolve, reject) => {
208208
if (!serviceWorker) {
209209
// This is a rare scenario but has occured in firefox
210-
reject(errorFactory.create(ERROR_CODES.NO_SW_IN_REG));
210+
reject(errorFactory.create(ErrorCode.NO_SW_IN_REG));
211211
return;
212212
}
213213
// Because the Promise function is called on next tick there is a
@@ -218,15 +218,15 @@ export class WindowController extends BaseController {
218218
}
219219

220220
if (serviceWorker.state === 'redundant') {
221-
reject(errorFactory.create(ERROR_CODES.SW_REG_REDUNDANT));
221+
reject(errorFactory.create(ErrorCode.SW_REG_REDUNDANT));
222222
return;
223223
}
224224

225225
const stateChangeListener = () => {
226226
if (serviceWorker.state === 'activated') {
227227
resolve(registration);
228228
} else if (serviceWorker.state === 'redundant') {
229-
reject(errorFactory.create(ERROR_CODES.SW_REG_REDUNDANT));
229+
reject(errorFactory.create(ErrorCode.SW_REG_REDUNDANT));
230230
} else {
231231
// Return early and wait to next state change
232232
return;
@@ -255,7 +255,7 @@ export class WindowController extends BaseController {
255255
scope: DEFAULT_SW_SCOPE
256256
})
257257
.catch((err: Error) => {
258-
throw errorFactory.create(ERROR_CODES.FAILED_DEFAULT_REGISTRATION, {
258+
throw errorFactory.create(ErrorCode.FAILED_DEFAULT_REGISTRATION, {
259259
browserErrorMessage: err.message
260260
});
261261
})
@@ -351,6 +351,6 @@ export async function manifestCheck(): Promise<void> {
351351
}
352352

353353
if (manifestContent.gcm_sender_id !== '103953800507') {
354-
throw errorFactory.create(ERROR_CODES.INCORRECT_GCM_SENDER_ID);
354+
throw errorFactory.create(ErrorCode.INCORRECT_GCM_SENDER_ID);
355355
}
356356
}

0 commit comments

Comments
 (0)