{"record":{"id":"f7e11176f50ebf7f","repo":"JuliusBrussee/caveman","slug":"fetch-failed-too-many-redirects","errorCode":null,"errorMessage":"fetch failed: too many redirects","messagePattern":"fetch failed: too many redirects","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/cli/src/proxy-fetch.ts","lineNumber":352,"sourceCode":"      headers,\n      body: buffered,\n      signal: request.signal ?? null,\n    };\n    let hop = resolveProxyUrl(current.url, env);\n\n    for (let redirects = 0; ; redirects += 1) {\n      const response = hop ? await sendThroughProxy(current, hop) : await baseFetch(current.url, {\n        method: current.method,\n        headers: current.headers as HeadersInit,\n        body: asBodyInit(current.body),\n        signal: current.signal,\n        redirect: \"manual\",\n      });\n\n      const location = response.headers.get(\"location\");\n      const redirectable = [301, 302, 303, 307, 308].includes(response.status);\n      if (!redirectable || !location || request.redirect === \"manual\") return response;\n      if (redirects >= MAX_REDIRECTS) throw new TypeError(\"fetch failed: too many redirects\");\n      if (request.redirect === \"error\") throw new TypeError(\"fetch failed: unexpected redirect\");\n\n      const next = new URL(location, current.url);\n      const method = nextMethod(response.status, current.method);\n      const bodyDropped = method !== current.method;\n      void response.body?.cancel();\n      current = {\n        method,\n        url: next,\n        headers: redirectedHeaders(current.headers, current.url, next, bodyDropped),\n        body: bodyDropped ? null : current.body,\n        signal: current.signal,\n      };\n      hop = resolveProxyUrl(next, env);\n    }\n  } as typeof fetch;\n}\n","sourceCodeStart":334,"sourceCodeEnd":370,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/cli/src/proxy-fetch.ts#L334-L370","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Inspect the redirect chain (curl -IL through the same proxy) and fix the server/proxy misconfiguration causing the loop","Call fetch with { redirect: 'manual' } if you want to handle redirects yourself — the guard returns the redirect response instead of throwing","Verify the proxy isn't stripping/rewriting Location headers so each hop resolves to a new URL rather than cycling"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await proxiedFetch(url);\n} catch (e) {\n  if (e instanceof TypeError && e.message === \"fetch failed: too many redirects\") {\n    // inspect headers via a manual curl -IL or fall back to redirect: 'manual'\n  } else throw e;\n}","preventionTips":["Test target URLs through the same proxy with curl -IL to detect redirect loops","Avoid proxies that rewrite Location headers","Prefer https:// target URLs to dodge http->https redirect cycles"],"tags":["network","http","redirect"],"backgroundTag":"too-many-redirects","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}