Repository navigation
Conversation
…ed body
The retry-handler branch parses any 200 response body that starts with
"{" to build its RetryHttpResponse. A truncated or otherwise malformed
body raised JSONDecodeError from json.loads before the caller could run
its non-JSON fallback, so sync WebClient calls with the default retry
handlers surfaced a decode error instead of SlackApiError.
Leave parsed_body unset when the parse fails, which restores the
existing fallback in _urllib_api_call.
mwbrooks
left a comment
There was a problem hiding this comment.
Thanks @agustin18, the fix is exactly what #2070 proposed! 🙇
Changing the raised type from JSONDecodeError to SlackApiError can break callers who catch ValueError today, so as-is this would wait for the 4.x release.
@WilliamBergamin do you have an alternative suggestion on how we could merge this without introducing a breaking change?
| try: | ||
| parsed_body = json.loads(body_string) | ||
| resp["parsed_body"] = parsed_body | ||
| except json.decoder.JSONDecodeError: |
There was a problem hiding this comment.
suggestion (non-blocking): Could we guard the json.loads in _request_for_pagination in this PR too? #2070 has this as a 4.x breaking change, so fixing both sync paths together means callers see the raised type change once instead of across two releases.
| def test_non_json_response_body_starting_with_brace_raises_api_error(self): | ||
| # The retry-handler branch parses any 200 body that starts with "{", so a | ||
| # truncated body must not escape from it as a JSONDecodeError. | ||
| response = {"status": 200, "headers": {}, "body": '{"ok": tru'} |
There was a problem hiding this comment.
praise: Thanks for reusing the exact repro from #2070, so this fails on main for the reason the issue describes.
Summary
Fixes #2070.
A sync
WebClientparses any 200 response body that starts with{inside the retry loop in_perform_urllib_http_request, so the retry handlers receive a parsed body. Thatjson.loadshad no guard: a truncated body raisedJSONDecodeErrorfrom the loop before_urllib_api_callcould run its non-JSON fallback, so callers got a decode error instead ofSlackApiError.The parse now leaves
parsed_bodyunset when the body is malformed. That is the same state a body not starting with{produces, so the existing fallback builds theReceived a response in a non-JSON formaterror._request_for_paginationhas the same unguardedjson.loadson the response body, independent of retry handlers. I left it out of this change; happy to handle it here or in a follow-up.Testing
tests/slack_sdk/web/test_web_client.py:test_non_json_response_body_starting_with_brace_raises_api_error. It fails onmainwithjson.decoder.JSONDecodeError: Expecting value: line 1 column 8 (char 7)and passes with the change.python -m pytest tests/-> 1082 passed, 5 skipped, 45 subtests passed.python scripts/codegen.py --path .produces no changes;base_client.pyis not a generated file../scripts/format.sh,./scripts/lint.shand./scripts/run_mypy.shpass, mypy clean on 107 source files.Category
/docs(Documents)/tutorial(PythOnBoardingBot tutorial)tests/integration_tests(Automated tests for this library)Requirements
python3 -m venv .venv && source .venv/bin/activate && ./scripts/run_validation.shafter making the changes.