JuliusBrussee/caveman · error · Error

device authorization failed: HTTP

Error message

device authorization failed: HTTP ${codeResponse.status}

What it means

Thrown by runCavemanDeviceFlow when the initial POST to /api/v1/auth/device/code returns a non-2xx HTTP status. The library cannot begin the device authorization grant without a device/user code pair from the server. The status code is embedded in the message for diagnosis.

Solutions

  1. Verify options.baseURL points at the correct Caveman server and the endpoint /api/v1/auth/device/code exists (curl it directly).
  2. Check that options.client is a valid registered client value accepted by the server's x-cave-client header.
  3. Inspect the reported HTTP status: 401/403 means credentials/client issue; 404 means wrong URL or path; 429/5xx means retry later.
  4. Confirm network/proxy configuration allows outbound POST requests to the server.

Example fix

// before
await runCavemanDeviceFlow({ baseURL: 'https://wrong-host.example.com', client: 'my-client', ... });
// after
await runCavemanDeviceFlow({ baseURL: 'https://api.caveman.example.com', client: 'my-registered-client', ... });
Defensive patterns

Strategy: try-catch

Validate before calling

const res = await fetch(`${baseURL}/api/v1/auth/device/code`, { method: 'POST', headers: { 'content-type': 'application/json', 'x-cave-client': client } });
if (!res.ok) throw new Error(`device endpoint unavailable: HTTP ${res.status}`);

Try / catch

try {
  await runCavemanDeviceFlow(opts);
} catch (e) {
  if (e instanceof Error && e.message.startsWith('device authorization failed: HTTP')) {
    // inspect status in message; surface actionable message or retry with backoff
  } else throw e;
}

Prevention

When it happens

Trigger: The fetch to `${baseURL}/api/v1/auth/device/code` with header `x-cave-client` resolves with response.ok === false (any non-2xx status such as 401, 403, 404, 429, 5xx).

Common situations: Wrong baseURL (typo or pointing at a non-Caveman server), invalid or unknown client identifier sent in x-cave-client, server outage, proxy/firewall blocking the endpoint, or API version path changed.

Understand the failure class

Background: "API error: {status}" and "HTTP 401/403/404/429/5xx" errors: non-2xx HTTP responses explained — this error's family across 27 libraries.

Related errors


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

Appendix: source

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

export async function runCavemanDeviceFlow(options: {
  baseURL: string;
  client: string;
  fetch?: typeof globalThis.fetch;
  signal?: AbortSignal;
  sleep?: (ms: number) => Promise<void>;
  onCode?: (code: DeviceCode) => void | Promise<void>;
}): Promise<DeviceGrant> {
  const fetcher = options.fetch ?? globalThis.fetch;
  const wait = options.sleep ?? defaultSleep;
  const baseURL = options.baseURL.replace(/\/$/, "");
  const codeResponse = await fetcher(`${baseURL}/api/v1/auth/device/code`, {
    method: "POST",
    headers: { "content-type": "application/json", "x-cave-client": options.client },
    body: "{}",
    signal: requestSignal(options.signal, 5000),
  });
  if (!codeResponse.ok) throw new Error(`device authorization failed: HTTP ${codeResponse.status}`);
  const rawCode = await codeResponse.json().catch(() => null) as Partial<DeviceCode> | null;
  if (rawCode === null || typeof rawCode.device_code !== "string" || rawCode.device_code === "" ||
    typeof rawCode.user_code !== "string" || typeof rawCode.verification_uri !== "string" ||
    typeof rawCode.expires_in !== "number" || !Number.isFinite(rawCode.expires_in) || rawCode.expires_in <= 0) {
    throw new Error(`device authorization failed: ${JSON.stringify(rawCode)}`);
  }
  const code = rawCode as DeviceCode;
  await options.onCode?.(structuredClone(code));
  let intervalMs = Math.max(0, Number(code.interval ?? 5)) * 1000;
  const deadline = Date.now() + code.expires_in * 1000;
  while (Date.now() < deadline) {
    let payload: Record<string, unknown>;
    let status = 0;
    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 },

View on GitHub (pinned to 3ee70a1026)