hakumizuki 4 лет назад
Родитель
Сommit
872408625d

+ 17 - 7
src/server/routes/apiv3/slack-integration.js

@@ -9,7 +9,7 @@ const { verifySlackRequest, generateWebClient } = require('@growi/slack');
 const logger = loggerFactory('growi:routes:apiv3:slack-integration');
 const router = express.Router();
 const SlackAppIntegration = mongoose.model('SlackAppIntegration');
-const slackBotResponse = require('../../service/slack-command-handler/slackbot-response');
+const { slackbotResponse } = require('../../service/slack-command-handler/slackbot-response');
 
 module.exports = (crowi) => {
   this.app = crowi.express;
@@ -104,9 +104,13 @@ module.exports = (crowi) => {
     const args = body.text.split(' ');
     const command = args[0];
 
-    await slackBotResponse(client, body, async() => {
+    try {
       await crowi.slackBotService.handleCommandRequest(command, client, body, args);
-    });
+    }
+    catch (err) {
+      await slackbotResponse(client, body, err);
+    }
+
   }
 
   router.post('/commands', addSigningSecretToReq, verifySlackRequest, async(req, res) => {
@@ -149,14 +153,20 @@ module.exports = (crowi) => {
     try {
       switch (type) {
         case 'block_actions':
-          await slackBotResponse(client, req.body, async() => {
+          try {
             await crowi.slackBotService.handleBlockActionsRequest(client, payload);
-          });
+          }
+          catch (err) {
+            await slackbotResponse(client, req.body, err);
+          }
           break;
         case 'view_submission':
-          await slackBotResponse(client, req.body, async() => {
+          try {
             await crowi.slackBotService.handleViewSubmissionRequest(client, payload);
-          });
+          }
+          catch (err) {
+            await slackbotResponse(client, req.body, err);
+          }
           break;
         default:
           break;

+ 53 - 56
src/server/service/slack-command-handler/slackbot-response.js

@@ -2,66 +2,63 @@ const logger = require('@alias/logger')('growi:service:SlackCommandHandler:slack
 const { markdownSectionBlock } = require('@growi/slack');
 const SlackbotError = require('../../models/vo/slackbot-error');
 
-module.exports = async function(client, body, callback) {
+async function slackbotResponse(client, body, err) {
   // check if the request is to /commands OR /interactions
   const isInteraction = !body.channel_id;
 
-  try {
-    await callback();
+  // throw non-SlackbotError
+  if (!SlackbotError.isSlackbotError(err)) {
+    logger.error(`A non-SlackbotError error occured.\n${err.toString()}`);
+    throw err;
   }
-  catch (err) {
-    // throw non-SlackbotError
-    if (!SlackbotError.isSlackbotError(err)) {
-      logger.error(`A non-SlackbotError error occured.\n${err.toString()}`);
-      throw err;
-    }
 
-    // for both postMessage and postEphemeral
-    let toChannel;
-    // for only postEphemeral
-    let toUser;
-    // decide which channel to send to
-    switch (err.to) {
-      case 'dm':
-        toChannel = isInteraction ? JSON.parse(body.payload).user.id : body.user_id;
-        toUser = toChannel;
-        break;
-      case 'channel':
-        toChannel = isInteraction ? JSON.parse(body.payload).channel.id : body.channel_id;
-        toUser = isInteraction ? JSON.parse(body.payload).user.id : body.user_id;
-        break;
-      default:
-        logger.error('The "to" property of SlackbotError must be "dm" or "channel".');
-        break;
-    }
-
-    // argumentObj object to pass to postMessage OR postEphemeral
-    let argumentsObj = {};
-    switch (err.method) {
-      case 'postMessage':
-        argumentsObj = {
-          channel: toChannel,
-          text: err.popupMessage,
-          blocks: [
-            markdownSectionBlock(err.mainMessage),
-          ],
-        };
-        break;
-      case 'postEphemeral':
-        argumentsObj = {
-          channel: toChannel,
-          user: toUser,
-          text: err.popupMessage,
-          blocks: [
-            markdownSectionBlock(err.mainMessage),
-          ],
-        };
-        break;
-      default:
-        logger.error('The "method" property of SlackbotError must be "postMessage" or "postEphemeral".');
-        break;
-    }
+  // for both postMessage and postEphemeral
+  let toChannel;
+  // for only postEphemeral
+  let toUser;
+  // decide which channel to send to
+  switch (err.to) {
+    case 'dm':
+      toChannel = isInteraction ? JSON.parse(body.payload).user.id : body.user_id;
+      toUser = toChannel;
+      break;
+    case 'channel':
+      toChannel = isInteraction ? JSON.parse(body.payload).channel.id : body.channel_id;
+      toUser = isInteraction ? JSON.parse(body.payload).user.id : body.user_id;
+      break;
+    default:
+      logger.error('The "to" property of SlackbotError must be "dm" or "channel".');
+      break;
+  }
 
-    await client.chat[err.method](argumentsObj);
+  // argumentObj object to pass to postMessage OR postEphemeral
+  let argumentsObj = {};
+  switch (err.method) {
+    case 'postMessage':
+      argumentsObj = {
+        channel: toChannel,
+        text: err.popupMessage,
+        blocks: [
+          markdownSectionBlock(err.mainMessage),
+        ],
+      };
+      break;
+    case 'postEphemeral':
+      argumentsObj = {
+        channel: toChannel,
+        user: toUser,
+        text: err.popupMessage,
+        blocks: [
+          markdownSectionBlock(err.mainMessage),
+        ],
+      };
+      break;
+    default:
+      logger.error('The "method" property of SlackbotError must be "postMessage" or "postEphemeral".');
+      break;
   }
-};
+
+  await client.chat[err.method](argumentsObj);
+}
+
+module.exports = { slackbotResponse };