{"record":{"id":"5e1b77a17c6aa5ea","repo":"router-for-me/CLIProxyAPI","slug":"xai-device-token-request-failed-with-status-d-s","errorCode":null,"errorMessage":"xai device token request failed with status %d: %s","messagePattern":"xai device token request failed with status (.+?): (.+?)","errorType":"http","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/auth/xai/xai.go","lineNumber":320,"sourceCode":"\t\t\treturn nil, nil, interval, true\n\t\tcase \"slow_down\":\n\t\t\tnextInterval := interval + defaultPollInterval\n\t\t\treturn nil, nil, nextInterval, true\n\t\tcase \"expired_token\":\n\t\t\treturn nil, fmt.Errorf(\"xai device code expired\"), interval, false\n\t\tcase \"access_denied\":\n\t\t\treturn nil, fmt.Errorf(\"xai device authorization denied\"), interval, false\n\t\tdefault:\n\t\t\tdesc := strings.TrimSpace(payload.ErrorDescription)\n\t\t\tif desc != \"\" {\n\t\t\t\treturn nil, fmt.Errorf(\"xai device token error: %s: %s\", payload.Error, desc), interval, false\n\t\t\t}\n\t\t\treturn nil, fmt.Errorf(\"xai device token error: %s\", payload.Error), interval, false\n\t\t}\n\t}\n\n\tif resp.StatusCode != http.StatusOK {\n\t\treturn nil, fmt.Errorf(\"xai device token request failed with status %d: %s\", resp.StatusCode, strings.TrimSpace(string(body))), interval, false\n\t}\n\tif strings.TrimSpace(payload.AccessToken) == \"\" {\n\t\treturn nil, fmt.Errorf(\"xai device token response missing access_token\"), interval, false\n\t}\n\n\temail, subject := parseJWTIdentity(payload.IDToken)\n\treturn buildTokenData(payload.AccessToken, payload.RefreshToken, payload.IDToken, payload.TokenType, payload.ExpiresIn, email, subject), nil, interval, false\n}\n\n// RefreshTokens refreshes an xAI access token.\nfunc (a *XAIAuth) RefreshTokens(ctx context.Context, refreshToken, tokenEndpoint string) (*TokenData, error) {\n\tif strings.TrimSpace(refreshToken) == \"\" {\n\t\treturn nil, fmt.Errorf(\"xai token refresh: refresh token is required\")\n\t}\n\tif ctx == nil {\n\t\tctx = context.Background()\n\t}\n\trefreshToken = strings.TrimSpace(refreshToken)","sourceCodeStart":302,"sourceCodeEnd":338,"githubUrl":"https://github.com/router-for-me/CLIProxyAPI/blob/78f0c4079e3e6273d65d03b5549cffc898703264/internal/auth/xai/xai.go#L302-L338","documentation":"The token exchange finished without an error payload, but the HTTP status was not 200. Because the error field was empty, this is an unexpected server state — e.g. 5xx with an empty body, or a redirect that landed on a non-JSON page — and the status plus raw body are surfaced.","triggerScenarios":"Token endpoint returns 500/502/503 with an empty or non-standard body after the device code is granted.","commonSituations":"xAI auth service outage or deploy blip; gateway timeout pages; retrying an already-consumed device code in a way that yields a bare non-200.","solutions":["Check the status code in the message: 5xx suggests retrying later, 4xx suggests inspecting the body","Verify xAI status/health pages for auth service incidents","Restart the flow with a fresh device code if the code might have been consumed"],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err != nil && strings.Contains(err.Error(), \"failed with status\") {\n    status := extractStatusCode(err)\n    if status >= 500 {\n        time.Sleep(backoff)\n        return restartDeviceFlow(ctx)\n    }\n    return err // 4xx: inspect the surfaced body\n}","preventionTips":["Retry 5xx with backoff; investigate 4xx via the body included in the message","Check xAI status pages during auth outages before debugging locally"],"tags":["oauth","xai","auth","http-status"],"backgroundTag":null,"analyzedSha":"78f0c4079e3e6273d65d03b5549cffc898703264","analyzedAt":"2026-08-15T12:26:37.444Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}