Skip to content

"unknown owner type" Error Message on gh project Bad Auth Token #8885

Open

Description

@dsanders11

Describe the bug

When using gh project subcommands, if a bad auth token is used (such as an expired one), the error message is a confusing "uknown error type".

Tested with version 2.46.0

Steps to reproduce the behavior

$ GH_TOKEN=foo gh project unlink 999 --repo cli/cli
unknown owner type

Expected vs actual behavior

Expected a more useful error on bad auth like what you get with gh pr:

$ GH_TOKEN=foo gh pr view 999 --repo cli/cli
HTTP 401: Bad credentials (https://api.github.com/graphql)
Try authenticating with:  gh auth login

Activity

  1. added
    bug
    on Mar 29, 2024
  2. added
    needs-triage
    on Mar 29, 2024
  3. Innu38 commented on Apr 10, 2024

    @Innu38
  4. Innu38 commented on Apr 10, 2024

    @Innu38
  5. harveysanders commented on Apr 11, 2024

    @harveysanders
    Contributor

    Hmm, both the pr and project commands uses a github.com/cli/go-gh/v2 Graphql client to their respective GraphQL requests. The pr command calls (*GraphQLClient).DoWithContext(). The API auth error error is a *ghAPI.HTTPError and is handled here:

    cli/api/client.go

    Lines 166 to 175 in 88a7e52

    var restErr *ghAPI.HTTPError
    if errors.As(err, &restErr) {
    return HTTPError{
    HTTPError: restErr,
    scopesSuggestion: generateScopesSuggestion(restErr.StatusCode,
    restErr.Headers.Get("X-Accepted-Oauth-Scopes"),
    restErr.Headers.Get("X-Oauth-Scopes"),
    restErr.RequestURL.Hostname()),
    }
    }

    With the project command, the GraphQL request is made with (*GraphQLClient).QueryWithContext(), which uses a github.com/cli/shurcooL-graphql client. The API auth error is handled here:
    https://github.com/shurcooL/graphql/blob/main/graphql.go#L77-L80

    For a non 200 HTTP response code, the shurcooL-graphql client returns a generic error rather than the custom *ghAPI.GraphQLError type, so it doesn't match the error targets in handleResponse

    cli/api/client.go

    Lines 161 to 185 in 88a7e52

    func handleResponse(err error) error {
    if err == nil {
    return nil
    }
    var restErr *ghAPI.HTTPError
    if errors.As(err, &restErr) {
    return HTTPError{
    HTTPError: restErr,
    scopesSuggestion: generateScopesSuggestion(restErr.StatusCode,
    restErr.Headers.Get("X-Accepted-Oauth-Scopes"),
    restErr.Headers.Get("X-Oauth-Scopes"),
    restErr.RequestURL.Hostname()),
    }
    }
    var gqlErr *ghAPI.GraphQLError
    if errors.As(err, &gqlErr) {
    return GraphQLError{
    GraphQLError: gqlErr,
    }
    }
    return err
    }

    As such, the error doesn't match the final api.GraphQLError target check where the request was originally made in (Client).OwnerIDAndType

    var graphErr api.GraphQLError
    if errors.As(err, &graphErr) {
    if graphErr.Match("NOT_FOUND", "user") && graphErr.Match("NOT_FOUND", "organization") {
    return "", "", err
    } else if graphErr.Match("NOT_FOUND", "organization") { // org isn't found must be a user
    return query.User.Id, UserOwner, nil
    } else if graphErr.Match("NOT_FOUND", "user") { // user isn't found must be an org
    return query.Organization.Id, OrgOwner, nil
    }
    }
    }
    return "", "", errors.New("unknown owner type")

    So we're left with "unknown owner type" error message.

    The simplest fix may be to wrap the GraphQL error with the "unknown owner type" error (or another message altogether):

    --- return "", "", errors.New("unknown owner type")
    +++ return "", "", fmt.Errorf("unknown owner type: %w", err)

    That would result in a more informative, (maybe ugly) error message:

    unknown owner type: non-200 OK status code: 401 Unauthorized body: "{\"message\":\"Bad credentials\",\"documentation_url\":\"https://docs.github.com/graphql\"}"
    
  6. adityasharad commented on Mar 12, 2025

    @adityasharad

    A similar error unknown owner type can be observed with a valid token, but with insufficient scopes, e.g. running gh project field-list <project number> --owner <owner> with a token containing repo scope but missing read:org and read:project scope. Setting GH_DEBUG=api gives a more detailed error trace from the GitHub API, which reveals the missing read:org scope first.

    The error message when just the read:project scope is missing is more meaningful: error: your authentication token is missing required scopes [read:project] -- this would be really helpful for all such missing scopes!

    (testing with 2.67.0 on macOS Sequoia)

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

    bugneeds-triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions