From fb0fd99a3855667122a11572623cf8d0140f8f1b Mon Sep 17 00:00:00 2001 From: martmull Date: Mon, 29 Jul 2024 18:51:57 +0200 Subject: [PATCH] Fix error handling in serverless service (#6442) - narrowing error handling in aws sdk usage - updating dto to authorize null descriptions --- .../serverless/drivers/lambda.driver.ts | 44 ++++++++++++------- .../dtos/serverless-function.dto.ts | 2 +- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/packages/twenty-server/src/engine/integrations/serverless/drivers/lambda.driver.ts b/packages/twenty-server/src/engine/integrations/serverless/drivers/lambda.driver.ts index 241744b9212c..9820aa8d8b8d 100644 --- a/packages/twenty-server/src/engine/integrations/serverless/drivers/lambda.driver.ts +++ b/packages/twenty-server/src/engine/integrations/serverless/drivers/lambda.driver.ts @@ -8,6 +8,7 @@ import { GetFunctionCommand, UpdateFunctionCodeCommand, DeleteFunctionCommand, + ResourceNotFoundException, } from '@aws-sdk/client-lambda'; import { CreateFunctionCommandInput } from '@aws-sdk/client-lambda/dist-types/commands/CreateFunctionCommand'; import { UpdateFunctionCodeCommandInput } from '@aws-sdk/client-lambda/dist-types/commands/UpdateFunctionCodeCommand'; @@ -46,15 +47,36 @@ export class LambdaDriver this.buildDirectoryManagerService = options.buildDirectoryManagerService; } - async delete(serverlessFunction: ServerlessFunctionEntity) { + private async checkFunctionExists( + serverlessFunctionId: string, + ): Promise { try { + const getFunctionCommand = new GetFunctionCommand({ + FunctionName: serverlessFunctionId, + }); + + await this.lambdaClient.send(getFunctionCommand); + + return true; + } catch (error) { + if (error instanceof ResourceNotFoundException) { + return false; + } + throw error; + } + } + + async delete(serverlessFunction: ServerlessFunctionEntity) { + const functionExists = await this.checkFunctionExists( + serverlessFunction.id, + ); + + if (functionExists) { const deleteFunctionCommand = new DeleteFunctionCommand({ FunctionName: serverlessFunction.id, }); await this.lambdaClient.send(deleteFunctionCommand); - } catch { - return; } } @@ -75,19 +97,11 @@ export class LambdaDriver await createZipFile(sourceTemporaryDir, lambdaZipPath); - let existingFunction = true; - - try { - const getFunctionCommand = new GetFunctionCommand({ - FunctionName: serverlessFunction.id, - }); - - await this.lambdaClient.send(getFunctionCommand); - } catch { - existingFunction = false; - } + const functionExists = await this.checkFunctionExists( + serverlessFunction.id, + ); - if (!existingFunction) { + if (!functionExists) { const params: CreateFunctionCommandInput = { Code: { ZipFile: await fs.promises.readFile(lambdaZipPath), diff --git a/packages/twenty-server/src/engine/metadata-modules/serverless-function/dtos/serverless-function.dto.ts b/packages/twenty-server/src/engine/metadata-modules/serverless-function/dtos/serverless-function.dto.ts index 7a145c631b17..df293846c48b 100644 --- a/packages/twenty-server/src/engine/metadata-modules/serverless-function/dtos/serverless-function.dto.ts +++ b/packages/twenty-server/src/engine/metadata-modules/serverless-function/dtos/serverless-function.dto.ts @@ -48,7 +48,7 @@ export class ServerlessFunctionDto { name: string; @IsString() - @Field() + @Field({ nullable: true }) description: string; @IsString()