Skip to content

Commit

Permalink
Fix #1421 Update SlackApiError exception handling for web client (#1423)
Browse files Browse the repository at this point in the history
* Update exception handling for SlackApiError

* Correct lint error for import
  • Loading branch information
vinceta authored Nov 13, 2023
1 parent 2d592cd commit 2afeba6
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ except SlackApiError as e:
assert e.response["ok"] is False
assert e.response["error"] # str like 'invalid_auth', 'channel_not_found'
print(f"Got an error: {e.response['error']}")
# Also receive a corresponding status_code
assert isinstance(e.response.status_code, int)
print(f"Received a response status_code: {e.response.status_code}")
```

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.
Expand Down
4 changes: 2 additions & 2 deletions slack_sdk/web/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from urllib.parse import urlencode
from urllib.request import Request, urlopen, OpenerDirector, ProxyHandler, HTTPSHandler

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

all_params: Dict[str, Any] = copy.copy(body_params) if body_params is not None else {}
if query_params:
Expand Down
2 changes: 1 addition & 1 deletion tests/slack_sdk/web/test_web_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def test_html_response_body_issue_718(self):
self.fail("SlackApiError expected here")
except err.SlackApiError as e:
self.assertTrue(
str(e).startswith("Received a response in a non-JSON format: <!DOCTYPE HTML PUBLIC"),
str(e).startswith("The request to the Slack API failed. (url: http://"),
e,
)

Expand Down
11 changes: 10 additions & 1 deletion tests/slack_sdk/web/test_web_client_http_retry_server_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,16 @@ def test_html_response_body_issue_829(self):
client.users_list(token="xoxb-error_html_response")
self.fail("SlackApiError expected here")
except err.SlackApiError as e:
self.assertTrue(str(e).startswith("Received a response in a non-JSON format: "), e)
self.assertTrue(
str(e).startswith("The request to the Slack API failed. (url: http://"),
e,
)
self.assertIsInstance(e.response.status_code, int)
self.assertFalse(e.response["ok"])
self.assertTrue(
e.response["error"].startswith("Received a response in a non-JSON format: <!DOCTYPE "),
e.response["error"],
)

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

Expand Down
11 changes: 10 additions & 1 deletion tests/slack_sdk/web/test_web_client_issue_829.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,13 @@ def test_html_response_body_issue_829(self):
client.users_list(token="xoxb-error_html_response")
self.fail("SlackApiError expected here")
except err.SlackApiError as e:
self.assertTrue(str(e).startswith("Received a response in a non-JSON format: "), e)
self.assertTrue(
str(e).startswith("The request to the Slack API failed. (url: http://"),
e,
)
self.assertIsInstance(e.response.status_code, int)
self.assertFalse(e.response["ok"])
self.assertTrue(
e.response["error"].startswith("Received a response in a non-JSON format: <!DOCTYPE "),
e.response["error"],
)

0 comments on commit 2afeba6

Please sign in to comment.