{"record":{"id":"024a32b73b5d9fea","repo":"router-for-me/CLIProxyAPI","slug":"xai-device-token-read-response-w","errorCode":null,"errorMessage":"xai device token: read response: %w","messagePattern":"xai device token: read response: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/auth/xai/xai.go","lineNumber":283,"sourceCode":"\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"xai device token: create request: %w\", err), interval, false\n\t}\n\treq.Header.Set(\"Content-Type\", \"application/x-www-form-urlencoded\")\n\treq.Header.Set(\"Accept\", \"application/json\")\n\n\tresp, err := a.httpClient.Do(req)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"xai device token request failed: %w\", err), interval, false\n\t}\n\tdefer func() {\n\t\tif errClose := resp.Body.Close(); errClose != nil {\n\t\t\tlog.Errorf(\"xai device token: close response body error: %v\", errClose)\n\t\t}\n\t}()\n\n\tbody, err := io.ReadAll(resp.Body)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"xai device token: read response: %w\", err), interval, false\n\t}\n\n\tvar payload struct {\n\t\tError            string `json:\"error\"`\n\t\tErrorDescription string `json:\"error_description\"`\n\t\tAccessToken      string `json:\"access_token\"`\n\t\tRefreshToken     string `json:\"refresh_token\"`\n\t\tIDToken          string `json:\"id_token\"`\n\t\tTokenType        string `json:\"token_type\"`\n\t\tExpiresIn        int    `json:\"expires_in\"`\n\t}\n\tif err = json.Unmarshal(body, &payload); err != nil {\n\t\treturn nil, fmt.Errorf(\"xai device token: parse response: %w\", err), interval, false\n\t}\n\n\tif payload.Error != \"\" {\n\t\tswitch payload.Error {\n\t\tcase \"authorization_pending\":","sourceCodeStart":265,"sourceCodeEnd":301,"githubUrl":"https://github.com/router-for-me/CLIProxyAPI/blob/78f0c4079e3e6273d65d03b5549cffc898703264/internal/auth/xai/xai.go#L265-L301","documentation":"The token endpoint responded and the connection succeeded, but reading the response body with io.ReadAll failed. Usually a broken connection mid-body: the server or an intermediary closed the stream before Content-Length bytes arrived, or the context was cancelled during the read.","triggerScenarios":"Connection reset while streaming the token response body; context cancellation during body read; proxy truncating the response.","commonSituations":"Flaky networks, aggressive proxy timeouts, server closing keep-alive connections, long-running poll loops hitting idle-connection reuse problems.","solutions":["Retry with a fresh device code; transient truncation is the most common cause","Disable or tune keep-alive/idle timeouts on the httpClient if resets recur","Check whether a proxy between the client and xAI truncates responses"],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err != nil && strings.Contains(err.Error(), \"read response\") {\n    log.Warnf(\"truncated token response, retrying with fresh device code: %v\", err)\n    return restartDeviceFlow(ctx)\n}","preventionTips":["Retry on body-read failures; they are almost always transient","Tune httpClient Transport idle-connection settings if resets recur behind proxies"],"tags":["network","xai","auth","io"],"backgroundTag":null,"analyzedSha":"78f0c4079e3e6273d65d03b5549cffc898703264","analyzedAt":"2026-08-15T12:26:37.444Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}