Skip to content

Commit 0b56f25

Browse files
committed
added migration guide for V2
1 parent 17d23d1 commit 0b56f25

6 files changed

+129
-3
lines changed

docs/_advanced/error_handling.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ order: 1
66
---
77

88
<div class="section-content">
9-
If an error occurs in a listener, it’s recommended you handle it directly. However, there are cases where errors may be triggered after your listener has already returned (such as in `say()`, `respond()`, or if you don't call `ack()` when it's required). By default, these errors will be logged to the console. To handle them yourself, you can attach a global error handler to your app with the `error(fn)` method.
9+
*Note: Since `Bolt@2.x`, error handling has improved! View the [migration guide for V2](https://slack.dev/bolt/tutorial/migration-v2) to learn more.*
1010

11-
If you want more control over errors, it’s advised to use the [`chat.postMessage`](https://api.slack.com/methods/chat.postMessage) method attached to your app under the `client` key (instead of `say()` or `respond()`). This returns a `Promise` that can be caught to handle the error.
11+
If an error occurs in a listener, it’s recommended you handle it directly with a `try`/`catch`. However, there still may be cases where errors slip through the cracks. By default, these errors will be logged to the console. To handle them yourself, you can attach a global error handler to your app with the `error(fn)` method.
1212
</div>
1313

1414
```javascript

docs/_advanced/middleware_global.md

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Global middleware is run for all incoming events before any listener middleware.
1111
Both global and listener middleware must call `await next()` to pass control of the execution chain to the next middleware, or call `throw` to pass an error back up the previously-executed middleware chain.
1212

1313
As an example, let's say your app should only respond to users identified with a corresponding internal authentication service (an SSO provider or LDAP, for example). You may define a global middleware that looks up a user record in the authentication service and errors if the user is not found.
14+
15+
*Note: Since `Bolt@2.x`, global middleware was updated to support `async` functions! View the [migration guide for V2](https://slack.dev/bolt/tutorial/migration-v2) to learn more.*
1416
</div>
1517

1618
```javascript

docs/_advanced/middleware_listener.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ But of course, you can write your own middleware for more custom functionality.
1414

1515
As an example, let’s say your listener should only deal with messages from humans. You can write a listener middleware that excludes any bot messages.
1616

17+
*Note: Since `Bolt@2.x`, listener middleware was updated to support `async` functions! View the [migration guide for V2](https://slack.dev/bolt/tutorial/migration-v2) to learn more.*
1718
</div>
1819

1920
```javascript

docs/_basic/listening_actions.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Actions can be filtered on an `action_id` of type string or RegExp object. `acti
1212

1313
You’ll notice in all `action()` examples, `ack()` is used. It is required to call the `ack()` function within an action listener to acknowledge that the event was received from Slack. This is discussed in the [acknowledging events section](#acknowledge).
1414

15+
*Note: Since `Bolt@2.x`, message shortcuts (previously message actions) now use the `shortcut()` method instead of the `action()` method. View the [migration guide for V2](https://slack.dev/bolt/tutorial/migration-v2) to learn more.*
1516
</div>
1617

1718
```javascript

docs/_tutorials/hubot_migration.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Migrating Hubot apps
3-
order: 1
3+
order: 2
44
slug: hubot-migration
55
lang: en
66
layout: tutorial

docs/_tutorials/migration_v2.md

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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

Comments
 (0)