Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1421 Update SlackApiError exception handling for web client #1423

Merged
merged 2 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"],
)