I am wondering what the purpose of the assert respose['ok'] statements in the official examples is.
All official examples for using the slack WebClient have an assert statement checking that ok is true (see below for an example). But depending on how the library works it should either not be needed or it's a bad choice.
When the API returns an error the library is always raising a SlackApiError exception and the assert statement is never reached.
Or should the library not be thorough and users still need to check ok in their code than an assert statement is a bad choice, because it will usually be optimized out in production.
So can I rely on exceptions from the library for every API call - then I would not need the assert - or do I need to put a proper check in my code (e..g, if not response[ok]: raise ...) ?
Example from readme.md:
import os
import slack
client = slack.WebClient(token=os.environ['SLACK_API_TOKEN'])
response = client.chat_postMessage(
channel='#random',
text="Hello world!")
assert response["ok"]
x in one of the [ ])x in each of the [ ])Thank you for taking the time to send this feedback. Also, I'm sorry about our late response here.
You're right. When the ok value in an API method response is False, the WebClient always raises an exception named slack.errors.SlackApiError. The assert snippets in README don't make sense. I will fix the snippets in README.
import os
import slack
try:
client = slack.WebClient(token=os.environ['SLACK_API_TOKEN'])
response = client.chat_postMessage(
channel='#random',
text="Hello world!")
assert response["ok"] # never come here if the ok is False
assert response["message"]["text"] == "Hello world!"
except Exception as e:
print(f"Got an exception here: {e}")
raise e
$ python app.py
Got an exception here: The request to the Slack API failed.
The server responded with: {'ok': False, 'error': 'not_in_channel'}
Traceback (most recent call last):
File "app.py", line 17, in <module>
raise e
File "app.py", line 10, in <module>
response = client.chat_postMessage(
File "/path-to-python/site-packages/slack/web/client.py", line 559, in chat_postMessage
return self.api_call("chat.postMessage", json=kwargs)
File "/path-to-python/site-packages/slack/web/base_client.py", line 171, in api_call
return self._event_loop.run_until_complete(future)
File "/path-to-python/asyncio/base_events.py", line 616, in run_until_complete
return future.result()
File "/path-to-python/site-packages/slack/web/base_client.py", line 226, in _send
return SlackResponse(**{**data, **res}).validate()
File "/path-to-python/site-packages/slack/web/slack_response.py", line 176, in validate
raise e.SlackApiError(message=msg, response=self)
slack.errors.SlackApiError: The request to the Slack API failed.
The server responded with: {'ok': False, 'error': 'not_in_channel'}
Fixed by #651
Most helpful comment
Thank you for taking the time to send this feedback. Also, I'm sorry about our late response here.
You're right. When the
okvalue in an API method response isFalse, theWebClientalways raises an exception namedslack.errors.SlackApiError. The assert snippets in README don't make sense. I will fix the snippets in README.