SlackApiError: 'dict' object has no attribute 'status_code' #1421
Labels
bug
M-T: A confirmed bug report. Issues are confirmed when the reproduction steps are documented
Version: 3x
web-client
Milestone
slack_sdk.web
reports the'dict' object has no attribute 'status_code'
error when attempting to retrieve the SlackApiError'sstatus_code
.Reproducible in:
Steps to reproduce:
status_code
, similar to the example code of checking ratelimitstatus_code 429
in slack.dev website. The code looks like below:Expected result:
When SlackApiError happens, the code (by calling
slack_sdk.web
) can retrievestatus_code
from SlackApiError's response attribute successfully, w/o throwing out any further error.Actual result:
When a real Slack service outage happened, the following AttributeError
'dict' object has no attribute 'status_code'
was throw out unexpectedly:Probable cause:
When encountering API return's json decoding error (that's the case of 500 error), the response is a dict (returned by _perform_urllib_http_request()) - this implementation in slack_sdk.web.base_client module must have been there for a long time, even before v3.0 :
But SlackApiError's response attribute should be the type of SlackResponse instead of dict that doesn't have
status_code
:Probable solution:
There are probably several solutions. To make the code easy to reference (always provide
status_code
return for whatever SlackApiError) and consistent (use only one type of response for SlackApiError and one entry to SlackApiError), we can re-assemble the response body data by leveraging the message output from _build_unexpected_body_error_message() (that's a str), with one line change only:Then this payload will be sent to SlackResponse's validate(), that will trigger SlackApiError eventually:
Seeking the feedback and suggestions - I can raise a PR if the above proposed fix is acceptable.
Mock test:
Using the above proposed solution, mock test passed like below - no longer trigger another unexpected AttributeError, also provide status_code access and consistent error message:
The text was updated successfully, but these errors were encountered: