JuliusBrussee/caveman · error

device login failed

Error message

device login failed: ${errorCode}

What it means

During RFC 8628 token polling the server may return an `error` field. `authorization_pending` and `slow_down` are expected and handled; any other error code (e.g. expired_token, access_denied, invalid_grant) is fatal and thrown with the code included. The flow then ends without credentials.

Solutions

  1. Read the error code in the message: expired_token → re-run login and approve faster; access_denied → re-run and approve the request
  2. Re-run `login` to get a fresh device code
  3. If codes expire too quickly, increase the device-code lifetime on the authorization server
  4. Check that the correct user account is used when approving in the browser

Example fix

// before
// user lets the code expire: server returns { "error": "expired_token" }
// after
// re-run promptly
caveman login --instance=https://your-instance  // then approve within the code lifetime
Defensive patterns

Strategy: try-catch

Try / catch

try { await login({ instance }) } catch (e) {
  const m = e.message.match(/device login failed: (\S+)/);
  if (m?.[1] === "expired_token") await promptReLoginQuickly();
  else if (m?.[1] === "access_denied") await promptUserToApprove();
}

Prevention

When it happens

Trigger: The token endpoint returns a payload whose `error` is any value other than "authorization_pending" or "slow_down" — most commonly "expired_token" (user never approved in time), "access_denied" (user rejected the request), or "invalid_grant".

Common situations: User waited too long to approve, so the device code expired; user clicked 'deny' in the browser; someone else consumed the code; server revoked the pending grant.

Related errors


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

Appendix: source

Thrown at packages/cli/src/index.ts:9789

      // effort: login never fails for seats or a down entitlement service.
      await fetchAndStoreWrapEntitlement(baseURL, credentials.access_token);
      if (gateway && wrapMode(gateway) === "managed") {
        console.error(`  ${mark("ok")} wrap now routes through the managed gateway (${gateway}) — governed reporting; verified stays zero without qualifying provider evidence`);
      } else if (gateway) {
        console.error(`  ${mark("ok")} connected; wrap routes through ${gateway}`);
      }
      console.error(SYNC_DISCLOSURE);
      print({ authenticated: true, baseURL, gateway_url: gateway || null, organization_id: organizationId ?? null, token_store: tokenStore });
      // The funnel bridge: pull the spans the local proxy already measured into
      // the dashboard, once, right now (always labeled inferred; best-effort).
      await syncAfterLogin();
      return;
    }
    const errorCode = typeof tok.error === "string" ? tok.error : "";
    if (errorCode === "slow_down") {
      intervalMs = nextDevicePollIntervalMs(intervalMs, errorCode);
    } else if (errorCode && errorCode !== "authorization_pending") {
      throw new Error(`device login failed: ${errorCode}`);
    }
    await sleep(Math.max(intervalMs, 200));
  }
  throw new Error("device login timed out before approval");
}

async function logout() {
	const cfg = await config();
	const externalToken = Boolean(process.env.CAVE_TOKEN);
	if (cfg.token && (!cfg.logoutPendingLocalCleanup || externalToken)) {
	  if (cfg.projectId && cfg.gatewayKeyId) {
	    let response: Response;
	    try {
	      response = await fetch(`${cfg.baseURL}/api/v1/projects/${encodeURIComponent(cfg.projectId)}/keys/${encodeURIComponent(cfg.gatewayKeyId)}/revoke`, {
	        method: "POST",
	        headers: { authorization: `Bearer ${cfg.token}`, "content-type": "application/json", "x-cave-csrf": "cli" },
	        body: "{}",
	        signal: AbortSignal.timeout(5000),

View on GitHub (pinned to 3ee70a1026)