Skip to content

Commit

Permalink
chore: improve s3 error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jsetton committed Nov 10, 2023
1 parent 12c0f97 commit 3bd4560
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
13 changes: 9 additions & 4 deletions lib/builtins/deploy-delegates/cfn-deployer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ async function bootstrap(options, callback) {
fs.writeFileSync(templateLocation, templateContent);
userConfig.templatePath = `./${path.posix.join("infrastructure", path.basename(workspacePath), SKILL_STACK_PUBLIC_FILE_NAME)}`;
updatedUserConfig = R.set(R.lensPath(["awsRegion"]), awsDefaultRegion, userConfig);
} catch (e) {
return callback(e.message);
} catch (err) {
return callback(err.message);
}
callback(null, {userConfig: updatedUserConfig});
}
Expand Down Expand Up @@ -96,7 +96,12 @@ async function _deploy(reporter, options, deployProgress) {
templateBody.includes("SkillClientId") && templateBody.includes("SkillClientSecret") ? await helper.getSkillCredentials(skillId) : {};

// s3 upload
const uploadResult = await helper.uploadToS3(bucketName, bucketKey, code.codeBuild);
let uploadResult;
try {
uploadResult = await helper.uploadToS3(bucketName, bucketKey, code.codeBuild);
} catch (err) {
throw new CliCFNDeployerError(`Failed to upload code build to S3: ${err.message}`);
}

deployProgress.deployState.s3 = {
bucket: bucketName,
Expand Down Expand Up @@ -212,7 +217,7 @@ function _getUserDefinedParameters(alexaRegion, userConfig) {
Object.keys(parameters).forEach((key) => {
if (reservedParametersKeys.has(key)) {
const message = reservedParameters[key];
throw new CliCFNDeployerError(`Cloud Formation parameter "${key}" is reserved. ${message}`);
throw new CliCFNDeployerError(`CloudFormation parameter "${key}" is reserved. ${message}`);
}
});

Expand Down
21 changes: 17 additions & 4 deletions test/unit/builtins/cfn-deployer/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe("Builtins test - cfn-deployer index test", () => {
const endpointUri = "some endpoint uri";
const userDefinedParamKey = "someKey";
const userDefinedParamValue = "someValue";
let deployStackStub, getAWSProfileStub;
let deployStackStub, getAWSProfileStub, uploadToS3Stub;

describe("bootstrap", () => {
const bootstrapOptions = {
Expand Down Expand Up @@ -118,7 +118,7 @@ describe("Builtins test - cfn-deployer index test", () => {
};
getAWSProfileStub = sinon.stub(awsUtil, "getAWSProfile").returns("some profile");
sinon.stub(Helper.prototype, "getSkillCredentials").resolves({clientId: "id", clientSecret: "secret"});
sinon.stub(Helper.prototype, "uploadToS3").resolves({VersionId: s3VersionId});
uploadToS3Stub = sinon.stub(Helper.prototype, "uploadToS3").resolves({VersionId: s3VersionId});
deployStackStub = sinon.stub(Helper.prototype, "deployStack");
});

Expand Down Expand Up @@ -227,13 +227,26 @@ describe("Builtins test - cfn-deployer index test", () => {
});
});

it("should throw error when failed to upload to s3", (done) => {
const errorMessage = "some error";
uploadToS3Stub.rejects(new Error(errorMessage));

Deployer.invoke({}, deployOptions, (err, result) => {
expectedErrorOutput.resultMessage =
`The CloudFormation deploy failed for Alexa region "${alexaRegion}": Failed to upload code build to S3: ${errorMessage}`;
expect(err).eql(null);
expect(result).eql(expectedErrorOutput);
done();
});
});

it("should throw error when reserved parameter is used", (done) => {
deployOptions.userConfig.cfn.parameters.SkillId = "reserved parameter value";

Deployer.invoke({}, deployOptions, (err, result) => {
expectedErrorOutput.resultMessage =
`The CloudFormation deploy failed for Alexa region "${alexaRegion}": ` +
'Cloud Formation parameter "SkillId" is reserved. Please use a different name.';
'CloudFormation parameter "SkillId" is reserved. Please use a different name.';
expect(err).eql(null);
expect(result).eql(expectedErrorOutput);
done();
Expand All @@ -245,7 +258,7 @@ describe("Builtins test - cfn-deployer index test", () => {

Deployer.invoke({}, deployOptions, (err, result) => {
expectedErrorOutput.resultMessage =
`The CloudFormation deploy failed for Alexa region "${alexaRegion}": ` + "The template path in userConfig must be provided.";
`The CloudFormation deploy failed for Alexa region "${alexaRegion}": The template path in userConfig must be provided.`;
expect(err).eql(null);
expect(result).eql(expectedErrorOutput);
done();
Expand Down

0 comments on commit 3bd4560

Please sign in to comment.