|
| 1 | +--- |
| 2 | +title: Migrating to V2 |
| 3 | +order: 1 |
| 4 | +slug: migration-v2 |
| 5 | +lang: en |
| 6 | +layout: tutorial |
| 7 | +permalink: /tutorial/migration-v2 |
| 8 | +--- |
| 9 | +# Migrating to v2.x |
| 10 | + |
| 11 | +<div class="section-content"> |
| 12 | +This guide will walk you through the process of updating your app from using `bolt@v1.x` to `bolt@2.x`. |
| 13 | +</div> |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +## Upgrading your listeners to `async` |
| 18 | + |
| 19 | +Listeners in your app should updated to `async` functions and `say()`, `respond()`, and `ack()` should be prefaced with `await`. |
| 20 | + |
| 21 | +Before: |
| 22 | + |
| 23 | +```javascript |
| 24 | +app.action('some-action-id', ({action, ack, say}) => { |
| 25 | + ack(); |
| 26 | + say('hello world'); |
| 27 | +}) |
| 28 | +``` |
| 29 | + |
| 30 | +After: |
| 31 | + |
| 32 | +```javascript |
| 33 | +app.action('some-action-id', async ({action, ack, say}) => { |
| 34 | + await ack(); |
| 35 | + await say('hello world'); |
| 36 | +}) |
| 37 | +``` |
| 38 | + |
| 39 | + |
| 40 | +## Error Handling |
| 41 | + |
| 42 | +The recent changes in Bolt for JavaScript V2 have improved our ability to catch errors and filter them to the global error handler. It is still recommended to manage errors in the listeners themselves instead of letting them propagate to the global handler when possible. |
| 43 | + |
| 44 | +### Handling Errors in Listeners with `try`/`catch` |
| 45 | + |
| 46 | +```javascript |
| 47 | +app.action('some-action-id', async ({action, ack, say, logger}) => { |
| 48 | + try { |
| 49 | + await ack(); |
| 50 | + await say('hello world'); |
| 51 | + } catch (error) { |
| 52 | + logger.error(error); |
| 53 | + // handle error |
| 54 | + } |
| 55 | +}) |
| 56 | +``` |
| 57 | + |
| 58 | +### Handling Errors with the Global Error Handler |
| 59 | + |
| 60 | +```javascript |
| 61 | +app.error((error) => { |
| 62 | + // Check the details of the error to handle cases where you should retry sending a message or stop the app |
| 63 | + console.error(error); |
| 64 | +}); |
| 65 | +``` |
| 66 | + |
| 67 | +Other error related changes include: |
| 68 | + |
| 69 | +- When your listener doesn’t call `ack` within the 3 section time limit, we log the failure instead of throwing an error. |
| 70 | +- If you have multiple errors at once when running middleware, Bolt for Javascript will return a wrapper error with a `code` parameter of `slack_bolt_multiple_listener_error` and an `original` parameter that contains an array of all of the errors. |
| 71 | + |
| 72 | + |
| 73 | +## Message Shortcuts |
| 74 | + |
| 75 | +[Message shortcuts](https://api.slack.com/interactivity/shortcuts/using#message_shortcuts) (previously referred to as message actions) now use the `shortcut()` method instead of the `action()` method. |
| 76 | + |
| 77 | +Before: |
| 78 | + |
| 79 | +```javascript |
| 80 | +app.action('message-action-callback', ({action, ack, context}) => { |
| 81 | + ack(); |
| 82 | + // Do stuff |
| 83 | +}) |
| 84 | +``` |
| 85 | + |
| 86 | +After: |
| 87 | + |
| 88 | +```javascript |
| 89 | +app.shortcut('message-action-callback', async ({shortcut, ack, context}) => { |
| 90 | + await ack(); |
| 91 | + // Do stuff |
| 92 | +}) |
| 93 | +``` |
| 94 | + |
| 95 | +## Upgrading Middleware |
| 96 | + |
| 97 | +If you wrote a custom middleware, adjust your function to `async` and update `next()` to `await next()`. If your middleware does some post processing, instead of passing a function to `next()`, you can now run it after `await next()`. |
| 98 | + |
| 99 | +Before: |
| 100 | + |
| 101 | +```javascript |
| 102 | +function noBotMessages({ message, next }) { |
| 103 | + function doAfter() { |
| 104 | + // Post processing goes here |
| 105 | + } |
| 106 | + |
| 107 | +if (!message.subtype || message.subtype !== 'bot_message') { |
| 108 | + next(doAfter); |
| 109 | + } |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +After: |
| 114 | + |
| 115 | +```javascript |
| 116 | +async function noBotMessages({ message, next }) { |
| 117 | + if (!message.subtype || message.subtype !== 'bot_message') { |
| 118 | + await next(); |
| 119 | + // Post processing goes here |
| 120 | + } |
| 121 | +} |
| 122 | +``` |
0 commit comments