{"record":{"id":"f6907858177b1fe9","repo":"can1357/oh-my-pi","slug":"xai-device-code-request-failed-error-instanceof","errorCode":null,"errorMessage":"xAI device-code request failed: ${error instanceof Error ? error.message : String(error)}","messagePattern":"xAI device-code request failed: (.+?)","errorType":"exception","errorClass":"AIError.OAuthError","httpStatus":null,"severity":"error","filePath":"packages/ai/src/registry/oauth/xai-oauth.ts","lineNumber":381,"sourceCode":"): Promise<XAIDeviceAuthorization> {\n\tlet response: Response;\n\ttry {\n\t\tconst timeoutSignal = AbortSignal.timeout(TOKEN_REQUEST_TIMEOUT_MS);\n\t\tresponse = await fetchImpl(XAI_OAUTH_DEVICE_CODE_URL, {\n\t\t\tmethod: \"POST\",\n\t\t\theaders: {\n\t\t\t\t\"Content-Type\": \"application/x-www-form-urlencoded\",\n\t\t\t\tAccept: \"application/json\",\n\t\t\t},\n\t\t\tbody: new URLSearchParams({\n\t\t\t\tclient_id: XAI_OAUTH_CLIENT_ID,\n\t\t\t\tscope: XAI_OAUTH_SCOPE,\n\t\t\t}),\n\t\t\tsignal: signal ? AbortSignal.any([signal, timeoutSignal]) : timeoutSignal,\n\t\t});\n\t} catch (error) {\n\t\tif (signal?.aborted) throw new AIError.LoginCancelledError();\n\t\tthrow new AIError.OAuthError(\n\t\t\t`xAI device-code request failed: ${error instanceof Error ? error.message : String(error)}`,\n\t\t\t{ kind: \"device-auth\", provider: \"xai\", cause: error },\n\t\t);\n\t}\n\n\tif (!response.ok) {\n\t\tlet detail = \"\";\n\t\ttry {\n\t\t\tdetail = (await response.text()).trim();\n\t\t} catch {\n\t\t\t// Ignore body-read failures; the status code is the diagnostic.\n\t\t}\n\t\tthrow new AIError.OAuthError(`xAI device-code request failed: ${response.status}${detail ? ` ${detail}` : \"\"}`, {\n\t\t\tkind: \"device-auth\",\n\t\t\tprovider: \"xai\",\n\t\t\tstatus: response.status,\n\t\t});\n\t}","sourceCodeStart":363,"sourceCodeEnd":399,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/ai/src/registry/oauth/xai-oauth.ts#L363-L399","documentation":"Thrown by requestXAIDeviceAuthorization when the fetch call to the xAI device-code endpoint itself fails — DNS resolution, TCP/TLS errors, connection refused, or the internal request timeout firing (TOKEN_REQUEST_TIMEOUT_MS). The original error is attached as `cause`. If the caller's AbortSignal was aborted, a LoginCancelledError is thrown instead, so this error specifically means the request failed on its own.","triggerScenarios":"device-flow login initiated (via `device` -> requestXAIDeviceAuthorization) while offline, with DNS failure, against a blocked/firewalled network, through a misconfigured HTTPS_PROXY, or when the request exceeds the built-in timeout; fetchImpl rejects for any reason.","commonSituations":"Corporate firewall blocking xAI endpoints; no internet/VPN required; proxy env vars pointing at an unreachable proxy; very slow network tripping the request timeout; IPv6-only environment that cannot reach the host.","solutions":["Read the `cause` chain — the underlying fetch/DNS message says whether it's DNS, TLS, timeout, or refusal.","Verify network connectivity and that xAI OAuth endpoints are reachable (`curl -v` the device-code URL).","Check HTTPS_PROXY/HTTP_PROXY env vars and VPN state; disable intercepting proxies to test.","Retry login — transient outages resolve on a second attempt; if the timeout is too tight for your network, retry when latency is lower.","Confirm no firewall rule blocks *.x.ai / grok.com domains."],"exampleFix":"// before: ignoring network readiness before login\nawait xaiProvider.device();\n// after: preflight connectivity and handle cancellation distinctly\ntry {\n  await xaiProvider.device();\n} catch (err) {\n  if (err instanceof AIError.LoginCancelledError) return;\n  if (err instanceof AIError.OAuthError && err.kind === \"device-auth\") {\n    console.error(\"Network problem reaching xAI:\", err.cause);\n  }\n  throw err;\n}","handlingStrategy":"try-catch","validationCode":"// preflight connectivity before starting device login\nconst probe = await fetch(\"https://api.x.ai\", { method: \"HEAD\", signal: AbortSignal.timeout(5000) }).catch(() => null);\nif (!probe) {\n  throw new Error(\"xAI endpoints unreachable — check network/VPN/proxy before login\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  await xaiProvider.device();\n} catch (err) {\n  if (err instanceof AIError.LoginCancelledError) return; // user cancelled — not an error\n  if (err instanceof AIError.OAuthError && err.kind === \"device-auth\" && err.cause) {\n    logger.error(\"xAI device-code request transport failure\", { cause: err.cause });\n    // inspect cause: DNS failure, TLS error, timeout, ECONNREFUSED\n  }\n  throw err;\n}","preventionTips":["Verify HTTPS_PROXY/HTTP_PROXY/NO_PROXY env vars before running headless logins in CI.","Allow-list xAI/grok.com domains in corporate firewalls.","Run login on a stable connection; the built-in request timeout is tight for high-latency links.","Check `err.cause` first — it holds the underlying fetch error that names the real failure."],"tags":["network","oauth","xai","device-flow","timeout","fetch"],"backgroundTag":"oauth-endpoint-unreachable","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}