From 3420220b5a70334e53c4222399b1f17d0b712281 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Wed, 9 Oct 2019 15:32:41 -0700 Subject: [PATCH] Don't panic --- api/client.go | 22 ++++++++++++++-------- command/pr.go | 11 ++++++++--- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/api/client.go b/api/client.go index 82c72d9e1..fc6895ca1 100644 --- a/api/client.go +++ b/api/client.go @@ -49,14 +49,20 @@ func graphQL(query string, variables map[string]string, v interface{}) error { url := "https://api.github.com/graphql" reqBody, err := json.Marshal(map[string]interface{}{"query": query, "variables": variables}) if err != nil { - panic(err) + return err } req, err := http.NewRequest("POST", url, bytes.NewBuffer(reqBody)) if err != nil { - panic(err) + return err } - req.Header.Set("Authorization", "token "+getToken()) + + token, err := getToken() + if err != nil { + return err + } + + req.Header.Set("Authorization", "token "+token) req.Header.Set("Content-Type", "application/json; charset=utf-8") req.Header.Set("User-Agent", "GitHub CLI "+version.Version) @@ -65,7 +71,7 @@ func graphQL(query string, variables map[string]string, v interface{}) error { client := &http.Client{} resp, err := client.Do(req) if err != nil { - panic(err) + return err } defer resp.Body.Close() @@ -132,18 +138,18 @@ func debugResponse(resp *http.Response, body string) { } // TODO: Everything below this line will be removed when Nate's context work is complete -func getToken() string { +func getToken() (string, error) { usr, err := user.Current() if err != nil { - panic(err) + return "", err } content, err := ioutil.ReadFile(usr.HomeDir + "/.config/hub") if err != nil { - panic(err) + return "", err } r := regexp.MustCompile(`oauth_token: (\w+)`) token := r.FindStringSubmatch(string(content)) - return token[1] + return token[1], nil } diff --git a/command/pr.go b/command/pr.go index 4b9c02732..79d176e03 100644 --- a/command/pr.go +++ b/command/pr.go @@ -27,14 +27,17 @@ var prListCmd = &cobra.Command{ Use: "list", Short: "List pull requests", Run: func(cmd *cobra.Command, args []string) { - ExecutePr() + err := ExecutePr() + if err != nil { + panic(err) // In the future this should handle the error better, but for now panic seems like a valid reaction + } }, } -func ExecutePr() { +func ExecutePr() error { prPayload, err := api.PullRequests() if err != nil { - panic(err) + return error } fmt.Printf("Current Pr\n") @@ -49,6 +52,8 @@ func ExecutePr() { for _, pr := range prPayload.ReviewRequested { printPr(pr) } + + return nil } func printPr(pr api.PullRequest) {