From b839445b2bc1020225d1a860764a5fe89bc5a53e Mon Sep 17 00:00:00 2001 From: Oleh <65224395+OlehDulebaEpam@users.noreply.github.com> Date: Tue, 19 Dec 2023 05:25:11 +0200 Subject: [PATCH] added try/catch wrapper to conversation and group creation (#9) --- src/functions/conversation-read-messages.ts | 2 ++ src/functions/create-personal-conversation.ts | 21 ++++++++++++++++--- src/functions/groups-create.ts | 12 +++++++++-- src/functions/groups-read-messages.ts | 2 ++ 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/functions/conversation-read-messages.ts b/src/functions/conversation-read-messages.ts index e11971a..ee5e5e4 100644 --- a/src/functions/conversation-read-messages.ts +++ b/src/functions/conversation-read-messages.ts @@ -114,6 +114,8 @@ export const handler = async (event: APIGatewayProxyEventV2) => { message: `Conversation with id "${conversationID}" does not exist or was deleted before.`, }), }; + } else if ((err as Error).name === 'ValidationException') { + return { statusCode: 400, body: JSON.stringify({ type: 'InvalidFormDataException', message: 'Validation of "conversationID" parameter failed' }) }; } throw err; diff --git a/src/functions/create-personal-conversation.ts b/src/functions/create-personal-conversation.ts index d81f371..f624d37 100644 --- a/src/functions/create-personal-conversation.ts +++ b/src/functions/create-personal-conversation.ts @@ -131,7 +131,14 @@ export const handler = async (event: APIGatewayProxyEventV2) => { }; const queryCommand = new ScanCommand(queryInput); - let result = await client.send(queryCommand); + + let result; + + try { + result = await client.send(queryCommand); + } catch (err) { + return { statusCode: 500, body: JSON.stringify(err), }; + } if (result.Count > 0) { // conversation between people already exists @@ -170,7 +177,11 @@ export const handler = async (event: APIGatewayProxyEventV2) => { }; const newTableCommand = new CreateTableCommand(newTableInput); - result = await client.send(newTableCommand); + try { + result = await client.send(newTableCommand); + } catch (err) { + return { statusCode: 500, body: JSON.stringify(err), }; + } // save conversation id in list const input = { @@ -194,7 +205,11 @@ export const handler = async (event: APIGatewayProxyEventV2) => { const command = new PutItemCommand(input); - result = await client.send(command); + try { + result = await client.send(command); + } catch (err) { + return { statusCode: 500, body: JSON.stringify(err), }; + } return { statusCode: 201, diff --git a/src/functions/groups-create.ts b/src/functions/groups-create.ts index 64bf75e..a0457ef 100644 --- a/src/functions/groups-create.ts +++ b/src/functions/groups-create.ts @@ -135,7 +135,11 @@ export const handler = async (event: APIGatewayProxyEventV2) => { }; const newTableCommand = new CreateTableCommand(newTableInput); - await client.send(newTableCommand); + try { + await client.send(newTableCommand); + } catch (err) { + return { statusCode: 500, body: JSON.stringify(err) }; + } // save conversation id in list const input = { @@ -159,7 +163,11 @@ export const handler = async (event: APIGatewayProxyEventV2) => { const command = new PutItemCommand(input); - await client.send(command); + try { + await client.send(command); + } catch (err) { + return { statusCode: 500, body: JSON.stringify(err), }; + } return { statusCode: 201, diff --git a/src/functions/groups-read-messages.ts b/src/functions/groups-read-messages.ts index e0d3767..34604a1 100644 --- a/src/functions/groups-read-messages.ts +++ b/src/functions/groups-read-messages.ts @@ -114,6 +114,8 @@ export const handler = async (event: APIGatewayProxyEventV2) => { message: `Group with id "${groupID}" does not exist or was deleted before.`, }), }; + } else if ((err as Error).name === 'ValidationException') { + return { statusCode: 400, body: JSON.stringify({ type: 'InvalidFormDataException', message: 'Validation of "conversationID" parameter failed' }) }; } throw err;