{"record":{"id":"43c322784b3775f2","repo":"JuliusBrussee/caveman","slug":"fetch-failed-unexpected-redirect","errorCode":null,"errorMessage":"fetch failed: unexpected redirect","messagePattern":"fetch failed: unexpected redirect","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/cli/src/proxy-fetch.ts","lineNumber":353,"sourceCode":"      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\n/**","sourceCodeStart":335,"sourceCodeEnd":371,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/cli/src/proxy-fetch.ts#L335-L371","documentation":"When a redirect response (301/302/303/307/308 with a Location header) is received and the caller passed request.redirect === 'error' (matching the WHATWG fetch mode where redirects are a protocol error), the wrapper throws instead of following. This preserves fetch's redirect:'error' semantics in the proxy-aware implementation.","triggerScenarios":"Calling the proxy-aware fetch with { redirect: 'error' } and the server responds with any 301/302/303/307/308 status carrying a Location header.","commonSituations":"Strict API clients that forbid redirects hitting an endpoint that starts redirecting (e.g. after a base-URL change, http->https upgrade, or CDN move); tests asserting no redirects where the server began redirecting.","solutions":["Remove redirect: 'error' (default 'follow') to let the wrapper follow redirects transparently","Use redirect: 'manual' to receive the 3xx response and handle Location yourself","Update the requested URL to the final destination so the server stops redirecting"],"exampleFix":"// before\nfetch(url, { redirect: 'error' })\n// after\nfetch(url) // or { redirect: 'follow' }","handlingStrategy":"validation","validationCode":"// Decide redirect policy up front; only set 'error' if 3xx must fail.\nconst res = await proxiedFetch(url, { redirect: \"manual\" });\nif ([301,302,303,307,308].includes(res.status) && res.headers.get(\"location\")) {\n  // handle or log the redirect instead of throwing\n}","typeGuard":null,"tryCatchPattern":"try {\n  await proxiedFetch(url, { redirect: \"error\" });\n} catch (e) {\n  if (e instanceof TypeError && e.message === \"fetch failed: unexpected redirect\") {\n    // retry with default redirect: 'follow'\n  } else throw e;\n}","preventionTips":["Only pass redirect: 'error' when strict no-redirect semantics are required","Pin API base URLs to their final (post-redirect) destinations","Monitor endpoints for newly introduced 3xx responses"],"tags":["network","http","redirect"],"backgroundTag":"unexpected-redirect","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"}