Browse Source

[slack] Create separate SlackBotException based on PopUpException in botserver views.py (#2121)

- By default, it is 500 status code (Internal server error). 
- This does not acknowledge the Slack event request, thereby Slack sends request again and again.
- With status 200, Slack event request will be acknowledged and also it will not clutter logs with multiple tries now
Harsh Gupta 4 years ago
parent
commit
0a7d757104

+ 11 - 7
desktop/core/src/desktop/lib/botserver/views.py

@@ -44,6 +44,10 @@ else:
 
 
 LOG = logging.getLogger(__name__)
 LOG = logging.getLogger(__name__)
 
 
+class SlackBotException(PopupException):
+  def __init__(self, msg, detail=None, error_code=200):
+    PopupException.__init__(self, message=msg, detail=detail, error_code=error_code)
+
 
 
 @login_notrequired
 @login_notrequired
 @csrf_exempt
 @csrf_exempt
@@ -111,7 +115,7 @@ def _send_ephemeral_message(channel_id, user_id, raw_sql_message):
   try:
   try:
     slack_client.chat_postEphemeral(channel=channel_id, user=user_id, text=raw_sql_message)
     slack_client.chat_postEphemeral(channel=channel_id, user=user_id, text=raw_sql_message)
   except Exception as e:
   except Exception as e:
-    raise PopupException(_("Error posting ephemeral message"), detail=e)
+    raise SlackBotException(_("Error posting ephemeral message"), detail=e)
 
 
 
 
 def handle_on_link_shared(host_domain, channel_id, message_ts, links, user_id):
 def handle_on_link_shared(host_domain, channel_id, message_ts, links, user_id):
@@ -128,10 +132,10 @@ def handle_on_link_shared(host_domain, channel_id, message_ts, links, user_id):
         doc = _get_gist_document(**query_id)
         doc = _get_gist_document(**query_id)
         doc_type = 'gist'
         doc_type = 'gist'
       else:
       else:
-        raise PopupException(_("Cannot unfurl link"))
+        raise SlackBotException(_("Cannot unfurl link"))
     except Document2.DoesNotExist:
     except Document2.DoesNotExist:
       msg = "Document with {key} does not exist".format(key=query_id)
       msg = "Document with {key} does not exist".format(key=query_id)
-      raise PopupException(_(msg))
+      raise SlackBotException(_(msg))
 
 
     # Permission check for Slack user to be Hue user
     # Permission check for Slack user to be Hue user
     slack_user = check_slack_user_permission(host_domain, user_id)
     slack_user = check_slack_user_permission(host_domain, user_id)
@@ -144,7 +148,7 @@ def handle_on_link_shared(host_domain, channel_id, message_ts, links, user_id):
     try:
     try:
       slack_client.chat_unfurl(channel=channel_id, ts=message_ts, unfurls=payload['payload'])
       slack_client.chat_unfurl(channel=channel_id, ts=message_ts, unfurls=payload['payload'])
     except Exception as e:
     except Exception as e:
-      raise PopupException(_("Cannot unfurl link"), detail=e)
+      raise SlackBotException(_("Cannot unfurl link"), detail=e)
     
     
     # Generate and upload result xlsx file only if result available
     # Generate and upload result xlsx file only if result available
     if payload['file_status']:
     if payload['file_status']:
@@ -157,14 +161,14 @@ def get_user(channel_id, slack_user):
   except User.DoesNotExist:
   except User.DoesNotExist:
     bot_message = 'Corresponding Hue user not found or does not have access to the query'
     bot_message = 'Corresponding Hue user not found or does not have access to the query'
     _send_message(channel_id, bot_message)
     _send_message(channel_id, bot_message)
-    raise PopupException(_("Slack user does not have access to the query"), error_code=200)
+    raise SlackBotException(_("Slack user does not have access to the query"))
 
 
 
 
 def check_slack_user_permission(host_domain, user_id):
 def check_slack_user_permission(host_domain, user_id):
   try:
   try:
     slack_user = slack_client.users_info(user=user_id)
     slack_user = slack_client.users_info(user=user_id)
   except Exception as e:
   except Exception as e:
-    raise PopupException(_("Cannot find query owner in Slack"), detail=e)
+    raise SlackBotException(_("Cannot find query owner in Slack"), detail=e)
 
 
   response = {
   response = {
     'is_bot': slack_user['user']['is_bot'],
     'is_bot': slack_user['user']['is_bot'],
@@ -197,7 +201,7 @@ def send_result_file(request, channel_id, message_ts, doc, file_format):
       initial_comment='Here is your result file!'
       initial_comment='Here is your result file!'
     )
     )
   except Exception as e:
   except Exception as e:
-    raise PopupException(_("Cannot upload result file"), detail=e)
+    raise SlackBotException(_("Cannot upload result file"), detail=e)
 
 
 
 
 def _query_result(request, notebook, max_rows):
 def _query_result(request, notebook, max_rows):

+ 1 - 1
desktop/core/src/desktop/lib/exceptions_renderable.py

@@ -82,4 +82,4 @@ class PopupException(Exception):
     else:
     else:
       response.status_code = self.error_code
       response.status_code = self.error_code
 
 
-    return response
+    return response