Просмотр исходного кода

refactor try-catch for showEphemeralSearchResults

Yuki Takei 4 лет назад
Родитель
Сommit
4b18c1ad0b
1 измененных файлов с 55 добавлено и 36 удалено
  1. 55 36
      src/server/service/slackbot.js

+ 55 - 36
src/server/service/slackbot.js

@@ -164,9 +164,27 @@ class SlackBotService extends S2sMessageHandlable {
   }
   }
 
 
   async showEphemeralSearchResults(client, body, args, offsetNum) {
   async showEphemeralSearchResults(client, body, args, offsetNum) {
+
+    let searchResult;
+    try {
+      searchResult = await this.getSearchResultPaths(client, body, args, offsetNum);
+    }
+    catch (err) {
+      logger.error('Failed to get search results.', err);
+      await client.chat.postEphemeral({
+        channel: body.channel_id,
+        user: body.user_id,
+        text: 'Failed To Search',
+        blocks: [
+          this.generateMarkdownSectionBlock('*Failed to search.*\n Hint\n `/growi search [keyword]`'),
+        ],
+      });
+      throw new Error('/growi command:search: Failed to search');
+    }
+
     const {
     const {
       resultPaths, offset, resultsTotal,
       resultPaths, offset, resultsTotal,
-    } = await this.getSearchResultPaths(client, body, args, offsetNum);
+    } = searchResult;
 
 
     const keywords = this.getKeywords(args);
     const keywords = this.getKeywords(args);
 
 
@@ -201,39 +219,40 @@ class SlackBotService extends S2sMessageHandlable {
 
 
     const keywordsAndDesc = `keyword(s) : "${keywords}" \n ${searchResultsDesc}.`;
     const keywordsAndDesc = `keyword(s) : "${keywords}" \n ${searchResultsDesc}.`;
 
 
-    try {
-      // DEFAULT show "Share" button
-      const actionBlocks = {
-        type: 'actions',
-        elements: [
-          {
-            type: 'button',
-            text: {
-              type: 'plain_text',
-              text: 'Share',
-            },
-            style: 'primary',
-            action_id: 'shareSearchResults',
-            value: JSON.stringify({
-              offset, body, args, pageList: `${keywordsAndDesc} \n\n ${urls.join('\n')}`,
-            }),
+    // DEFAULT show "Share" button
+    const actionBlocks = {
+      type: 'actions',
+      elements: [
+        {
+          type: 'button',
+          text: {
+            type: 'plain_text',
+            text: 'Share',
           },
           },
-        ],
-      };
-      // show "Next" button if next page exists
-      if (resultsTotal > offset + PAGINGLIMIT) {
-        actionBlocks.elements.unshift(
-          {
-            type: 'button',
-            text: {
-              type: 'plain_text',
-              text: 'Next',
-            },
-            action_id: 'showNextResults',
-            value: JSON.stringify({ offset, body, args }),
+          style: 'primary',
+          action_id: 'shareSearchResults',
+          value: JSON.stringify({
+            offset, body, args, pageList: `${keywordsAndDesc} \n\n ${urls.join('\n')}`,
+          }),
+        },
+      ],
+    };
+    // show "Next" button if next page exists
+    if (resultsTotal > offset + PAGINGLIMIT) {
+      actionBlocks.elements.unshift(
+        {
+          type: 'button',
+          text: {
+            type: 'plain_text',
+            text: 'Next',
           },
           },
-        );
-      }
+          action_id: 'showNextResults',
+          value: JSON.stringify({ offset, body, args }),
+        },
+      );
+    }
+
+    try {
       await client.chat.postEphemeral({
       await client.chat.postEphemeral({
         channel: body.channel_id,
         channel: body.channel_id,
         user: body.user_id,
         user: body.user_id,
@@ -247,16 +266,16 @@ class SlackBotService extends S2sMessageHandlable {
       });
       });
     }
     }
     catch (err) {
     catch (err) {
-      logger.error('Failed to get search results.', err);
+      logger.error('Failed to post ephemeral message.', err);
       await client.chat.postEphemeral({
       await client.chat.postEphemeral({
         channel: body.channel_id,
         channel: body.channel_id,
         user: body.user_id,
         user: body.user_id,
-        text: 'Failed To Search',
+        text: 'Failed to post ephemeral message.',
         blocks: [
         blocks: [
-          this.generateMarkdownSectionBlock('*Failed to search.*\n Hint\n `/growi search [keyword]`'),
+          this.generateMarkdownSectionBlock(err.toString()),
         ],
         ],
       });
       });
-      throw new Error('/growi command:search: Failed to search');
+      throw new Error(err);
     }
     }
   }
   }