{"record":{"id":"06229a6bb58b97df","repo":"JuliusBrussee/caveman","slug":"device-authorization-failed-http-coderesponse-s","errorCode":null,"errorMessage":"device authorization failed: HTTP ${codeResponse.status}","messagePattern":"device authorization failed: HTTP (.+?)","errorType":"http","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/device-auth/src/index.ts","lineNumber":102,"sourceCode":"\nexport async function runCavemanDeviceFlow(options: {\n  baseURL: string;\n  client: string;\n  fetch?: typeof globalThis.fetch;\n  signal?: AbortSignal;\n  sleep?: (ms: number) => Promise<void>;\n  onCode?: (code: DeviceCode) => void | Promise<void>;\n}): Promise<DeviceGrant> {\n  const fetcher = options.fetch ?? globalThis.fetch;\n  const wait = options.sleep ?? defaultSleep;\n  const baseURL = options.baseURL.replace(/\\/$/, \"\");\n  const codeResponse = await fetcher(`${baseURL}/api/v1/auth/device/code`, {\n    method: \"POST\",\n    headers: { \"content-type\": \"application/json\", \"x-cave-client\": options.client },\n    body: \"{}\",\n    signal: requestSignal(options.signal, 5000),\n  });\n  if (!codeResponse.ok) throw new Error(`device authorization failed: HTTP ${codeResponse.status}`);\n  const rawCode = await codeResponse.json().catch(() => null) as Partial<DeviceCode> | null;\n  if (rawCode === null || typeof rawCode.device_code !== \"string\" || rawCode.device_code === \"\" ||\n    typeof rawCode.user_code !== \"string\" || typeof rawCode.verification_uri !== \"string\" ||\n    typeof rawCode.expires_in !== \"number\" || !Number.isFinite(rawCode.expires_in) || rawCode.expires_in <= 0) {\n    throw new Error(`device authorization failed: ${JSON.stringify(rawCode)}`);\n  }\n  const code = rawCode as DeviceCode;\n  await options.onCode?.(structuredClone(code));\n  let intervalMs = Math.max(0, Number(code.interval ?? 5)) * 1000;\n  const deadline = Date.now() + code.expires_in * 1000;\n  while (Date.now() < deadline) {\n    let payload: Record<string, unknown>;\n    let status = 0;\n    let retryAfterMs = 0;\n    try {\n      const response = await fetcher(`${baseURL}/api/v1/auth/device/token`, {\n        method: \"POST\",\n        headers: { \"content-type\": \"application/json\", \"x-cave-client\": options.client },","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/device-auth/src/index.ts#L84-L120","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Verify options.baseURL points at the correct Caveman server and the endpoint /api/v1/auth/device/code exists (curl it directly).","Check that options.client is a valid registered client value accepted by the server's x-cave-client header.","Inspect the reported HTTP status: 401/403 means credentials/client issue; 404 means wrong URL or path; 429/5xx means retry later.","Confirm network/proxy configuration allows outbound POST requests to the server."],"exampleFix":"// before\nawait runCavemanDeviceFlow({ baseURL: 'https://wrong-host.example.com', client: 'my-client', ... });\n// after\nawait runCavemanDeviceFlow({ baseURL: 'https://api.caveman.example.com', client: 'my-registered-client', ... });","handlingStrategy":"try-catch","validationCode":"const res = await fetch(`${baseURL}/api/v1/auth/device/code`, { method: 'POST', headers: { 'content-type': 'application/json', 'x-cave-client': client } });\nif (!res.ok) throw new Error(`device endpoint unavailable: HTTP ${res.status}`);","typeGuard":null,"tryCatchPattern":"try {\n  await runCavemanDeviceFlow(opts);\n} catch (e) {\n  if (e instanceof Error && e.message.startsWith('device authorization failed: HTTP')) {\n    // inspect status in message; surface actionable message or retry with backoff\n  } else throw e;\n}","preventionTips":["Pin and verify baseURL via an env var validated at startup","Register and validate the client id before shipping","Add a startup health check against the auth endpoint","Monitor for 429/5xx and back off before retrying"],"tags":["network","http","device-auth","oauth-device-flow"],"backgroundTag":"http-error-response","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"}