nikivdev/code · error
gh failed
Error message
gh {} failed: {} What it means
Generic failure wrapper for `gh_capture_in`: when a spawned `gh <args>` command exits non-zero, the tool bails with the command name and gh's trimmed stderr. Because gh prints actionable errors (auth, permissions, rate limits), the appended text is the primary diagnostic.
Solutions
- Read the gh stderr in the message and run `gh auth status`
- Authenticate: `gh auth login` (or fix GH_TOKEN/GITHUB_TOKEN)
- Re-run the exact gh command manually to confirm the cause
- Check network/proxy access to api.github.com
Example fix
// before Error: gh pr view failed: gh: To use GitHub CLI in a GitHub Actions workflow, set the GH_TOKEN environment variable // after $ export GH_TOKEN=$(gh auth token) # or run `gh auth login` $ tool create-review
Defensive patterns
Strategy: try-catch
Validate before calling
let auth = Command::new("gh").args(["auth", "status"]).output()?;
if !auth.status.success() {
return Err(anyhow!("gh not authenticated; run `gh auth login`"));
} Try / catch
let msg = e.to_string();
if msg.contains("gh auth") || msg.contains("authentication") {
eprintln!("run `gh auth login` or set GH_TOKEN");
} else if msg.contains("rate limit") {
eprintln!("wait for rate-limit reset or use a token with higher limits");
}
return Err(e); Prevention
- Keep gh authenticated: check `gh auth status` in setup scripts
- Set GH_TOKEN in CI rather than interactive login
- Verify network/proxy access to api.github.com
- Run the failing gh command manually to read its full stderr
When it happens
Trigger: Any captured gh call (e.g. `gh pr view`, `gh repo view`, `gh api`) that exits non-zero — typically unauthenticated sessions (`gh auth status` failing), missing scopes, nonexistent PR/repo, or network failures.
Common situations: Running `gh auth login` never done or token expired; GH_TOKEN/GITHUB_TOKEN env var pointing at a revoked token; operating in a directory with no git remote so gh can't infer the repo; corporate proxy blocking api.github.com.
Related errors
- failed to create private repo
- failed to create repo via gh (is it…
- Could not determine GitHub username
- Failed to create repository
- failed to determine PR URL after creation (gh output had no…
AI-assisted analysis of nikivdev/code@a747e741ae (2026-09-01).
Data as JSON: /api/errors/cc6ff8a96363fc39.
Report an issue: GitHub.
Appendix: source
Thrown at src/commit.rs:8515
.args(["--version"])
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.context("failed to run `gh` (GitHub CLI)")?;
if !status.success() {
bail!("`gh` is installed but not working");
}
Ok(())
}
fn gh_capture_in(repo_root: &Path, args: &[&str]) -> Result<String> {
let output = Command::new("gh")
.current_dir(repo_root)
.args(args)
.output()
.with_context(|| format!("failed to run gh {}", args.join(" ")))?;
if !output.status.success() {
bail!(
"gh {} failed: {}",
args.join(" "),
String::from_utf8_lossy(&output.stderr).trim()
);
}
Ok(String::from_utf8_lossy(&output.stdout).to_string())
}
fn github_repo_from_remote_url(url: &str) -> Option<String> {
let trimmed = url.trim().trim_end_matches('/');
if trimmed.is_empty() {
return None;
}
// https://github.com/owner/repo(.git)
if let Some(rest) = trimmed.strip_prefix("https://github.com/") {
return Some(rest.trim_end_matches(".git").to_string());
}View on GitHub (pinned to a747e741ae)