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

Merge pull request #3497 from weseek/fix/prevent-timeout-error-slack-bot

change timing to send response
yusuketk 5 лет назад
Родитель
Сommit
2709f2f9d5
2 измененных файлов с 13 добавлено и 18 удалено
  1. 6 8
      src/server/routes/apiv3/slack-bot.js
  2. 7 10
      src/server/service/bolt.js

+ 6 - 8
src/server/routes/apiv3/slack-bot.js

@@ -2,7 +2,6 @@
 const express = require('express');
 
 const router = express.Router();
-const ErrorV3 = require('../../models/vo/error-apiv3');
 
 module.exports = (crowi) => {
   this.app = crowi.express;
@@ -15,13 +14,12 @@ module.exports = (crowi) => {
       res.send(req.body);
       return;
     }
-    try {
-      const response = await requestHandler(req.body) || null;
-      res.send(response);
-    }
-    catch (err) {
-      return res.apiv3Err(new ErrorV3(`Error:Slack-Bot:${err}`), 500);
-    }
+
+    // Send response immediately to avoid opelation_timeout error
+    // See https://api.slack.com/apis/connections/events-api#the-events-api__responding-to-events
+    res.send();
+
+    await requestHandler(req.body);
   });
 
   return router;

+ 7 - 10
src/server/service/bolt.js

@@ -27,7 +27,7 @@ class BoltReciever {
       body: reqBody,
       ack: (response) => {
         if (ackCalled) {
-          return null;
+          return;
         }
 
         ackCalled = true;
@@ -36,13 +36,7 @@ class BoltReciever {
           const message = response.message || 'Error occurred';
           throw new Error(message);
         }
-        else if (!response) {
-          return null;
-        }
-        else {
-          return response;
-        }
-
+        return;
       },
     };
 
@@ -140,7 +134,7 @@ class BoltService {
         this.generateMarkdownSectionBlock('*No command.*\n Hint\n `/growi [command] [keyword]`'),
       ],
     });
-    throw new Error('/growi command: Invalid first argument');
+    return;
   }
 
   async getSearchResultPaths(command, args) {
@@ -153,7 +147,7 @@ class BoltService {
           this.generateMarkdownSectionBlock('*Input keywords.*\n Hint\n `/growi search [keyword]`'),
         ],
       });
-      throw new Error('/growi command:search: Invalid keyword');
+      return;
     }
 
     // remove leading 'search'.
@@ -200,6 +194,9 @@ class BoltService {
 
   async showEphemeralSearchResults(command, args) {
     const resultPaths = await this.getSearchResultPaths(command, args);
+    if (resultPaths == null) {
+      return;
+    }
     const base = this.crowi.appService.getSiteUrl();
 
     const urls = resultPaths.map((path) => {