JuliusBrussee/caveman · error · TypeError

fetch failed: too many redirects

Error message

fetch failed: too many redirects

What it means

createProxyAwareFetch follows redirects manually (redirect: 'manual') up to MAX_REDIRECTS times, since each hop may need to re-route through the proxy. When a response chain still returns a redirectable status after that budget is exhausted, it throws instead of looping forever.

Solutions

  1. Inspect the redirect chain (curl -IL through the same proxy) and fix the server/proxy misconfiguration causing the loop
  2. Call fetch with { redirect: 'manual' } if you want to handle redirects yourself — the guard returns the redirect response instead of throwing
  3. Verify the proxy isn't stripping/rewriting Location headers so each hop resolves to a new URL rather than cycling
Defensive patterns

Strategy: try-catch

Try / catch

try {
  await proxiedFetch(url);
} catch (e) {
  if (e instanceof TypeError && e.message === "fetch failed: too many redirects") {
    // inspect headers via a manual curl -IL or fall back to redirect: 'manual'
  } else throw e;
}

Prevention

When it happens

Trigger: A proxied HTTP(S) request hits a server (or misconfigured proxy/gateway) that keeps responding 301/302/307/308, producing a redirect loop longer than MAX_REDIRECTS — e.g. http->https->http cycles or a Location header pointing at itself.

Common situations: Corporate proxy rewriting URLs into a redirect loop; server misconfiguration redirecting https back to http; auth redirect that never settles because cookies aren't forwarded.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/f7e11176f50ebf7f. Report an issue: GitHub.

Appendix: source

Thrown at packages/cli/src/proxy-fetch.ts:352

      headers,
      body: buffered,
      signal: request.signal ?? null,
    };
    let hop = resolveProxyUrl(current.url, env);

    for (let redirects = 0; ; redirects += 1) {
      const response = hop ? await sendThroughProxy(current, hop) : await baseFetch(current.url, {
        method: current.method,
        headers: current.headers as HeadersInit,
        body: asBodyInit(current.body),
        signal: current.signal,
        redirect: "manual",
      });

      const location = response.headers.get("location");
      const redirectable = [301, 302, 303, 307, 308].includes(response.status);
      if (!redirectable || !location || request.redirect === "manual") return response;
      if (redirects >= MAX_REDIRECTS) throw new TypeError("fetch failed: too many redirects");
      if (request.redirect === "error") throw new TypeError("fetch failed: unexpected redirect");

      const next = new URL(location, current.url);
      const method = nextMethod(response.status, current.method);
      const bodyDropped = method !== current.method;
      void response.body?.cancel();
      current = {
        method,
        url: next,
        headers: redirectedHeaders(current.headers, current.url, next, bodyDropped),
        body: bodyDropped ? null : current.body,
        signal: current.signal,
      };
      hop = resolveProxyUrl(next, env);
    }
  } as typeof fetch;
}

View on GitHub (pinned to 3ee70a1026)