can1357/oh-my-pi · error · AIError.OAuthError
xAI device-code request failed: ${error instanceof Error ? e
Error message
xAI device-code request failed: ${error instanceof Error ? error.message : String(error)} What it means
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.
Source
Thrown at packages/ai/src/registry/oauth/xai-oauth.ts:381
): Promise<XAIDeviceAuthorization> {
let response: Response;
try {
const timeoutSignal = AbortSignal.timeout(TOKEN_REQUEST_TIMEOUT_MS);
response = await fetchImpl(XAI_OAUTH_DEVICE_CODE_URL, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Accept: "application/json",
},
body: new URLSearchParams({
client_id: XAI_OAUTH_CLIENT_ID,
scope: XAI_OAUTH_SCOPE,
}),
signal: signal ? AbortSignal.any([signal, timeoutSignal]) : timeoutSignal,
});
} catch (error) {
if (signal?.aborted) throw new AIError.LoginCancelledError();
throw new AIError.OAuthError(
`xAI device-code request failed: ${error instanceof Error ? error.message : String(error)}`,
{ kind: "device-auth", provider: "xai", cause: error },
);
}
if (!response.ok) {
let detail = "";
try {
detail = (await response.text()).trim();
} catch {
// Ignore body-read failures; the status code is the diagnostic.
}
throw new AIError.OAuthError(`xAI device-code request failed: ${response.status}${detail ? ` ${detail}` : ""}`, {
kind: "device-auth",
provider: "xai",
status: response.status,
});
}View on GitHub (pinned to 9690622007)
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.
Example fix
// before: ignoring network readiness before login
await xaiProvider.device();
// after: preflight connectivity and handle cancellation distinctly
try {
await xaiProvider.device();
} catch (err) {
if (err instanceof AIError.LoginCancelledError) return;
if (err instanceof AIError.OAuthError && err.kind === "device-auth") {
console.error("Network problem reaching xAI:", err.cause);
}
throw err;
} Defensive patterns
Strategy: try-catch
Validate before calling
// preflight connectivity before starting device login
const probe = await fetch("https://api.x.ai", { method: "HEAD", signal: AbortSignal.timeout(5000) }).catch(() => null);
if (!probe) {
throw new Error("xAI endpoints unreachable — check network/VPN/proxy before login");
} Try / catch
try {
await xaiProvider.device();
} catch (err) {
if (err instanceof AIError.LoginCancelledError) return; // user cancelled — not an error
if (err instanceof AIError.OAuthError && err.kind === "device-auth" && err.cause) {
logger.error("xAI device-code request transport failure", { cause: err.cause });
// inspect cause: DNS failure, TLS error, timeout, ECONNREFUSED
}
throw err;
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- xAI device-code token polling failed: ${error instanceof Err
- AnthropicConnectionTimeoutError
- Failed to initiate device authorization: ${initiateResponse.
- Kimi device flow timed out
- xAI device-code response was not a JSON object.
AI-assisted analysis of can1357/oh-my-pi@9690622007 (2026-08-31).
Data as JSON: /api/errors/f6907858177b1fe9.
Report an issue: GitHub.