Skip to content

Commit 2afeba6

Browse files
authored
Fix #1421 Update SlackApiError exception handling for web client (#1423)
* Update exception handling for SlackApiError * Correct lint error for import
1 parent 2d592cd commit 2afeba6

File tree

5 files changed

+26
-5
lines changed

5 files changed

+26
-5
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ except SlackApiError as e:
107107
assert e.response["ok"] is False
108108
assert e.response["error"] # str like 'invalid_auth', 'channel_not_found'
109109
print(f"Got an error: {e.response['error']}")
110+
# Also receive a corresponding status_code
111+
assert isinstance(e.response.status_code, int)
112+
print(f"Received a response status_code: {e.response.status_code}")
110113
```
111114

112115
Here we also ensure that the response back from Slack is a successful one and that the message is the one we sent by using the `assert` statement.

slack_sdk/web/base_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
from urllib.parse import urlencode
2020
from urllib.request import Request, urlopen, OpenerDirector, ProxyHandler, HTTPSHandler
2121

22-
import slack_sdk.errors as err
2322
from slack_sdk.errors import SlackRequestError
2423
from .deprecation import show_deprecation_warning_if_any
2524
from .internal_utils import (
@@ -299,7 +298,8 @@ def convert_params(values: dict) -> dict:
299298
response_body_data = json.loads(response["body"])
300299
except json.decoder.JSONDecodeError:
301300
message = _build_unexpected_body_error_message(response.get("body", ""))
302-
raise err.SlackApiError(message, response)
301+
self._logger.error(f"Failed to decode Slack API response: {message}")
302+
response_body_data = {"ok": False, "error": message}
303303

304304
all_params: Dict[str, Any] = copy.copy(body_params) if body_params is not None else {}
305305
if query_params:

tests/slack_sdk/web/test_web_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def test_html_response_body_issue_718(self):
150150
self.fail("SlackApiError expected here")
151151
except err.SlackApiError as e:
152152
self.assertTrue(
153-
str(e).startswith("Received a response in a non-JSON format: <!DOCTYPE HTML PUBLIC"),
153+
str(e).startswith("The request to the Slack API failed. (url: http://"),
154154
e,
155155
)
156156

tests/slack_sdk/web/test_web_client_http_retry_server_error.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,16 @@ def test_html_response_body_issue_829(self):
5151
client.users_list(token="xoxb-error_html_response")
5252
self.fail("SlackApiError expected here")
5353
except err.SlackApiError as e:
54-
self.assertTrue(str(e).startswith("Received a response in a non-JSON format: "), e)
54+
self.assertTrue(
55+
str(e).startswith("The request to the Slack API failed. (url: http://"),
56+
e,
57+
)
58+
self.assertIsInstance(e.response.status_code, int)
59+
self.assertFalse(e.response["ok"])
60+
self.assertTrue(
61+
e.response["error"].startswith("Received a response in a non-JSON format: <!DOCTYPE "),
62+
e.response["error"],
63+
)
5564

5665
self.assertEqual(2, retry_handlers[0].call_count)
5766

tests/slack_sdk/web/test_web_client_issue_829.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,13 @@ def test_html_response_body_issue_829(self):
2121
client.users_list(token="xoxb-error_html_response")
2222
self.fail("SlackApiError expected here")
2323
except err.SlackApiError as e:
24-
self.assertTrue(str(e).startswith("Received a response in a non-JSON format: "), e)
24+
self.assertTrue(
25+
str(e).startswith("The request to the Slack API failed. (url: http://"),
26+
e,
27+
)
28+
self.assertIsInstance(e.response.status_code, int)
29+
self.assertFalse(e.response["ok"])
30+
self.assertTrue(
31+
e.response["error"].startswith("Received a response in a non-JSON format: <!DOCTYPE "),
32+
e.response["error"],
33+
)

0 commit comments

Comments
 (0)