Skip to content

Commit

Permalink
Export GraphQL error types
Browse files Browse the repository at this point in the history
  • Loading branch information
heaths committed May 9, 2022
1 parent c41a127 commit fc2567a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
26 changes: 26 additions & 0 deletions gh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,32 @@ func TestGQLClient(t *testing.T) {
assert.Equal(t, "token GH_TOKEN", http.Requests[0].Header.Get("Authorization"))
}

func TestGQLClientError(t *testing.T) {
tempDir := t.TempDir()
orig_GH_CONFIG_DIR := os.Getenv("GH_CONFIG_DIR")
orig_GH_TOKEN := os.Getenv("GH_TOKEN")
t.Cleanup(func() {
os.Setenv("GH_CONFIG_DIR", orig_GH_CONFIG_DIR)
os.Setenv("GH_TOKEN", orig_GH_TOKEN)
})
os.Setenv("GH_CONFIG_DIR", tempDir)
os.Setenv("GH_TOKEN", "GH_TOKEN")

http := httpmock.NewRegistry(t)
http.Register(
httpmock.GQL("QUERY"),
httpmock.StringResponse(`{"errors":[{"type":"NOT_FOUND","path":["organization"],"message":"Could not resolve to an Organization with the login of 'cli'."}]}`),
)

client, err := GQLClient(&api.ClientOptions{Transport: http})
assert.NoError(t, err)

vars := map[string]interface{}{}
res := struct{ Organization struct{ Name string } }{}
err = client.Do("QUERY", vars, &res)
assert.EqualError(t, err, "GQL error: Could not resolve to an Organization with the login of 'cli'.")
}

func TestHTTPClient(t *testing.T) {
tempDir := t.TempDir()
orig_GH_CONFIG_DIR := os.Getenv("GH_CONFIG_DIR")
Expand Down
22 changes: 2 additions & 20 deletions internal/api/gql_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"io"
"net/http"
"strings"

"github.com/cli/go-gh/pkg/api"
graphql "github.com/cli/shurcooL-graphql"
Expand Down Expand Up @@ -75,7 +74,7 @@ func (c gqlClient) Do(query string, variables map[string]interface{}, response i
}

if len(gr.Errors) > 0 {
return &gqlErrorResponse{Errors: gr.Errors}
return &api.GQLError{Errors: gr.Errors}
}

return nil
Expand All @@ -97,22 +96,5 @@ func (c gqlClient) Query(name string, q interface{}, variables map[string]interf

type gqlResponse struct {
Data interface{}
Errors []gqlError
}

type gqlError struct {
Type string
Message string
}

type gqlErrorResponse struct {
Errors []gqlError
}

func (gr gqlErrorResponse) Error() string {
errorMessages := make([]string, 0, len(gr.Errors))
for _, e := range gr.Errors {
errorMessages = append(errorMessages, e.Message)
}
return fmt.Sprintf("GQL error: %s", strings.Join(errorMessages, "\n"))
Errors []api.GQLErrorItem
}
22 changes: 22 additions & 0 deletions pkg/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
package api

import (
"fmt"
"io"
"net/http"
"strings"
"time"
)

Expand Down Expand Up @@ -97,3 +99,23 @@ type GQLClient interface {
// to the GitHub GraphQL schema.
Query(name string, query interface{}, variables map[string]interface{}) error
}

// GQLError contains GQLErrors from a GraphQ request.
type GQLError struct {
Errors []GQLErrorItem
}

// GQLErrorItem contains error information from a GraphQL request.
type GQLErrorItem struct {
Type string
Message string
}

// Error formats all GQLError messages.
func (gr GQLError) Error() string {
errorMessages := make([]string, 0, len(gr.Errors))
for _, e := range gr.Errors {
errorMessages = append(errorMessages, e.Message)
}
return fmt.Sprintf("GQL error: %s", strings.Join(errorMessages, "\n"))
}

0 comments on commit fc2567a

Please sign in to comment.