Skip to content

Commit

Permalink
fix: await unawaited pinpoint response (#11993)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdstolworthy committed Feb 13, 2023
1 parent f39cddb commit c47b505
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions packages/amplify-category-notifications/src/channel-fcm.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import {
$TSAny, $TSContext, AmplifyError, AmplifyFault,
} from 'amplify-cli-core';
import { $TSAny, $TSContext, AmplifyError, AmplifyFault } from 'amplify-cli-core';
import { printer, prompter } from 'amplify-prompts';
import ora from 'ora';
import { ChannelAction, ChannelConfigDeploymentType, IChannelAPIResponse } from './channel-types';
Expand Down Expand Up @@ -39,12 +37,12 @@ export const configure = async (context: $TSContext): Promise<void> => {
* @param context amplify cli context
* @param successMessage optional message to be displayed on successfully enabling channel for notifications
*/
export const enable = async (context: $TSContext, successMessage: string | undefined) : Promise<IChannelAPIResponse> => {
export const enable = async (context: $TSContext, successMessage: string | undefined): Promise<IChannelAPIResponse> => {
let answers;
if (context.exeInfo.pinpointInputParams?.[channelName]) {
answers = validateInputParams(context.exeInfo.pinpointInputParams[channelName]);
} else {
let channelOutput : $TSAny = {};
let channelOutput: $TSAny = {};
if (context.exeInfo.serviceMeta.output[channelName]) {
channelOutput = context.exeInfo.serviceMeta.output[channelName];
}
Expand All @@ -63,24 +61,23 @@ export const enable = async (context: $TSContext, successMessage: string | undef

spinner.start('Enabling FCM channel.');
try {
const data = context.exeInfo.pinpointClient.updateGcmChannel(params).promise();
const data = await context.exeInfo.pinpointClient.updateGcmChannel(params).promise();
spinner.succeed(successMessage ?? `The ${channelName} channel has been successfully enabled.`);
context.exeInfo.serviceMeta.output[channelName] = data.GCMChannelResponse;
return buildPinpointChannelResponseSuccess(
ChannelAction.ENABLE,
deploymentType,
channelName,
data.GCMChannelResponse,
);
return buildPinpointChannelResponseSuccess(ChannelAction.ENABLE, deploymentType, channelName, data.GCMChannelResponse);
} catch (err) {
spinner.stop();
throw new AmplifyFault('NotificationsChannelFCMFault', {
message: `Failed to enable the ${channelName} channel`,
}, err);
throw new AmplifyFault(
'NotificationsChannelFCMFault',
{
message: `Failed to enable the ${channelName} channel`,
},
err,
);
}
};

const validateInputParams = (channelInput: $TSAny):$TSAny => {
const validateInputParams = (channelInput: $TSAny): $TSAny => {
if (!channelInput.ApiKey) {
throw new AmplifyError('UserInputError', {
message: 'ApiKey is missing for the FCM channel',
Expand Down Expand Up @@ -125,9 +122,13 @@ export const disable = async (context: $TSContext): Promise<$TSAny> => {
return buildPinpointChannelResponseSuccess(ChannelAction.DISABLE, deploymentType, channelName, data.GCMChannelResponse);
} catch (err) {
spinner.stop();
throw new AmplifyFault('NotificationsChannelFCMFault', {
message: `Failed to disable the ${channelName} channel`,
}, err);
throw new AmplifyFault(
'NotificationsChannelFCMFault',
{
message: `Failed to disable the ${channelName} channel`,
},
err,
);
}
};

Expand All @@ -137,7 +138,7 @@ export const disable = async (context: $TSContext): Promise<$TSAny> => {
* @param pinpointApp Pinpoint resource metadata
* @returns GCMChannel response
*/
export const pull = async (context: $TSContext, pinpointApp: $TSAny):Promise<$TSAny> => {
export const pull = async (context: $TSContext, pinpointApp: $TSAny): Promise<$TSAny> => {
const params = {
ApplicationId: pinpointApp.Id,
};
Expand All @@ -152,9 +153,13 @@ export const pull = async (context: $TSContext, pinpointApp: $TSAny):Promise<$TS
} catch (err) {
spinner.stop();
if (err.code !== 'NotFoundException') {
throw new AmplifyFault('NotificationsChannelFCMFault', {
message: `Failed to retrieve channel information for ${channelName}`,
}, err);
throw new AmplifyFault(
'NotificationsChannelFCMFault',
{
message: `Failed to retrieve channel information for ${channelName}`,
},
err,
);
}

return undefined;
Expand Down

0 comments on commit c47b505

Please sign in to comment.