diff --git a/lambda/cleaner.js b/lambda/cleaner.js index 4c1d3c3..b99ce9b 100644 --- a/lambda/cleaner.js +++ b/lambda/cleaner.js @@ -1,5 +1,6 @@ 'use strict'; +const { ResourceNotFoundException } = require("@aws-sdk/client-lambda"); const utils = require('./utils'); /** @@ -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 { diff --git a/lambda/utils.js b/lambda/utils.js index c708d6a..99046f7 100644 --- a/lambda/utils.js +++ b/lambda/utils.js @@ -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'); @@ -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; diff --git a/test/unit/test-lambda.js b/test/unit/test-lambda.js index 4dcc29a..28a13e3 100644 --- a/test/unit/test-lambda.js +++ b/test/unit/test-lambda.js @@ -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'); @@ -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') @@ -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; } }); @@ -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 }); @@ -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); @@ -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); @@ -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); @@ -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(); diff --git a/test/unit/test-utils.js b/test/unit/test-utils.js index 41204f9..fae37e7 100644 --- a/test/unit/test-utils.js +++ b/test/unit/test-utils.js @@ -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, @@ -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');