Skip to content

status: throws bad error when not logged in #8434

Open

Description

@alexng353

Describe the bug

A clear and concise description of what the bug is. Include version by typing gh --version.

Version: 2.38.0
gh pr status throws a malformed error when the user is not logged in

edit: Even more puzzling, logging in doesn't fix the error because I have a $GITHUB_TOKEN set with incorrect permissions. CLI should detect this and tell the user that the token that they are logged in with does not have the correct permissions to execute the command they are attempting.

Steps to reproduce the behavior

  1. log out
  2. gh pr status
  3. error: non-200 OK status code: 401 Unauthorized body: "{\"message\":\"Bad credentials\",\"documentation_url\":\"https://docs.github.com/graphql\"}"

Expected vs actual behavior

Expected: Print a correctly formatted error with a help message
image

Actual: prints a malformed error
image

Logs

Paste the activity from your command line. Redact if needed.

mono on  alex/migrate-kinde-v2 [$!] via  v18.17.1
❯ gh pr status
non-200 OK status code: 401 Unauthorized body: "{\"message\":\"Bad credentials\",\"documentation_url\":\"https://docs.github.com/graphql\"}"

mono on  alex/migrate-kinde-v2 [$!] via  v18.17.1
❯ gh pr view
HTTP 401: Bad credentials (https://api.github.com/graphql)
Try authenticating with:  gh auth login

Activity

  1. added
    bug
    on Dec 8, 2023
  2. added
    needs-triage
    on Dec 8, 2023
  3. jcasale commented on Dec 9, 2023

    @jcasale

    I am now seeing something similar (exact same error as the gh pr status error above) with 2.40.0.

    If I have GH_TOKEN or GITHUB_TOKEN defined, no command works.

    This was a red herring, my exact problem is now correctly identified in #8441.

  4. samcoe commented on Jan 11, 2024

    @samcoe
    Contributor

    @alexng353 Thanks for writing in. I was able to reproduce this easily by running GH_TOKEN=test gh pr status. I agree we should output a nicely formatted error message as we do in other commands. I am going to open this up for community contributions.

  5. added
    priority-3
    help wanted
    gh-pr
    and removed
    needs-triage
    on Jan 11, 2024
  6. t4kamura commented on Mar 17, 2024

    @t4kamura
    Contributor

    @samcoe
    Hi.
    I have read the code about this and it seems that other commands have the same problem.

    • gh gist list
    • gh release list
      etc.

    api.NewClientFromHTTP(...) .Query(...) can all be reproduced.
    The cause is that go-gh is not correctly handling the shurcooL/graphql errors it uses internally.
    Since this error is not likely to be evaluated with errors.As() or errors.Is(), I think I will have to force it to be handled as follows.

    cli/cmd/gh/main.go

    Lines 158 to 166 in c0658b7

    var httpErr api.HTTPError
    if errors.As(err, &httpErr) && httpErr.StatusCode == 401 {
    fmt.Fprintln(stderr, "Try authenticating with: gh auth login")
    } else if u := factory.SSOURL(); u != "" {
    // handles organization SAML enforcement error
    fmt.Fprintf(stderr, "Authorize in your web browser: %s\n", u)
    } else if msg := httpErr.ScopesSuggestion(); msg != "" {
    fmt.Fprintln(stderr, msg)
    }

    to

    var httpErr api.HTTPError
    if (errors.As(err, &httpErr) && httpErr.StatusCode == 401) || err.Error() == `non-200 OK status code: 401 Unauthorized body: "{\"message\":\"Bad credentials\",\"documentation_url\":\"https://docs.github.com/graphql\"}"` {
    	fmt.Fprintln(stderr, "Try authenticating with:  gh auth login")
    } else if u := factory.SSOURL(); u != "" {
    	// handles organization SAML enforcement error
    	fmt.Fprintf(stderr, "Authorize in your web browser:  %s\n", u)
    } else if msg := httpErr.ScopesSuggestion(); msg != "" {
    	fmt.Fprintln(stderr, msg)
    }

    However, this is not smart, because it will not work if the GitHub API response changes, and it depends on the shurcooL/graphql specification.

    I would like to ask the core team how to solve these problems. If I can handle it, I would like to do it.

    • Requesting without using Query()
    • Modify go-gh to handle Query() errors as well

    What are your thoughts on these and other possible responses?

  7. ale8k commented on Jun 24, 2025

    @ale8k

    Hi @williammartin (wasn't sure who to ping), I was checking this out and on on version gh version 2.74.2 (2025-06-18) this appears to be fixed for all core list commands. I think this can be closed.

  8. williammartin commented on Jun 25, 2025

    @williammartin
    Member
    ➜  cli git:(active-token-user) gh version
    gh version 2.74.2 (2025-06-17)
    https://github.com/cli/cli/releases/tag/v2.74.2
    
    ➜  cli git:(active-token-user) GH_TOKEN=test gh gist list
    non-200 OK status code: 401 Unauthorized body: "{\"message\":\"Bad credentials\",\"documentation_url\":\"https://docs.github.com/graphql\",\"status\":\"401\"}"
    

    Seems to still indicate an issue.

  9. williammartin commented on Jun 25, 2025

    @williammartin
    Member

    @t4kamura sorry no one responded to you, Sam actually left GitHub shortly after his message and no one else was subscribed. I suspect the better option would be to ensure we capture this error in go-gh and wrap correctly so that gh can handle it like the other errors.

  10. ale8k commented on Jun 25, 2025

    @ale8k

    GH_TOKEN=test gh gist list

    Ah, it only appears to be an issue when using GH_TOKEN, sorry, I didn't check with GH_TOKEN set.

    ❯ gh gist list
    To get started with GitHub CLI, please run:  gh auth login
    Alternatively, populate the GH_TOKEN environment variable with a GitHub API authentication token.
    
    ❯ GH_TOKEN=test gh gist list
    non-200 OK status code: 401 Unauthorized body: "{\"message\":\"Bad credentials\",\"documentation_url\":\"https://docs.github.com/graphql\",\"status\":\"401\"}"
    

    I can see in gist list where it directly returns when token is set (pkg/cmd/gist/list/list.go L83). I guess we'd have to check each command individually to see which have malformed go-gh errors?

  11. added
    help wanted candidate
    and removed
    help wanted
    on Jul 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    buggh-prhelp wanted candidatepriority-3

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions