Last active
September 1, 2020 17:27
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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