Skip to content

Commit

Permalink
Use ResourceNotFoundException instead of checking error type
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcasalboni committed Jan 8, 2024
1 parent 2cf10b4 commit 2242632
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 19 deletions.
3 changes: 2 additions & 1 deletion lambda/cleaner.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const { ResourceNotFoundException } = require("@aws-sdk/client-lambda");
const utils = require('./utils');

/**
Expand Down Expand Up @@ -39,7 +40,7 @@ const cleanup = async(lambdaARN, alias) => {
await utils.deleteLambdaAlias(lambdaARN, alias);
await utils.deleteLambdaVersion(lambdaARN, FunctionVersion);
} catch (error) {
if (error.code === 'ResourceNotFoundException') {
if (error instanceof ResourceNotFoundException) {
console.error('OK, even if version/alias was not found');
console.error(error);
} else {
Expand Down
5 changes: 3 additions & 2 deletions lambda/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { CreateAliasCommand, DeleteAliasCommand, DeleteFunctionCommand, GetAliasCommand, GetFunctionConfigurationCommand, InvokeCommand, LambdaClient, PublishVersionCommand, UpdateAliasCommand, UpdateFunctionConfigurationCommand, waitUntilFunctionActive, waitUntilFunctionUpdated } = require("@aws-sdk/client-lambda");
const { CreateAliasCommand, DeleteAliasCommand, DeleteFunctionCommand, GetAliasCommand, GetFunctionConfigurationCommand, InvokeCommand, LambdaClient, PublishVersionCommand, UpdateAliasCommand, UpdateFunctionConfigurationCommand, waitUntilFunctionActive, waitUntilFunctionUpdated, ResourceNotFoundException } = require("@aws-sdk/client-lambda");
const { GetObjectCommand, S3Client } = require("@aws-sdk/client-s3");
const url = require('url');

Expand Down Expand Up @@ -56,7 +56,8 @@ module.exports.verifyAliasExistance = async(lambdaARN, alias) => {
await utils.getLambdaAlias(lambdaARN, alias);
return true;
} catch (error) {
if (error.code === 'ResourceNotFoundException') {
console.log("Error during verifyAlias (probably OK!)")
if (error instanceof ResourceNotFoundException) {
// OK, the alias isn't supposed to exist
console.log('OK, even if missing alias ');
return false;
Expand Down
19 changes: 6 additions & 13 deletions test/unit/test-lambda.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const sinon = require('sinon');
const expect = require('expect.js');

var awsV3Mock = require('aws-sdk-client-mock');
const { CreateAliasCommand, DeleteAliasCommand, DeleteFunctionCommand, GetAliasCommand, InvokeCommand, LambdaClient, PublishVersionCommand, UpdateAliasCommand, UpdateFunctionConfigurationCommand } = require("@aws-sdk/client-lambda");
const { CreateAliasCommand, DeleteAliasCommand, DeleteFunctionCommand, GetAliasCommand, InvokeCommand, LambdaClient, PublishVersionCommand, UpdateAliasCommand, UpdateFunctionConfigurationCommand, ResourceNotFoundException } = require("@aws-sdk/client-lambda");

const utils = require('../../lambda/utils');

Expand Down Expand Up @@ -103,8 +103,7 @@ describe('Lambda Functions', async() => {
});
getLambdaAliasStub = sandBox.stub(utils, 'getLambdaAlias')
.callsFake(async() => {
const error = new Error('alias is not defined');
error.code = 'ResourceNotFoundException';
const error = new ResourceNotFoundException('alias is not defined');
throw error;
});
sandBox.stub(utils, 'getLambdaPower')
Expand Down Expand Up @@ -224,8 +223,7 @@ describe('Lambda Functions', async() => {
if (alias === 'RAM128') {
return { FunctionVersion: '1' };
} else {
const error = new Error('alias is not defined');
error.code = 'ResourceNotFoundException';
const error = new ResourceNotFoundException('alias is not defined');
throw error;
}
});
Expand Down Expand Up @@ -262,7 +260,6 @@ describe('Lambda Functions', async() => {
getLambdaAliasStub = sandBox.stub(utils, 'getLambdaAlias')
.callsFake(async() => {
const error = new Error('very bad error');
error.code = 'VeryBadError';
throw error;
});
await invokeForFailure(handler, { lambdaARN: 'arnOK', num: 5 });
Expand Down Expand Up @@ -322,8 +319,7 @@ describe('Lambda Functions', async() => {
deleteLambdaVersionStub && deleteLambdaVersionStub.restore();
deleteLambdaVersionStub = sandBox.stub(utils, 'deleteLambdaVersion')
.callsFake(async() => {
const error = new Error('version is not defined');
error.code = 'ResourceNotFoundException';
const error = new ResourceNotFoundException('version is not defined');
throw error;
});
await invokeForSuccess(handler, eventOK);
Expand All @@ -333,8 +329,7 @@ describe('Lambda Functions', async() => {
deleteLambdaAliasStub && deleteLambdaAliasStub.restore();
deleteLambdaAliasStub = sandBox.stub(utils, 'deleteLambdaAlias')
.callsFake(async() => {
const error = new Error('alias is not defined');
error.code = 'ResourceNotFoundException';
const error = new ResourceNotFoundException('alias is not defined');
throw error;
});
await invokeForSuccess(handler, eventOK);
Expand All @@ -345,7 +340,6 @@ describe('Lambda Functions', async() => {
deleteLambdaVersionStub = sandBox.stub(utils, 'deleteLambdaVersion')
.callsFake(async() => {
const error = new Error('very bad error');
error.code = 'VeryBadError';
throw error;
});
await invokeForFailure(handler, eventOK);
Expand Down Expand Up @@ -1664,8 +1658,7 @@ describe('Lambda Functions', async() => {
getLambdaAliasStub && getLambdaAliasStub.restore();
getLambdaAliasStub = sandBox.stub(utils, 'getLambdaAlias')
.callsFake(async() => {
const error = new Error('alias is not defined');
error.code = 'ResourceNotFoundException';
const error = new ResourceNotFoundException('alias is not defined');
throw error;
});
setLambdaPowerStub && setLambdaPowerStub.restore();
Expand Down
5 changes: 2 additions & 3 deletions test/unit/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const expect = require('expect.js');

var awsV3Mock = require('aws-sdk-client-mock');
const {
CreateAliasCommand, DeleteAliasCommand, DeleteFunctionCommand, GetAliasCommand, GetFunctionConfigurationCommand, InvokeCommand, LambdaClient, PublishVersionCommand, UpdateFunctionConfigurationCommand, UpdateAliasCommand } = require("@aws-sdk/client-lambda");
CreateAliasCommand, DeleteAliasCommand, DeleteFunctionCommand, GetAliasCommand, GetFunctionConfigurationCommand, InvokeCommand, LambdaClient, PublishVersionCommand, UpdateFunctionConfigurationCommand, UpdateAliasCommand, ResourceNotFoundException } = require("@aws-sdk/client-lambda");
const { GetObjectCommand, S3Client } = require("@aws-sdk/client-s3");

process.env.sfCosts = `{"us-gov-west-1": 0.00003,"eu-north-1": 0.000025,
Expand Down Expand Up @@ -139,8 +139,7 @@ describe('Lambda Utils', () => {
it('should return false if the alias does not exists', async () => {
sandBox.stub(utils, 'getLambdaAlias')
.callsFake(async () => {
const error = new Error('alias is not defined');
error.code = 'ResourceNotFoundException';
const error = new ResourceNotFoundException('alias is not defined');
throw error;
});
const aliasExists = await utils.verifyAliasExistance('arnOK', 'aliasName');
Expand Down

0 comments on commit 2242632

Please sign in to comment.