Skip to content

Commit

Permalink
Emit user-catchable exceptions instead of just ValueError where possi…
Browse files Browse the repository at this point in the history
…ble (Closes: #45)
  • Loading branch information
lamby committed May 3, 2016
1 parent 1ecdf27 commit 1af7452
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
45 changes: 45 additions & 0 deletions django_slack/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class SlackException(ValueError):
pass

class ChannelNotFound(SlackException):
pass

class IsArchived(SlackException):
pass

class MsgTooLong(SlackException):
pass

class NoText(SlackException):
pass

class RateLimited(SlackException):
pass

class NotAuthed(SlackException):
pass

class InvalidAuth(SlackException):
pass

class TokenRevoked(SlackException):
pass

class AccountInactive(SlackException):
pass

class UserIsBot(SlackException):
pass

LABEL_TO_EXCEPTION = {
'channel_not_found': ChannelNotFound,
'is_archived': IsArchived,
'msg_too_long': MsgTooLong,
'no_text': NoText,
'rate_limited': RateLimited,
'not_authed': NotAuthed,
'invalid_auth': InvalidAuth,
'token_revoked': TokenRevoked,
'account_inactive': AccountInactive,
'user_is_bot': UserIsBot,
}
7 changes: 5 additions & 2 deletions django_slack/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from django.utils.module_loading import import_string

from .exceptions import LABEL_TO_EXCEPTION, SlackException
from .app_settings import app_settings

class Backend(object):
Expand All @@ -13,10 +14,12 @@ def validate(self, content_type, content):
result = json.loads(content)

if not result['ok']:
raise ValueError(result['error'])
klass = LABEL_TO_EXCEPTION.get(result['error'], SlackException)

raise klass(result['error'])

elif content != 'ok':
raise ValueError(content)
raise SlackException(content)

def get_backend():
"""
Expand Down

0 comments on commit 1af7452

Please sign in to comment.