Skip to content

Commit 5f70ec1

Browse files
authored
Merge pull request #247 from aoberoi/responseparseerror
adds new SlackClientError and ResponseParseError types
2 parents 422e96f + 41a56d9 commit 5f70ec1

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

slackclient/client.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import traceback
66

77
from .server import Server
8+
from .exceptions import ParseResponseError
89

910

1011
class SlackClient(object):
@@ -84,7 +85,11 @@ def api_call(self, method, timeout=None, **kwargs):
8485
8586
See here for more information on responses: https://api.slack.com/web
8687
'''
87-
result = json.loads(self.server.api_call(method, timeout=timeout, **kwargs))
88+
response_body = self.server.api_call(method, timeout=timeout, **kwargs)
89+
try:
90+
result = json.loads(response_body)
91+
except ValueError as json_decode_error:
92+
raise ParseResponseError(response_body, json_decode_error)
8893
if self.server:
8994
if method == 'im.open':
9095
if "ok" in result and result["ok"]:

slackclient/exceptions.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class SlackClientError(Exception):
2+
"""
3+
Base exception for all errors raised by the SlackClient library
4+
"""
5+
def __init__(self, msg=None):
6+
if msg is None:
7+
# default error message
8+
msg = "An error occurred in the SlackClient library"
9+
super(SlackClientError, self).__init__(msg)
10+
11+
12+
class ParseResponseError(SlackClientError, ValueError):
13+
"""
14+
Error raised when responses to Web API methods cannot be parsed as valid JSON
15+
"""
16+
def __init__(self, response_body, original_exception):
17+
super(ParseResponseError, self).__init__(
18+
"Slack API response body could not be parsed: {0}. Original exception: {1}".format(
19+
response_body, original_exception
20+
)
21+
)
22+
self.response_body = response_body
23+
self.original_exception = original_exception

slackclient/server.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from .channel import Channel
44
from .user import User
55
from .util import SearchList, SearchDict
6+
from .exceptions import SlackClientError
67
from ssl import SSLError
78

89
from websocket import create_connection
@@ -256,14 +257,18 @@ def api_call(self, method, timeout=None, **kwargs):
256257
'''
257258
return self.api_requester.do(self.token, method, kwargs, timeout=timeout).text
258259

260+
# TODO: Move the error types defined below into the .exceptions namespace. This would be a semver
261+
# major change because any clients already referencing these types in order to catch them
262+
# specifically would need to deal with the symbol names changing.
259263

260-
class SlackConnectionError(Exception):
264+
265+
class SlackConnectionError(SlackClientError):
261266
def __init__(self, message='', reply=None):
262267
super(SlackConnectionError, self).__init__(message)
263268
self.reply = reply
264269

265270

266-
class SlackLoginError(Exception):
271+
class SlackLoginError(SlackClientError):
267272
def __init__(self, message='', reply=None):
268273
super(SlackLoginError, self).__init__(message)
269274
self.reply = reply

0 commit comments

Comments
 (0)