666ghj/MiroFish · error · FetchError

GitHub API returned invalid response metadata

Error message

GitHub API returned invalid response metadata

What it means

Raised by _read_response in scripts/fetch_star_count.py when the HTTP response's Content-Length header exists but cannot be parsed as a base-10 integer (int() raises TypeError or ValueError). This is a defense-in-depth check on response metadata before any body bytes are trusted; a non-numeric Content-Length means the response is malformed or something is intercepting the connection.

Source

Thrown at scripts/fetch_star_count.py:62

        return FetchError("GitHub API authentication failed")
    if status == 403:
        return FetchError("GitHub API request was denied")
    if status == 404:
        return FetchError("repository metadata was not found")
    if status == 429:
        return FetchError("GitHub API rate limit was exhausted")
    if 500 <= status <= 599:
        return FetchError("GitHub API is unavailable")
    return FetchError("GitHub API request failed")


def _read_response(response: Any) -> bytes:
    raw_length = response.headers.get("Content-Length")
    if raw_length is not None:
        try:
            content_length = int(raw_length, 10)
        except (TypeError, ValueError) as exc:
            raise FetchError("GitHub API returned invalid response metadata") from exc
        if content_length < 0 or content_length > MAX_HTTP_BYTES:
            raise FetchError("GitHub API response exceeded the size limit")

    payload = response.read(MAX_HTTP_BYTES + 1)
    if len(payload) > MAX_HTTP_BYTES:
        raise FetchError("GitHub API response exceeded the size limit")
    return payload


def fetch_star_count(token: str, opener: Any | None = None) -> int:
    if not token or len(token) > 4_096 or "\r" in token or "\n" in token:
        raise FetchError("GITHUB_TOKEN is missing or invalid")

    request = urllib.request.Request(
        API_URL,
        headers={
            "Accept": "application/vnd.github+json",
            "Authorization": f"Bearer {token}",

View on GitHub (pinned to b5b53acc57)

Solutions

  1. Retry the fetch once: transient proxy/edge malformation usually does not repeat.
  2. If behind a corporate proxy or SSL-inspecting appliance, bypass it for api.github.com or export HTTPS_PROXY correctly.
  3. If this reproduces deterministically, capture response.headers (never the token) and check what is actually returning the malformed header — it is almost never api.github.com itself.
  4. In tests, make the fake response's headers return plain digit strings (e.g. {'Content-Length': '123'}).
Defensive patterns

Strategy: try-catch

Try / catch

from scripts.fetch_star_count import FetchError, fetch_star_count

try:
    count = fetch_star_count(token)
except FetchError as exc:
    if exc.args[0] == "GitHub API returned invalid response metadata":
        # header-level malformation: do not retry blindly, inspect network path
        log.warning("malformed Content-Length from GitHub path; proxy suspected")
        raise
    raise

Prevention

When it happens

Trigger: GitHub (or a man-in-the-middle proxy) returns a Content-Length like 'abc', '', '12 34', or a hex value; a corporate proxy rewrites headers inconsistently; a mocked test opener returns a headers mapping whose value is not a clean digit string.

Common situations: Corporate transparent proxies or antivirus SSL inspection mangling headers; misconfigured local mock/test doubles that set Content-Length to arbitrary objects; exotic CDN edge behavior during incidents.

Related errors


AI-assisted analysis of 666ghj/MiroFish@b5b53acc57 (2026-08-14). Data as JSON: /api/errors/7bc74587322a3373. Report an issue: GitHub.