redis/redis-py · error · HttpError

{response_body}

Error message

{response_body}

What it means

_json_call (redis/http/http_client.py:347) raises HttpError(resp.status, resp.url, resp.text()) whenever the HTTP response status is outside the 200-399 success range. The exception's message is the raw response body text ({response_body}), and HttpError also exposes .status and .url for programmatic handling. This is the HttpClient's single failure path for non-success HTTP responses used by auth/scenario HTTP flows.

Source

Thrown at redis/http/http_client.py:347

        path: str,
        params: Optional[
            Mapping[str, Union[None, str, int, float, bool, list, tuple]]
        ] = None,
        headers: Optional[Mapping[str, str]] = None,
        timeout: Optional[float] = None,
        body: Optional[Union[bytes, str]] = None,
        expect_json: bool = True,
    ) -> Union[HttpResponse, Any]:
        resp = self.request(
            method=method,
            path=path,
            params=params,
            headers=headers,
            body=body,
            timeout=timeout,
        )
        if not (200 <= resp.status < 400):
            raise HttpError(resp.status, resp.url, resp.text())
        if expect_json:
            return resp.json()
        return resp

    def _prepare_body(
        self, json_body: Optional[Any] = None, data: Optional[Union[bytes, str]] = None
    ) -> Optional[Union[bytes, str]]:
        if json_body is not None and data is not None:
            raise ValueError("Provide either json_body or data, not both.")
        if json_body is not None:
            return json.dumps(json_body, ensure_ascii=False, separators=(",", ":"))
        return data

    def _build_url(
        self,
        path: str,
        params: Optional[
            Mapping[str, Union[None, str, int, float, bool, list, tuple]]

View on GitHub (pinned to 6a6b581b48)

Solutions

  1. Inspect err.status to branch: refresh credentials on 401/403, fix the URL/body on 4xx, retry/backoff on 5xx/429.
  2. Increase the retry count / backoff (Retry with ExponentialWithJitterBackoff) so transient 429/5xx are absorbed before surfacing.
  3. Verify base_url, path, and required headers (Authorization, Content-Type) match the service contract.
  4. Log err.url and err.status (not the full body if it may contain secrets) for diagnosis.

Example fix

// before
resp = http.post('/v1/token', json_body=payload)
// after
from redis.http.http_client import HttpError
try:
    resp = http.post('/v1/token', json_body=payload)
except HttpError as e:
    if e.status in (401, 403):
        refresh_token(); resp = http.post('/v1/token', json_body=payload)
    else:
        raise
Defensive patterns

Strategy: try-catch

Validate before calling

def is_likely_ok(method: str, path: str, body) -> bool:
    # cheap client-side sanity before the call
    return bool(path) and (body is None or isinstance(body, (dict, bytes, str)))

Type guard

from redis.http.http_client import HttpError
def is_http_error(e) -> bool:
    return isinstance(e, HttpError)

Try / catch

from redis.http.http_client import HttpError
try:
    resp = http.post('/v1/token', json_body=payload)
except HttpError as e:
    if e.status in (401, 403):
        refresh_credentials()
        resp = http.post('/v1/token', json_body=payload)
    elif e.status >= 500:
        raise  # or retry with backoff
    else:
        raise

Prevention

When it happens

Trigger: Any HttpClient.get/post/put/patch/delete/request that returns 4xx or 5xx (and is not retried away): 401/403 from an auth endpoint when the token is expired/invalid, 404 from a wrong path, 400 from a malformed JSON body, 5xx from an upstream outage after retries are exhausted.

Common situations: Expired OAuth/EntraID token hitting the token endpoint; wrong base_url/path; sending json_body that fails server validation; the token-issuer or scenario-test service being down; rate limiting (429) after the retry budget is spent.

Related errors


AI-assisted analysis of redis/redis-py@6a6b581b48 (2026-08-10). Data as JSON: /api/errors/529ec8a2fb117683. Report an issue: GitHub.