{"record":{"id":"282b20d49bf027cc","repo":"Kuberwastaken/claurst","slug":"failed-to-exchange-code","errorCode":null,"errorMessage":"Failed to exchange code: {}","messagePattern":"Failed to exchange code: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src-rust/crates/cli/src/codex_oauth_flow.rs","lineNumber":200,"sourceCode":"}\r\n\r\n/// Exchange authorization code for access tokens.\r\nasync fn exchange_code_for_tokens(code: &str, verifier: &str) -> anyhow::Result<CodexTokens> {\r\n    let client = reqwest::Client::new();\r\n    let params = [\r\n        (\"client_id\", CODEX_CLIENT_ID),\r\n        (\"code\", code),\r\n        (\"code_verifier\", verifier),\r\n        (\"grant_type\", \"authorization_code\"),\r\n        (\"redirect_uri\", CODEX_REDIRECT_URI),\r\n    ];\r\n\r\n    let resp = client\r\n        .post(CODEX_TOKEN_URL)\r\n        .form(&params)\r\n        .send()\r\n        .await\r\n        .map_err(|e| anyhow!(\"Failed to exchange code: {}\", e))?;\r\n\r\n    if !resp.status().is_success() {\r\n        let status = resp.status();\r\n        let body = resp.text().await.unwrap_or_default();\r\n        bail!(\"Token exchange failed ({}): {}\", status, body);\r\n    }\r\n\r\n    let body: serde_json::Value = resp\r\n        .json()\r\n        .await\r\n        .map_err(|e| anyhow!(\"Failed to parse token response: {}\", e))?;\r\n\r\n    let access_token = body[\"access_token\"]\r\n        .as_str()\r\n        .unwrap_or(\"\")\r\n        .to_string();\r\n\r\n    if access_token.is_empty() {\r","sourceCodeStart":182,"sourceCodeEnd":218,"githubUrl":"https://github.com/Kuberwastaken/claurst/blob/b0637c97ec34144387cbf2f74f65df6d16a6cef1/src-rust/crates/cli/src/codex_oauth_flow.rs#L182-L218","documentation":"Wraps a reqwest transport failure that occurred while POSTing the OAuth authorization code (plus code_verifier and redirect_uri) to OpenAI's Codex token endpoint. The library uses anyhow to append the underlying reqwest error to the message. It means the HTTP request itself never completed, so no token status code was ever received.","triggerScenarios":"In exchange_code_for_tokens, client.post(CODEX_TOKEN_URL).form(&params).send() returns Err: DNS failure, connection refused/reset, TLS error, or request timeout while contacting the token URL during run_oauth_flow_with_label.","commonSituations":"No network access or offline machine; corporate proxy or firewall blocking the token endpoint; DNS misconfiguration; TLS interception with untrusted CA; transient server outage of the OAuth provider.","solutions":["Check basic network connectivity (curl https://auth.openai.com) and any HTTP(S)_PROXY environment variables.","Retry the OAuth login flow; transient network failures resolve on retry.","If behind a corporate proxy, configure reqwest-compatible proxy env vars or add the corporate CA to the trust store.","If it persists, verify the CODEX_TOKEN_URL host is not blocked/renamed by your DNS provider."],"exampleFix":"// before\nlet resp = client.post(CODEX_TOKEN_URL).form(&params).send().await.map_err(|e| anyhow!(\"Failed to exchange code: {}\", e))?;\n// after\nlet resp = client.post(CODEX_TOKEN_URL)\n    .form(&params)\n    .timeout(Duration::from_secs(30))\n    .send()\n    .await\n    .map_err(|e| anyhow!(\"Failed to exchange code: {}\", e))?;","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"// retry with backoff on transport failure\nfor attempt in 0..3 {\n    match client.post(CODEX_TOKEN_URL).form(&params).send().await {\n        Ok(resp) => break resp,\n        Err(e) if attempt < 2 => tokio::time::sleep(Duration::from_secs(2u64 << attempt)).await,\n        Err(e) => return Err(anyhow!(\"Failed to exchange code: {}\", e)),\n    }\n}","preventionTips":["Verify network/proxy reachability of the token endpoint before starting the OAuth flow","Set a request timeout so failures are fast and diagnosable","Trust the corporate CA in the client's TLS config when behind an intercepting proxy"],"tags":["network","oauth","http-client","token-exchange"],"backgroundTag":"network-request-failed","analyzedSha":"b0637c97ec34144387cbf2f74f65df6d16a6cef1","analyzedAt":"2026-09-10T00:24:58.650Z","contentChangedAt":"2026-09-10T00:24:58.650Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}