Browse Source

catch err in posting to slack

sou 7 years ago
parent
commit
e88133928e
2 changed files with 69 additions and 60 deletions
  1. 17 12
      lib/routes/comment.js
  2. 52 48
      lib/util/slack.js

+ 17 - 12
lib/routes/comment.js

@@ -81,21 +81,26 @@ module.exports = function(crowi, app) {
 
     // slack notification
     if (slackNotificationForm.isSlackEnabled) {
-      try {
-        const user = await User.findUserByUsername(req.user.username);
-        const path = page.path;
-        const channels = slackNotificationForm.slackChannels;
+      const user = await User.findUserByUsername(req.user.username);
+      const path = page.path;
+      const channels = slackNotificationForm.slackChannels;
 
-        if (channels) {
-          page.updateSlackChannel(channels).then(function() {}).catch(function() {});
+      if (channels) {
+        try {
+          page.updateSlackChannel(channels).then(function() {});
+        }
+        catch (err) {
+          logger.error('error occured in updating slack channels: ', err);
+        }
+
+        const promises = channels.split(',').map(function(chan) {
+          return crowi.slack.postComment(createdComment, user, chan, path);
+        });
 
-          channels.split(',').map(function(chan) {
-            crowi.slack.postComment(createdComment, user, chan, path);
+        Promise.all(promises)
+          .catch(err => {
+            logger.error('error occured in sending slack notification: ', err);
           });
-        }
-      }
-      catch (err) {
-        logger.error('error occured in sending slack notification: ', err);
       }
     }
   };

+ 52 - 48
lib/util/slack.js

@@ -11,19 +11,37 @@ module.exports = function(crowi) {
     Slack = require('slack-node'),
     slack = {};
 
-  const postWithIwh = function(messageObj, callback) {
-    const client = new Slack();
-    client.setWebhook(config.notification['slack:incomingWebhookUrl']);
-    client.webhook(messageObj, callback);
+  const postWithIwh = function(messageObj) {
+    return new Promise((resolve, reject) => {
+      const client = new Slack();
+      client.setWebhook(config.notification['slack:incomingWebhookUrl']);
+      client.webhook(messageObj, function(err, res) {
+        if (err) {
+          debug('Post error', err, res);
+          debug('Sent data to slack is:', messageObj);
+          return reject(err);
+        }
+        resolve(res);
+      });
+    });
   };
 
-  const postWithWebApi = function(messageObj, callback) {
-    const client = new Slack(config.notification['slack:token']);
-    // stringify attachments
-    if (messageObj.attachments != null) {
-      messageObj.attachments = JSON.stringify(messageObj.attachments);
-    }
-    client.api('chat.postMessage', messageObj, callback);
+  const postWithWebApi = function(messageObj) {
+    return new Promise((resolve, reject) => {
+      const client = new Slack(config.notification['slack:token']);
+      // stringify attachments
+      if (messageObj.attachments != null) {
+        messageObj.attachments = JSON.stringify(messageObj.attachments);
+      }
+      client.api('chat.postMessage', messageObj, function(err, res) {
+        if (err) {
+          debug('Post error', err, res);
+          debug('Sent data to slack is:', messageObj);
+          return reject(err);
+        }
+        resolve(res);
+      });
+    });
   };
 
   const convertMarkdownToMrkdwn = function(body) {
@@ -182,52 +200,38 @@ module.exports = function(crowi) {
   slack.postPage = (page, user, channel, updateType, previousRevision) => {
     const messageObj = prepareSlackMessageForPage(page, user, channel, updateType, previousRevision);
 
-    return promisifiedSlackPost(messageObj);
+    return slackPost(messageObj);
   };
 
   slack.postComment = (comment, user, channel, path) => {
     const messageObj = prepareSlackMessageForComment(comment, user, channel, path);
 
-    return promisifiedSlackPost(messageObj);
+    return slackPost(messageObj);
   };
 
-  const promisifiedSlackPost = (messageObj) => {
-    return new Promise((resolve, reject) => {
-      // define callback function for Promise
-      const callback = function(err, res) {
-        if (err) {
-          debug('Post error', err, res);
-          debug('Sent data to slack is:', messageObj);
-          return reject(err);
-        }
-        resolve(res);
-      };
-
-      // when incoming Webhooks is prioritized
-      if (Config.isIncomingWebhookPrioritized(config)) {
-        if (Config.hasSlackIwhUrl(config)) {
-          debug('posting message with IncomingWebhook');
-          postWithIwh(messageObj, callback);
-        }
-        else if (Config.hasSlackToken(config)) {
-          debug('posting message with Web API');
-          postWithWebApi(messageObj, callback);
-        }
+  const slackPost = (messageObj) => {
+    // when incoming Webhooks is prioritized
+    if (Config.isIncomingWebhookPrioritized(config)) {
+      if (Config.hasSlackIwhUrl(config)) {
+        debug('posting message with IncomingWebhook');
+        return postWithIwh(messageObj);
       }
-      // else
-      else {
-        if (Config.hasSlackToken(config)) {
-          debug('posting message with Web API');
-          postWithWebApi(messageObj, callback);
-        }
-        else if (Config.hasSlackIwhUrl(config)) {
-          debug('posting message with IncomingWebhook');
-          postWithIwh(messageObj, callback);
-        }
+      else if (Config.hasSlackToken(config)) {
+        debug('posting message with Web API');
+        return postWithWebApi(messageObj);
       }
-
-      resolve();
-    });
+    }
+    // else
+    else {
+      if (Config.hasSlackToken(config)) {
+        debug('posting message with Web API');
+        return postWithWebApi(messageObj);
+      }
+      else if (Config.hasSlackIwhUrl(config)) {
+        debug('posting message with IncomingWebhook');
+        return postWithIwh(messageObj);
+      }
+    }
   };
 
   return slack;