Skip to content

Commit

Permalink
added try/catch wrapper to conversation and group creation (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
OlehDulebaEpam authored Dec 19, 2023
1 parent 912dcc4 commit b839445
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/functions/conversation-read-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
21 changes: 18 additions & 3 deletions src/functions/create-personal-conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = {
Expand All @@ -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,
Expand Down
12 changes: 10 additions & 2 deletions src/functions/groups-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/functions/groups-read-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit b839445

Please sign in to comment.