From 3736d14c55d596271ad7f077fa17fabebd9681f2 Mon Sep 17 00:00:00 2001 From: Sergey Shelomentsev Date: Fri, 22 Dec 2023 14:22:46 +0200 Subject: [PATCH] feat: improve error handling in mgmt-lambda --- mgmt-lambda/handlers/statusHandler.ts | 24 ++++++++++++------ mgmt-lambda/handlers/updateHandler.ts | 36 +++++++++++++++++++++------ 2 files changed, 45 insertions(+), 15 deletions(-) diff --git a/mgmt-lambda/handlers/statusHandler.ts b/mgmt-lambda/handlers/statusHandler.ts index dee3452c..f91b6f77 100644 --- a/mgmt-lambda/handlers/statusHandler.ts +++ b/mgmt-lambda/handlers/statusHandler.ts @@ -7,13 +7,23 @@ export async function handleStatus( settings: DeploymentSettings, ): Promise { const command = new GetFunctionCommand({ FunctionName: settings.LambdaFunctionName }) - const functionResult = await lambdaClient.send(command) + try { + const functionResult = await lambdaClient.send(command) - return { - statusCode: 200, - body: JSON.stringify(functionResult), - headers: { - 'content-type': 'application/json', - }, + return { + statusCode: 200, + body: JSON.stringify(functionResult), + headers: { + 'content-type': 'application/json', + }, + } + } catch (error) { + return { + statusCode: 500, + body: JSON.stringify(error), + headers: { + 'content-type': 'application/json', + }, + } } } diff --git a/mgmt-lambda/handlers/updateHandler.ts b/mgmt-lambda/handlers/updateHandler.ts index b40e6495..acb08f97 100644 --- a/mgmt-lambda/handlers/updateHandler.ts +++ b/mgmt-lambda/handlers/updateHandler.ts @@ -26,12 +26,12 @@ export async function handleUpdate( console.info(`Going to upgrade Fingerprint Pro function association at CloudFront distbution.`) console.info(`Settings: ${settings}`) - const isLambdaFunctionExist = await checkLambdaFunctionExistance(lambdaClient, settings.LambdaFunctionName) - if (!isLambdaFunctionExist) { - return handleFailure(`Lambda function with name ${settings.LambdaFunctionName} not found`) - } - try { + const isLambdaFunctionExist = await checkLambdaFunctionExistence(lambdaClient, settings.LambdaFunctionName) + if (!isLambdaFunctionExist) { + return handleFailure(`Lambda function with name ${settings.LambdaFunctionName} not found`) + } + const functionVersionArn = await updateLambdaFunctionCode(lambdaClient, settings.LambdaFunctionName) return updateCloudFrontConfig( cloudFrontClient, @@ -39,8 +39,14 @@ export async function handleUpdate( settings.LambdaFunctionName, functionVersionArn, ) - } catch (error) { - return handleFailure(error) + } catch (error: any) { + if (error.name === 'ResourceNotFoundException') { + return handleException('Resource not found', error.message) + } else if (error.name === 'ResourceNotFoundException') { + return handleException('No permission', error.message) + } else { + return handleFailure(error) + } } } @@ -130,7 +136,7 @@ async function updateLambdaFunctionCode(lambdaClient: LambdaClient, functionName return result.FunctionArn } -async function checkLambdaFunctionExistance(client: LambdaClient, functionName: string): Promise { +async function checkLambdaFunctionExistence(client: LambdaClient, functionName: string): Promise { const params: GetFunctionCommandInput = { FunctionName: functionName, } @@ -142,6 +148,20 @@ async function checkLambdaFunctionExistance(client: LambdaClient, functionName: return true } +async function handleException(status: string, message: string): Promise { + const body = { + status: status, + error: message, + } + return { + statusCode: 500, + body: JSON.stringify(body), + headers: { + 'content-type': 'application/json', + }, + } +} + async function handleFailure(message?: any): Promise { const body = { status: 'Update failed',