nikivdev/code · error
gh api user failed
Error message
gh api user failed
What it means
github_login shells out to `gh api user -q .login` to identify the authenticated GitHub user; if the gh command completes but exits non-zero, this bail fires. It means the GitHub CLI is installed but the API call failed (auth, network, or token issues).
Source
Thrown at src/repos.rs:1473
owner: owner.to_string(),
repo: repo.to_string(),
})
}
fn normalize_git_url(url: &str) -> String {
url.trim()
.trim_end_matches('/')
.trim_end_matches(".git")
.to_string()
}
fn github_login() -> Result<String> {
let output = Command::new("gh")
.args(["api", "user", "-q", ".login"])
.output()
.context("failed to run gh api user")?;
if !output.status.success() {
bail!("gh api user failed");
}
let login = String::from_utf8_lossy(&output.stdout).trim().to_string();
if login.is_empty() {
bail!("gh login was empty");
}
Ok(login)
}
fn resolve_remote_default_branch_in(repo_root: &Path, remote: &str) -> Option<String> {
let head_ref = format!("refs/remotes/{remote}/HEAD");
if let Ok(symbolic) = git_capture_in(repo_root, &["symbolic-ref", &head_ref]) {
let prefix = format!("refs/remotes/{remote}/");
if let Some(branch) = symbolic.trim().strip_prefix(&prefix)
&& !branch.is_empty()
{
return Some(branch.to_string());
}
}View on GitHub (pinned to a747e741ae)
Solutions
- Run `gh auth login` to authenticate the GitHub CLI.
- Verify auth with `gh auth status` and refresh an expired token (`gh auth refresh`).
- Check GH_TOKEN/GITHUB_TOKEN env vars are unset or valid.
- Test connectivity: `gh api user` directly to see gh's own error.
Example fix
// before $ f clone private/repo // gh api user -> exit 1 // after $ gh auth login $ gh auth status # verify logged in $ f clone private/repo
Defensive patterns
Strategy: validation
Validate before calling
fn gh_ready() -> bool {
std::process::Command::new("gh")
.args(["auth", "status"])
.output()
.map(|o| o.status.success())
.unwrap_or(false)
}
// only call flows that use github_login when gh_ready() Try / catch
match github_login() {
Err(e) if e.to_string().contains("gh api user failed") => {
// re-auth then retry
run_gh_auth_login()?;
let login = github_login()?;
}
r => r?,
} Prevention
- Run `gh auth login` during machine setup and keep tokens fresh.
- Check `gh auth status` before automations that depend on gh.
- Avoid exporting invalid GH_TOKEN/GITHUB_TOKEN values.
- Monitor gh/CLI upgrades that change auth behavior in CI images.
When it happens
Trigger: `gh api user` returns a non-zero exit status while resolving the login during clone bootstrap / private-mirror flows.
Common situations: `gh` not logged in (`gh auth login` never run); expired or revoked token; GH_TOKEN/GITHUB_TOKEN env var overriding invalid credentials; network/VPN blocking api.github.com.
Related errors
- gh login was empty
- `gh auth token` failed; run `gh auth login`
- claude failed: {}
- `gh auth token` returned empty token
- Could not determine GitHub username
AI-assisted analysis of nikivdev/code@a747e741ae (2026-09-01).
Data as JSON: /api/errors/46bbdf86ef47dad4.
Report an issue: GitHub.