Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[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
  • Loading branch information
Harshg999 committed May 17, 2021
1 parent d7877bc commit 0a7d757
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
18 changes: 11 additions & 7 deletions desktop/core/src/desktop/lib/botserver/views.py
Expand Up @@ -44,6 +44,10 @@

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
@csrf_exempt
Expand Down Expand Up @@ -111,7 +115,7 @@ def _send_ephemeral_message(channel_id, user_id, raw_sql_message):
try:
slack_client.chat_postEphemeral(channel=channel_id, user=user_id, text=raw_sql_message)
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):
Expand All @@ -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_type = 'gist'
else:
raise PopupException(_("Cannot unfurl link"))
raise SlackBotException(_("Cannot unfurl link"))
except Document2.DoesNotExist:
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
slack_user = check_slack_user_permission(host_domain, user_id)
Expand All @@ -144,7 +148,7 @@ def handle_on_link_shared(host_domain, channel_id, message_ts, links, user_id):
try:
slack_client.chat_unfurl(channel=channel_id, ts=message_ts, unfurls=payload['payload'])
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
if payload['file_status']:
Expand All @@ -157,14 +161,14 @@ def get_user(channel_id, slack_user):
except User.DoesNotExist:
bot_message = 'Corresponding Hue user not found or does not have access to the query'
_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):
try:
slack_user = slack_client.users_info(user=user_id)
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 = {
'is_bot': slack_user['user']['is_bot'],
Expand Down Expand Up @@ -197,7 +201,7 @@ def send_result_file(request, channel_id, message_ts, doc, file_format):
initial_comment='Here is your result file!'
)
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):
Expand Down
2 changes: 1 addition & 1 deletion desktop/core/src/desktop/lib/exceptions_renderable.py
Expand Up @@ -82,4 +82,4 @@ def response(self, request):
else:
response.status_code = self.error_code

return response
return response

0 comments on commit 0a7d757

Please sign in to comment.