Skip to content

Commit 237fd04

Browse files
committed
use errors.Wrap
1 parent 933086b commit 237fd04

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

command/pr_create.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/github/gh-cli/api"
1010
"github.com/github/gh-cli/context"
1111
"github.com/github/gh-cli/git"
12+
"github.com/pkg/errors"
1213
"github.com/spf13/cobra"
1314
)
1415

@@ -31,12 +32,12 @@ func prCreate(cmd *cobra.Command, _ []string) error {
3132

3233
head, err := ctx.Branch()
3334
if err != nil {
34-
return fmt.Errorf("could not determine current branch: %s", err)
35+
return errors.Wrap(err, "could not determine current branch")
3536
}
3637

3738
remote, err := guessRemote(ctx)
3839
if err != nil {
39-
return err
40+
return errors.Wrap(err, "could not determine suitable remote")
4041
}
4142

4243
if err = git.Push(remote, fmt.Sprintf("HEAD:%s", head)); err != nil {
@@ -45,11 +46,11 @@ func prCreate(cmd *cobra.Command, _ []string) error {
4546

4647
title, err := cmd.Flags().GetString("title")
4748
if err != nil {
48-
return err
49+
return errors.Wrap(err, "could not parse title")
4950
}
5051
body, err := cmd.Flags().GetString("body")
5152
if err != nil {
52-
return err
53+
return errors.Wrap(err, "could not parse body")
5354
}
5455

5556
interactive := title == "" || body == ""
@@ -92,7 +93,7 @@ func prCreate(cmd *cobra.Command, _ []string) error {
9293

9394
err := survey.Ask(qs, &inProgress)
9495
if err != nil {
95-
return fmt.Errorf("could not prompt: %s", err)
96+
return errors.Wrap(err, "could not prompt")
9697
}
9798
confirmAnswers := struct {
9899
Confirmation string
@@ -113,7 +114,7 @@ func prCreate(cmd *cobra.Command, _ []string) error {
113114

114115
err = survey.Ask(confirmQs, &confirmAnswers)
115116
if err != nil {
116-
return fmt.Errorf("could not prompt: %s", err)
117+
return errors.Wrap(err, "could not prompt")
117118
}
118119

119120
switch confirmAnswers.Confirmation {
@@ -136,7 +137,7 @@ func prCreate(cmd *cobra.Command, _ []string) error {
136137
}
137138
base, err := cmd.Flags().GetString("base")
138139
if err != nil {
139-
return err
140+
return errors.Wrap(err, "could not parse base")
140141
}
141142
if base == "" {
142143
// TODO: use default branch for the repo
@@ -145,17 +146,17 @@ func prCreate(cmd *cobra.Command, _ []string) error {
145146

146147
client, err := apiClientForContext(ctx)
147148
if err != nil {
148-
return fmt.Errorf("could not initialize api client: %s", err)
149+
return errors.Wrap(err, "could not initialize api client")
149150
}
150151

151152
repo, err := ctx.BaseRepo()
152153
if err != nil {
153-
return fmt.Errorf("could not determine GitHub repo: %s", err)
154+
return errors.Wrap(err, "could not determine GitHub repo")
154155
}
155156

156157
isDraft, err := cmd.Flags().GetBool("draft")
157158
if err != nil {
158-
return err
159+
return errors.Wrap(err, "could not parse draft")
159160
}
160161

161162
params := map[string]interface{}{
@@ -168,7 +169,7 @@ func prCreate(cmd *cobra.Command, _ []string) error {
168169

169170
pr, err := api.CreatePullRequest(client, repo, params)
170171
if err != nil {
171-
return fmt.Errorf("failed to create PR: %s", err)
172+
return errors.Wrap(err, "failed to create PR")
172173
}
173174

174175
fmt.Fprintln(cmd.OutOrStdout(), pr.URL)
@@ -178,14 +179,14 @@ func prCreate(cmd *cobra.Command, _ []string) error {
178179
func guessRemote(ctx context.Context) (string, error) {
179180
remotes, err := ctx.Remotes()
180181
if err != nil {
181-
return "", fmt.Errorf("could not determine suitable remote: %s", err)
182+
return "", errors.Wrap(err, "could not determine suitable remote")
182183
}
183184

184185
// TODO: consolidate logic with fsContext.BaseRepo
185186
// TODO: check if the GH repo that the remote points to is writeable
186187
remote, err := remotes.FindByName("upstream", "github", "origin", "*")
187188
if err != nil {
188-
return "", fmt.Errorf("could not determine suitable remote: %s", err)
189+
return "", errors.Wrap(err, "could not determine suitable remote")
189190
}
190191

191192
return remote.Name, nil

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/mattn/go-isatty v0.0.9
1010
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b
1111
github.com/mitchellh/go-homedir v1.1.0
12+
github.com/pkg/errors v0.8.1
1213
github.com/spf13/cobra v0.0.5
1314
github.com/stretchr/testify v1.3.0 // indirect
1415
golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG
3434
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
3535
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
3636
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
37+
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
38+
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
3739
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
3840
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
3941
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=

0 commit comments

Comments
 (0)