Skip to content

Commit 0f6daa0

Browse files
committed
Don't panic
1 parent 3a931ea commit 0f6daa0

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

api/client.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,20 @@ func graphQL(query string, variables map[string]string, v interface{}) error {
4949
url := "https://api.github.com/graphql"
5050
reqBody, err := json.Marshal(map[string]interface{}{"query": query, "variables": variables})
5151
if err != nil {
52-
panic(err)
52+
return err
5353
}
5454

5555
req, err := http.NewRequest("POST", url, bytes.NewBuffer(reqBody))
5656
if err != nil {
57-
panic(err)
57+
return err
58+
}
59+
60+
token, err := getToken()
61+
if err != nil {
62+
return err
5863
}
59-
req.Header.Set("Authorization", "token "+getToken())
64+
65+
req.Header.Set("Authorization", "token "+token)
6066
req.Header.Set("Content-Type", "application/json; charset=utf-8")
6167
req.Header.Set("User-Agent", "GitHub CLI "+version.Version)
6268

@@ -65,7 +71,7 @@ func graphQL(query string, variables map[string]string, v interface{}) error {
6571
client := &http.Client{}
6672
resp, err := client.Do(req)
6773
if err != nil {
68-
panic(err)
74+
return err
6975
}
7076
defer resp.Body.Close()
7177

@@ -132,18 +138,18 @@ func debugResponse(resp *http.Response, body string) {
132138
}
133139

134140
// TODO: Everything below this line will be removed when Nate's context work is complete
135-
func getToken() string {
141+
func getToken() (string, error) {
136142
usr, err := user.Current()
137143
if err != nil {
138-
panic(err)
144+
return "", err
139145
}
140146

141147
content, err := ioutil.ReadFile(usr.HomeDir + "/.config/hub")
142148
if err != nil {
143-
panic(err)
149+
return "", err
144150
}
145151

146152
r := regexp.MustCompile(`oauth_token: (\w+)`)
147153
token := r.FindStringSubmatch(string(content))
148-
return token[1]
154+
return token[1], nil
149155
}

command/pr.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,17 @@ var prListCmd = &cobra.Command{
2727
Use: "list",
2828
Short: "List pull requests",
2929
Run: func(cmd *cobra.Command, args []string) {
30-
ExecutePr()
30+
err := ExecutePr()
31+
if err != nil {
32+
panic(err) // In the future this should handle the error better, but for now panic seems like a valid reaction
33+
}
3134
},
3235
}
3336

34-
func ExecutePr() {
37+
func ExecutePr() error {
3538
prPayload, err := api.PullRequests()
3639
if err != nil {
37-
panic(err)
40+
return error
3841
}
3942

4043
fmt.Printf("Current Pr\n")
@@ -49,6 +52,8 @@ func ExecutePr() {
4952
for _, pr := range prPayload.ReviewRequested {
5053
printPr(pr)
5154
}
55+
56+
return nil
5257
}
5358

5459
func printPr(pr api.PullRequest) {

0 commit comments

Comments
 (0)