Bound Slack response_url error reads

This commit is contained in:
luyifan 2026-06-30 10:14:43 +08:00 committed by Teknium
parent f36c748c85
commit f8aef2e4e0
2 changed files with 99 additions and 1 deletions

View file

@ -64,6 +64,36 @@ except ImportError: # pragma: no cover - plugin loaded outside package context
logger = logging.getLogger(__name__)
_SLACK_ERROR_BODY_LIMIT_BYTES = 8 * 1024
async def _read_error_text_limited(
response: Any,
*,
limit: int = _SLACK_ERROR_BODY_LIMIT_BYTES,
) -> str:
content = getattr(response, "content", None)
read = getattr(content, "read", None)
if callable(read):
chunks: list[bytes] = []
total = 0
while total <= limit:
size = min(4096, limit + 1 - total)
chunk = await read(size)
if not chunk:
break
data = bytes(chunk)
chunks.append(data)
total += len(data)
if total > limit:
release = getattr(response, "release", None)
if callable(release):
release()
return b"".join(chunks)[:limit].decode("utf-8", errors="replace")
text = await response.text()
return str(text)[:limit]
# ContextVar carrying the user_id of the slash-command invoker.
# Set in _handle_slash_command, read in send() to match the correct
# stashed response_url when multiple users issue commands on the same
@ -1175,7 +1205,7 @@ class SlackAdapter(BasePlatformAdapter):
) as resp:
if resp.status == 200:
return SendResult(success=True, message_id=None)
body = await resp.text()
body = await _read_error_text_limited(resp)
logger.warning(
"[Slack] response_url POST returned %s: %s",
resp.status,