JuliusBrussee/caveman · error · Error

device login polling failed

Error message

device login polling failed: ${error instanceof Error ? error.message : String(error)}

What it means

Thrown by runCavemanDeviceFlow when a token-polling request throws (network error, invalid JSON, etc.) and the polling deadline has already passed. The original error's message is wrapped in 'device login polling failed: ...'. If the signal aborted, the signal's reason is rethrown instead; if time remains, the loop retries after a backoff wait.

Solutions

  1. Restart the device flow (call runCavemanDeviceFlow again) to obtain a fresh device code with a new expiry window.
  2. Fix the underlying network issue named in the wrapped message (DNS, TLS, proxy).
  3. Complete approval in the browser before expires_in elapses; the wrapped message shows the last poll error after the deadline.
  4. If timeouts are frequent, check system clock skew — Date.now() vs server time affects the deadline.

Example fix

// before: user approves too late
await runCavemanDeviceFlow({ ..., onCode: c => console.log(c.user_code) }); // expired after 15 min
// after: detect and retry with a fresh code
try { await runCavemanDeviceFlow(opts); } catch (e) {
  if (String(e.message).startsWith('device login polling failed')) await runCavemanDeviceFlow(opts);
}
Defensive patterns

Strategy: retry

Try / catch

try {
  await runCavemanDeviceFlow(opts);
} catch (e) {
  if (e instanceof Error && e.message.startsWith('device login polling failed')) {
    // deadline passed with a transport error: restart the flow with a fresh device code
    await runCavemanDeviceFlow(opts);
  } else throw e;
}

Prevention

When it happens

Trigger: Inside the polling loop's catch block: a fetch or response.json() error occurs AND Date.now() >= deadline (the expires_in window from the device code has elapsed).

Common situations: User takes longer than expires_in to approve, network drops out mid-poll, server returns malformed JSON on the last poll after the window closed, or laptop sleeps past the deadline.

Understand the failure class

Background: Request timed out: what client-side request timeouts mean across libraries (Request timed out, TIMED_OUT, APITimeoutError) — this error's family across 39 libraries.

Related errors


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

Appendix: source

Thrown at packages/device-auth/src/index.ts:134

    let retryAfterMs = 0;
    try {
      const response = await fetcher(`${baseURL}/api/v1/auth/device/token`, {
        method: "POST",
        headers: { "content-type": "application/json", "x-cave-client": options.client },
        body: JSON.stringify({ device_code: code.device_code }),
        signal: requestSignal(options.signal, 5000),
      });
      status = response.status;
      const retryAfter = response.headers.get("retry-after");
      if (retryAfter !== null) {
        const seconds = Number(retryAfter);
        if (Number.isFinite(seconds) && seconds >= 0) retryAfterMs = seconds * 1000;
      }
      payload = await response.json() as Record<string, unknown>;
    } catch (error) {
      if (options.signal?.aborted) throw options.signal.reason;
      if (Date.now() >= deadline) {
        throw new Error(`device login polling failed: ${error instanceof Error ? error.message : String(error)}`);
      }
      await wait(Math.max(intervalMs, retryAfterMs, 200));
      continue;
    }
    if (status === 429) {
      await wait(Math.max(intervalMs, retryAfterMs, 200));
      continue;
    }
    const accessToken = typeof payload.access_token === "string" ? payload.access_token : "";
    if (accessToken !== "") {
      const credentials = { ...payload, access_token: accessToken } as DeviceCredentials;
      let acknowledged = false;
      return {
        code: structuredClone(code),
        credentials: structuredClone(credentials),
        async acknowledge() {
          if (acknowledged) return;
          await acknowledge({

View on GitHub (pinned to 3ee70a1026)