JuliusBrussee/caveman · error · TypeError

fetch failed: unexpected redirect

Error message

fetch failed: unexpected redirect

What it means

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.

Solutions

  1. Remove redirect: 'error' (default 'follow') to let the wrapper follow redirects transparently
  2. Use redirect: 'manual' to receive the 3xx response and handle Location yourself
  3. Update the requested URL to the final destination so the server stops redirecting

Example fix

// before
fetch(url, { redirect: 'error' })
// after
fetch(url) // or { redirect: 'follow' }
Defensive patterns

Strategy: validation

Validate before calling

// Decide redirect policy up front; only set 'error' if 3xx must fail.
const res = await proxiedFetch(url, { redirect: "manual" });
if ([301,302,303,307,308].includes(res.status) && res.headers.get("location")) {
  // handle or log the redirect instead of throwing
}

Try / catch

try {
  await proxiedFetch(url, { redirect: "error" });
} catch (e) {
  if (e instanceof TypeError && e.message === "fetch failed: unexpected redirect") {
    // retry with default redirect: 'follow'
  } else throw e;
}

Prevention

When it happens

Trigger: Calling the proxy-aware fetch with { redirect: 'error' } and the server responds with any 301/302/303/307/308 status carrying a Location header.

Common situations: 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.

Related errors


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

Appendix: source

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

      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)