Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle exceptions in Slack API #14

Merged
merged 1 commit into from
Dec 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,32 @@ const slack = require('./slackClient');
const dynamo = require('./dynamo');

async function postEphemeral({channel, text, blocks, user}) {
const result = await slack.chat.postEphemeral({channel, text, blocks, user});
if (!result.ok) {
throw new AppError(`Failed to post message: ${result.error}`, 500);
try {
const result = await slack.chat.postEphemeral({channel, text, blocks, user});
if (!result.ok) {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is possible but just in case. The docs suggest yes but in practice we've seen exceptions on failures.

https://api.slack.com/methods/chat.postEphemeral#response

throw new AppError(`Failed to post message: ${result.error}`, 500);
}
} catch (err) {
if (err.data && err.data.error) {
throw new AppError(`Failed to post message: ${err.data.error}`, 500);
} else {
throw new AppError(`Failed to post message: ${err.message}`, 500);
}
}
}

async function openView({trigger_id, view}) {
const result = await slack.views.open({trigger_id, view});
if (!result.ok) {
throw new AppError(`Failed to open view: ${result.error}`, 500);
try {
const result = await slack.views.open({trigger_id, view});
if (!result.ok) {
throw new AppError(`Failed to open view: ${result.error}`, 500);
}
} catch (err) {
if (err.data && err.data.error) {
throw new AppError(`Failed to open view: ${err.data.error}`, 500);
} else {
throw new AppError(`Failed to open view: ${err.message}`, 500);
}
}
}

Expand Down
20 changes: 12 additions & 8 deletions src/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,17 @@ module.exports.handler = async (event) => {
answerText: latestRecord.answerText
});

await postMessage({
date: latestRecord.date,
channel: latestRecord.channel,
blocks,
slackId: latestRecord.slackId
});
try {
await postMessage({
date: latestRecord.date,
channel: latestRecord.channel,
blocks,
slackId: latestRecord.slackId
});
} catch (err) {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will catch both Slack and DynamoDB failures.

logger.error({err}, 'Failed to post message');
// don't fail lambda or we could get stuck processing a bad record forever
}
}
};

Expand All @@ -80,8 +85,7 @@ async function postMessage({date, channel, blocks, slackId}) {
: await slack.chat.postMessage(postMessageParamters);

if (!result.ok) {
logger.error({result}, 'Failed to set message');
return;
throw new Error(`Failed to set message: ${result.error}`);
}

if (!slackId) {
Expand Down