Skip to content

Use a const enum for ErrorCode #1646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/messaging/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ import { FirebaseMessaging } from '@firebase/messaging-types';

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

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

const factoryMethod: FirebaseServiceFactory = app => {
if (!isSupported()) {
throw errorFactory.create(ERROR_CODES.UNSUPPORTED_BROWSER);
throw errorFactory.create(ErrorCode.UNSUPPORTED_BROWSER);
}

if (self && 'ServiceWorkerGlobalScope' in self) {
Expand Down
18 changes: 9 additions & 9 deletions packages/messaging/src/controllers/base-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
import { isArrayBufferEqual } from '../helpers/is-array-buffer-equal';
import { MessagePayload } from '../interfaces/message-payload';
import { TokenDetails } from '../interfaces/token-details';
import { ERROR_CODES, errorFactory } from '../models/errors';
import { ErrorCode, errorFactory } from '../models/errors';
import { IidModel } from '../models/iid-model';
import { TokenDetailsModel } from '../models/token-details-model';
import { VapidDetailsModel } from '../models/vapid-details-model';
Expand Down Expand Up @@ -57,7 +57,7 @@ export abstract class BaseController implements FirebaseMessaging {
!app.options[SENDER_ID_OPTION_NAME] ||
typeof app.options[SENDER_ID_OPTION_NAME] !== 'string'
) {
throw errorFactory.create(ERROR_CODES.BAD_SENDER_ID);
throw errorFactory.create(ErrorCode.BAD_SENDER_ID);
}

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

requestPermission(): Promise<void> {
throw errorFactory.create(ERROR_CODES.AVAILABLE_IN_WINDOW);
throw errorFactory.create(ErrorCode.AVAILABLE_IN_WINDOW);
}

useServiceWorker(registration: ServiceWorkerRegistration): void {
throw errorFactory.create(ERROR_CODES.AVAILABLE_IN_WINDOW);
throw errorFactory.create(ErrorCode.AVAILABLE_IN_WINDOW);
}

usePublicVapidKey(b64PublicKey: string): void {
throw errorFactory.create(ERROR_CODES.AVAILABLE_IN_WINDOW);
throw errorFactory.create(ErrorCode.AVAILABLE_IN_WINDOW);
}

onMessage(
nextOrObserver: NextFn<object> | Observer<object>,
error?: ErrorFn,
completed?: CompleteFn
): Unsubscribe {
throw errorFactory.create(ERROR_CODES.AVAILABLE_IN_WINDOW);
throw errorFactory.create(ErrorCode.AVAILABLE_IN_WINDOW);
}

onTokenRefresh(
nextOrObserver: NextFn<object> | Observer<object>,
error?: ErrorFn,
completed?: CompleteFn
): Unsubscribe {
throw errorFactory.create(ERROR_CODES.AVAILABLE_IN_WINDOW);
throw errorFactory.create(ErrorCode.AVAILABLE_IN_WINDOW);
}

//
// The following methods are used by the service worker only.
//

setBackgroundMessageHandler(callback: BgMessageHandler): void {
throw errorFactory.create(ERROR_CODES.AVAILABLE_IN_SW);
throw errorFactory.create(ErrorCode.AVAILABLE_IN_SW);
}

//
Expand Down
10 changes: 5 additions & 5 deletions packages/messaging/src/controllers/sw-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
MessagePayload,
NotificationDetails
} from '../interfaces/message-payload';
import { ERROR_CODES, errorFactory } from '../models/errors';
import { ErrorCode, errorFactory } from '../models/errors';
import { DEFAULT_PUBLIC_VAPID_KEY } from '../models/fcm-details';
import {
InternalMessage,
Expand Down Expand Up @@ -135,7 +135,7 @@ export class SwController extends BaseController {
try {
registration = await this.getSWRegistration_();
} catch (err) {
throw errorFactory.create(ERROR_CODES.UNABLE_TO_RESUBSCRIBE, {
throw errorFactory.create(ErrorCode.UNABLE_TO_RESUBSCRIBE, {
message: err
});
}
Expand Down Expand Up @@ -269,7 +269,7 @@ export class SwController extends BaseController {
*/
setBackgroundMessageHandler(callback: BgMessageHandler): void {
if (!callback || typeof callback !== 'function') {
throw errorFactory.create(ERROR_CODES.BG_HANDLER_FUNCTION_EXPECTED);
throw errorFactory.create(ErrorCode.BG_HANDLER_FUNCTION_EXPECTED);
}

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

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

const vapidKeyFromDatabase = await this.getVapidDetailsModel().getVapidFromSWScope(
Expand Down
26 changes: 13 additions & 13 deletions packages/messaging/src/controllers/window-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {

import { base64ToArrayBuffer } from '../helpers/base64-to-array-buffer';
import { DEFAULT_SW_PATH, DEFAULT_SW_SCOPE } from '../models/default-sw';
import { ERROR_CODES, errorFactory } from '../models/errors';
import { ErrorCode, errorFactory } from '../models/errors';
import { DEFAULT_PUBLIC_VAPID_KEY } from '../models/fcm-details';
import {
InternalMessage,
Expand Down Expand Up @@ -102,9 +102,9 @@ export class WindowController extends BaseController {
if (permissionResult === 'granted') {
return;
} else if (permissionResult === 'denied') {
throw errorFactory.create(ERROR_CODES.PERMISSION_BLOCKED);
throw errorFactory.create(ErrorCode.PERMISSION_BLOCKED);
} else {
throw errorFactory.create(ERROR_CODES.PERMISSION_DEFAULT);
throw errorFactory.create(ErrorCode.PERMISSION_DEFAULT);
}
}

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

if (this.registrationToUse != null) {
throw errorFactory.create(ERROR_CODES.USE_SW_BEFORE_GET_TOKEN);
throw errorFactory.create(ErrorCode.USE_SW_BEFORE_GET_TOKEN);
}

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

if (this.publicVapidKeyToUse != null) {
throw errorFactory.create(ERROR_CODES.USE_PUBLIC_KEY_BEFORE_GET_TOKEN);
throw errorFactory.create(ErrorCode.USE_PUBLIC_KEY_BEFORE_GET_TOKEN);
}

const parsedKey = base64ToArrayBuffer(publicKey);

if (parsedKey.length !== 65) {
throw errorFactory.create(ERROR_CODES.PUBLIC_KEY_DECRYPTION_FAILED);
throw errorFactory.create(ErrorCode.PUBLIC_KEY_DECRYPTION_FAILED);
}

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

if (serviceWorker.state === 'redundant') {
reject(errorFactory.create(ERROR_CODES.SW_REG_REDUNDANT));
reject(errorFactory.create(ErrorCode.SW_REG_REDUNDANT));
return;
}

const stateChangeListener = () => {
if (serviceWorker.state === 'activated') {
resolve(registration);
} else if (serviceWorker.state === 'redundant') {
reject(errorFactory.create(ERROR_CODES.SW_REG_REDUNDANT));
reject(errorFactory.create(ErrorCode.SW_REG_REDUNDANT));
} else {
// Return early and wait to next state change
return;
Expand Down Expand Up @@ -255,7 +255,7 @@ export class WindowController extends BaseController {
scope: DEFAULT_SW_SCOPE
})
.catch((err: Error) => {
throw errorFactory.create(ERROR_CODES.FAILED_DEFAULT_REGISTRATION, {
throw errorFactory.create(ErrorCode.FAILED_DEFAULT_REGISTRATION, {
browserErrorMessage: err.message
});
})
Expand Down Expand Up @@ -351,6 +351,6 @@ export async function manifestCheck(): Promise<void> {
}

if (manifestContent.gcm_sender_id !== '103953800507') {
throw errorFactory.create(ERROR_CODES.INCORRECT_GCM_SENDER_ID);
throw errorFactory.create(ErrorCode.INCORRECT_GCM_SENDER_ID);
}
}
Loading