Skip to content

Commit

Permalink
feat: improve error handling in mgmt-lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
sshelomentsev committed Feb 26, 2024
1 parent 5db4df9 commit 3736d14
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 15 deletions.
24 changes: 17 additions & 7 deletions mgmt-lambda/handlers/statusHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,23 @@ export async function handleStatus(
settings: DeploymentSettings,
): Promise<APIGatewayProxyResult> {
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',
},
}
}
}
36 changes: 28 additions & 8 deletions mgmt-lambda/handlers/updateHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,27 @@ 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,
settings.CFDistributionId,
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)
}
}
}

Expand Down Expand Up @@ -130,7 +136,7 @@ async function updateLambdaFunctionCode(lambdaClient: LambdaClient, functionName
return result.FunctionArn
}

async function checkLambdaFunctionExistance(client: LambdaClient, functionName: string): Promise<boolean> {
async function checkLambdaFunctionExistence(client: LambdaClient, functionName: string): Promise<boolean> {
const params: GetFunctionCommandInput = {
FunctionName: functionName,
}
Expand All @@ -142,6 +148,20 @@ async function checkLambdaFunctionExistance(client: LambdaClient, functionName:
return true
}

async function handleException(status: string, message: string): Promise<APIGatewayProxyResult> {
const body = {
status: status,
error: message,
}
return {
statusCode: 500,
body: JSON.stringify(body),
headers: {
'content-type': 'application/json',
},
}
}

async function handleFailure(message?: any): Promise<APIGatewayProxyResult> {
const body = {
status: 'Update failed',
Expand Down

0 comments on commit 3736d14

Please sign in to comment.