From 1714dba4cdc7bbaf2194088c80cd072dd4773fbc Mon Sep 17 00:00:00 2001 From: Jan Vorcak Date: Sat, 15 Feb 2020 01:04:54 +0100 Subject: [PATCH] Custom exceptions added --- setup.py | 2 +- slack_app/exceptions.py | 13 +++++++++++++ slack_app/views.py | 19 ++++++++++++++++--- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/setup.py b/setup.py index 2877085..bec1724 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ from setuptools import setup setup( - version="1.0.33", + version="1.0.34", install_requires=["slackclient"] ) diff --git a/slack_app/exceptions.py b/slack_app/exceptions.py index cbea227..c07f19a 100644 --- a/slack_app/exceptions.py +++ b/slack_app/exceptions.py @@ -9,6 +9,19 @@ class SlackAccountNotLinkedException(Exception): """ Thrown if user is not linked with a User model yet. """ + def __init__(self, slack_user_mapping): super().__init__() self.slack_user_mapping = slack_user_mapping + + +class SlackReceiverDoesNotExist(Exception): + pass + + +class SlackInteractivityTypeDoesNotExist(SlackReceiverDoesNotExist): + pass + + +class SlackCommandDoesNotExist(SlackReceiverDoesNotExist): + pass diff --git a/slack_app/views.py b/slack_app/views.py index 14b0baa..71860c7 100644 --- a/slack_app/views.py +++ b/slack_app/views.py @@ -11,7 +11,8 @@ from django.urls import reverse from django.views.decorators.http import require_http_methods -from .exceptions import SlackAppNotInstalledProperlyException, SlackAccountNotLinkedException +from .exceptions import SlackAppNotInstalledProperlyException, SlackAccountNotLinkedException, SlackCommandDoesNotExist, \ + SlackInteractivityTypeDoesNotExist from .models import SlackWorkspace, SlackUserMapping from .decorators import slack_verify_request from .settings import SLACK_LOGIN_OAUTH_REDIRECT_URL, SLACK_INSTALL_OAUTH_REDIRECT_URL @@ -65,7 +66,14 @@ def slack_login_callback(request): @require_http_methods(["POST"]) def slack_interactivity(request): payload = json.loads(request.POST.get('payload')) - fn, required_linked_account = slack_interactivity_callbacks.get(payload.get('type'), None) + payload_type = payload.get("type") + callback = slack_interactivity_callbacks.get(payload_type, None) + + if callback is None: + raise SlackInteractivityTypeDoesNotExist( + f"Interactivity type '{payload_type}' is not linked using @slack_interactivity decorator") + + fn, required_linked_account = callback if fn: if required_linked_account: try: @@ -110,7 +118,12 @@ def get_slack_user_and_workspace(team_id, user_id) -> Tuple[SlackUserMapping, Sl @require_http_methods(["POST"]) def slack_command(request, name: str): payload = request.POST - fn, required_linked_account = slack_commands.get(name, None) + callback = slack_commands.get(name, None) + + if callback is None: + raise SlackCommandDoesNotExist(f"Command '{name}' is not linked using @slack_command decorator") + + fn, required_linked_account = callback if fn: if required_linked_account: try: