{"record":{"id":"9cd36316c491254b","repo":"astral-sh/uv","slug":"failed-to-login-with-code-status","errorCode":null,"errorMessage":"Failed to login with code `{status}`","messagePattern":"Failed to login with code `(.+?)`","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/uv/src/commands/auth/login.rs","lineNumber":233,"sourceCode":"        let response = client\n            .for_host(store.api())\n            .get(Url::from(url.clone()))\n            .send()\n            .await?;\n        match response.status() {\n            // Retry on 404.\n            reqwest::StatusCode::NOT_FOUND => {\n                tokio::time::sleep(std::time::Duration::from_secs(1)).await;\n                retry += 1;\n            }\n            // Parse the credentials on success.\n            _ if response.status().is_success() => {\n                let credentials = response.json::<PyxOAuthTokens>().await?;\n                break Ok::<PyxTokens, anyhow::Error>(PyxTokens::OAuth(credentials));\n            }\n            // Fail on any other status code (like a 500).\n            status => {\n                break Err(anyhow::anyhow!(\"Failed to login with code `{status}`\"));\n            }\n        }\n\n        if retry >= STATUS_RETRY_LIMIT {\n            break Err(anyhow::anyhow!(\n                \"Login session timed out after {STATUS_RETRY_LIMIT} seconds\"\n            ));\n        }\n    }?;\n\n    store.write(&credentials).await?;\n\n    Ok(AccessToken::from(credentials))\n}\n","sourceCodeStart":215,"sourceCodeEnd":248,"githubUrl":"https://github.com/astral-sh/uv/blob/f1a42680ff5272232d65748acf338b19778dde24/crates/uv/src/commands/auth/login.rs#L215-L248","documentation":"Raised by the browser-based pyx login flow (`uv login` against a pyx-hosted index). After opening the auth/cli/login URL, uv polls auth/cli/status every second; a 404 means 'not yet completed' and retries, a 2xx returns tokens, but any other HTTP status (e.g., 500, 401, 410) breaks the loop with this error, embedding the raw status code. It signals a server-side or session problem, not a client timeout.","triggerScenarios":"The pyx auth backend returns 500/502/503 while handling the status request (outage, bad gateway behind a proxy); the login session was invalidated server-side (expired or already consumed, returning 4xx); an intermediary (corporate proxy, captive portal) returns a non-404 error page status.","commonSituations":"Logging in from behind a corporate proxy that intercepts the API host; pyx service outage during CI authentication; retrying `uv login` with a stale browser tab that already completed the flow.","solutions":["Re-run `uv login` to start a fresh login session (the old cli_token is likely dead) and complete the browser prompt promptly.","Check the pyx service status/health endpoint and retry once it is healthy.","Inspect proxy/VPN interference: bypass the proxy for the API host or fix TLS interception, since intermediary error statuses surface here.","If it persists, capture `uv login` with `-v` and the failing status code and contact the index operator."],"exampleFix":"# before\nuv login  # -> Failed to login with code `500 Internal Server Error`\n# after\nuv -v login https://pypi.???  # fresh session with verbose logging; complete browser flow immediately","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"match pyx_login_with_browser(&store, &client, &printer).await {\n    Ok(token) => { /* proceed */ }\n    Err(err) if err.to_string().contains(\"Failed to login with code\") => {\n        // transient server-side failure: fresh session, bounded retries\n        for attempt in 0..3 {\n            tokio::time::sleep(Duration::from_secs(5)).await;\n            if let Ok(token) = pyx_login_with_browser(&store, &client, &printer).await {\n                return Ok(token);\n            }\n            let _ = attempt;\n        }\n        Err(err)\n    }\n    Err(err) => Err(err),\n}","preventionTips":["Complete the browser approval promptly after `uv login` starts polling.","Ensure direct network access to the pyx API host (bypass intercepting proxies) before logging in.","If login keeps failing with a 5xx, check the index service's status page before retrying."],"tags":["auth","login","network","http-status"],"backgroundTag":null,"analyzedSha":"f1a42680ff5272232d65748acf338b19778dde24","analyzedAt":"2026-08-16T04:51:47.599Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}