Skip to content

Commit

Permalink
Add GQL error logging support
Browse files Browse the repository at this point in the history
  • Loading branch information
codepope committed Jun 24, 2020
1 parent ec85266 commit 698f1ed
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
11 changes: 11 additions & 0 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,24 @@ import (
"errors"
"fmt"
"net/http"
"os"
"regexp"
"strings"

"github.com/machinebox/graphql"
)

var baseURL string
var errorLog bool

func SetBaseURL(url string) {
baseURL = url
}

func SetErrorLog(log bool) {
errorLog = log
}

type Client struct {
httpClient *http.Client
client *graphql.Client
Expand Down Expand Up @@ -58,6 +64,11 @@ func (c *Client) RunWithContext(ctx context.Context, req *graphql.Request) (Quer
if err != nil && strings.HasPrefix(err.Error(), "graphql: ") {
return resp, errors.New(strings.TrimPrefix(err.Error(), "graphql: "))
}

if resp.Errors != nil && errorLog {
fmt.Fprintf(os.Stderr, "Error: %+v\n", resp.Errors)
}

return resp, err
}

Expand Down
2 changes: 2 additions & 0 deletions flyctl/flyctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,13 @@ func initViper() {
viper.BindEnv(ConfigAPIToken, "FLY_ACCESS_TOKEN")
viper.BindEnv(ConfigAPIToken, "FLY_API_TOKEN")
viper.BindEnv(ConfigVerboseOutput, "VERBOSE")
viper.BindEnv(ConfigGQLErrorLogging, "GQLErrorLogging")

viper.SetEnvPrefix("FLY")
viper.AutomaticEnv()

api.SetBaseURL(viper.GetString(ConfigAPIBaseURL))
api.SetErrorLog(viper.GetBool(ConfigGQLErrorLogging))
}

func loadConfig() error {
Expand Down

0 comments on commit 698f1ed

Please sign in to comment.