Skip to content

Commit

Permalink
Fix error handling in serverless service (twentyhq#6442)
Browse files Browse the repository at this point in the history
- narrowing error handling in aws sdk usage
- updating dto to authorize null descriptions
  • Loading branch information
martmull authored Jul 29, 2024
1 parent 0a21a16 commit fb0fd99
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -46,15 +47,36 @@ export class LambdaDriver
this.buildDirectoryManagerService = options.buildDirectoryManagerService;
}

async delete(serverlessFunction: ServerlessFunctionEntity) {
private async checkFunctionExists(
serverlessFunctionId: string,
): Promise<boolean> {
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;
}
}

Expand All @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class ServerlessFunctionDto {
name: string;

@IsString()
@Field()
@Field({ nullable: true })
description: string;

@IsString()
Expand Down

0 comments on commit fb0fd99

Please sign in to comment.