Skip to content

Instantly share code, notes, and snippets.

@aoberoi
Last active September 1, 2020 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aoberoi/c090fbc7ed108fd8560787ba36d350ba to your computer and use it in GitHub Desktop.
Save aoberoi/c090fbc7ed108fd8560787ba36d350ba to your computer and use it in GitHub Desktop.
example of a bolt listener middleware that catches errors in view submission handlers and pushes a view
import { App } from '@slack/bolt';
const app = new App(...);
// a listener middleware.
// could easily be shared. can be refactored into a global middleware too.
function pushViewOnErrors(errorViewTemplate) {
return async function _pushViewOnErrors({ body, client, logger, ack, next }) => {
try {
await next();
} catch (error) {
logger.info('pushViewOnErrors: error caught');
// acknowledge the submission in case the listener hadn't yet done so
await ack();
// push the view
await client.views.push({
trigger_id: body.trigger_id,
view: errorView(error),
});
logger.info('pushViewOnErrors: view pushed');
}
}
}
// a view template, maybe built with slack-block-builder ;)
const myErrorViewTemplate = (error) => {
// ...
};
app.view({ callback_id: 'cid', type: 'view_submission' }, pushViewOnErrors(myErrorViewTemplate), ({ view }) => {
// ...
// At runtime, something bad will happen, and it with throw!
// when that happens, the error view will be pushed on
throw Error('oh no!');
});
(async () => {
await app.start(3000);
console.log('app is running');
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment