Skip to content

fix(auth): improve error reporting when token lookup fails#221

Open
Cris682 wants to merge 2 commits into
cli:trunkfrom
Cris682:fix-112-better-auth-message
Open

fix(auth): improve error reporting when token lookup fails#221
Cris682 wants to merge 2 commits into
cli:trunkfrom
Cris682:fix-112-better-auth-message

Conversation

@Cris682
Copy link
Copy Markdown

@Cris682 Cris682 commented May 8, 2026

Summary

This PR addresses #112 by making token lookup errors more actionable.
When token resolution fails, the final error is no longer only the generic authentication token not found ; it now includes concrete failure reasons when available (for example, gh not found in PATH or gh auth token execution failure).

Changes

  • Added auth.TokenForHostWithError(host) :
    • Keeps the same token lookup order (env/config/gh),
    • Returns a detailed error when the gh fallback fails.
  • Kept auth.TokenForHost(host) backward-compatible:
    • Signature unchanged,
    • Still returns ("", "default") when no token is found (or when lookup fails).
  • Improved tokenFromGh error handling:
    • Switched to CombinedOutput() so stderr/output context is preserved in errors.
  • Updated api.resolveOptions :
    • Uses TokenForHostWithError ,
    • If no token is found, includes the underlying cause when present.

Why

Previously, specific failures (e.g. missing gh binary) were masked by a generic “token not found” message, making debugging and support harder.

Tests

Added targeted tests:

  • pkg/auth/auth_test.go
    • detailed error when gh is not in PATH ,
    • detailed error when GH_PATH points to an invalid executable.
  • pkg/api/client_options_test.go
    • verifies resolveOptions surfaces both the generic message and the concrete cause ( could not find gh executable in PATH ).

Backward Compatibility

  • No API breaking changes:
    • TokenForHost remains unchanged,
    • TokenForHostWithError is additive for callers that need detailed error context.

Related

Cristian Di Matteo and others added 2 commits May 8, 2026 16:37
Return a descriptive error when `gh auth token` command fails or returns
empty output, instead of silently falling back to a generic error. This
helps users understand the root cause of authentication failures.

Update client options resolution to surface the failure reason from
TokenForHost when the source indicates a lookup problem.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TokenForHost "authentication token not found" message could be more informative

1 participant