Skip to content

fix(web): raise SlackApiError for a malformed JSON body when retry handlers are set - #2073

Closed
tony1233200 wants to merge 1 commit into
slackapi:mainfrom
tony1233200:fix/2070-malformed-json-retry
Closed

tony1233200 wants to merge 1 commit into
slackapi:mainfrom
tony1233200:fix/2070-malformed-json-retry

Conversation

@tony1233200

Copy link
Copy Markdown

Summary

Fixes #2070.

With retry handlers set (the default), WebClient raises a raw json.decoder.JSONDecodeError instead of a SlackApiError when a 200 response body starts with { but is not valid JSON (for example a truncated body).

Root cause: the retry loop in WebClient._perform_urllib_http_request calls json.loads(body_string) on any body starting with {, with no guard. The decode error escapes through the except Exception branch before the graceful fallback in _urllib_api_call runs. Clients without retry handlers, AsyncWebClient, and bodies not starting with { already get a SlackApiError.

Fix: catch JSONDecodeError in that parse and leave parsed_body unset, so _urllib_api_call takes its existing fallback and raises SlackApiError (Received a response in a non-JSON format: ...). This is the fix proposed in the issue.

Testing

New tests/slack_sdk/web/test_web_client_malformed_json_body.py (patches _perform_urllib_http_request_internal to return {"ok": tru):

  • Before the fix: test_default_retry_handlers and test_custom_retry_handlers fail with JSONDecodeError; the no-retry-handlers case and the well-formed-JSON case pass.
  • After the fix: all 4 pass.

Other checks:

  • ruff format --check slack/ slack_sdk/ tests/ integration_tests/ (ruff 0.17.3 as pinned) and ruff check slack/ slack_sdk/ pass.
  • pytest tests/slack_sdk/web tests/slack_sdk_async/web tests/slack_sdk/http_retry tests/slack_sdk_async/http_retry tests/slack_sdk/webhook: 140 passed, 4 skipped, 51 failed, identical to unmodified main. All failures are in the async tests (aiohttp ServerDisconnectedError against the local mock server) plus test_webhook.py::test_proxy_issue_714, because my sandbox forces an HTTP proxy through environment variables. None involve the sync retry path. All sync tests/slack_sdk/web tests pass.

Category

  • slack_sdk.web.WebClient (sync/async) (Web API client)
  • tests/integration_tests (Automated tests for this library)

Requirements

  • I've read and understood the Contributing Guidelines and have done my best effort to follow them.
  • I've read and agree to the Code of Conduct.
  • I have signed the Salesforce CLA (required before your PR can be reviewed or merged).
  • I've run ./scripts/run_validation.sh (not run in full; I ran the format/lint checks and the relevant test subset listed above instead).

…ndlers are set

The retry loop in WebClient._perform_urllib_http_request parsed any 200
body starting with "{" with json.loads() and no guard, so a truncated
body surfaced as a raw JSONDecodeError. Leave "parsed_body" unset on a
decode error so _urllib_api_call() takes its existing fallback and
raises a SlackApiError, the same as without retry handlers.

Fixes slackapi#2070
@tony1233200
tony1233200 requested a review from a team as a code owner November 19, 2026 23:07
@salesforce-cla

Copy link
Copy Markdown

Thanks for the contribution! Before we can merge this, we need @tony1233200 to sign the Salesforce Inc. Contributor License Agreement.

@mwbrooks mwbrooks left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @tony1233200 for the fix and the thorough tests! #2072 has the same change and was opened a few minutes earlier, so we'll take that one and close this as a duplicate. 🙇

@mwbrooks mwbrooks closed this Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WebClient raises JSONDecodeError instead of SlackApiError on a malformed JSON body when retry handlers are set

2 participants