From 9122bc181cbde6d2b0f8cafee1e6f56c3eab1d5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Thu, 23 Jan 2020 13:19:28 +0100 Subject: [PATCH] Migrate away from `errors.Wrap()` Turns out the "standard" way of wrapping errors in Go is via `fmt.Errorf("%w")`, which doesn't require an external package and also allows a finer control of error sentence formatting. --- api/queries_repo.go | 4 +--- command/issue.go | 8 ++++---- command/pr_create.go | 25 ++++++++++++------------- command/title_body_survey.go | 11 ++++++----- go.mod | 1 - 5 files changed, 23 insertions(+), 26 deletions(-) diff --git a/api/queries_repo.go b/api/queries_repo.go index f974b41cb..7dff79087 100644 --- a/api/queries_repo.go +++ b/api/queries_repo.go @@ -2,8 +2,6 @@ package api import ( "fmt" - - "github.com/pkg/errors" ) // Repository contains information about a GitHub repo @@ -37,7 +35,7 @@ func GitHubRepo(client *Client, ghRepo Repo) (*Repository, error) { if err != nil || result.Repository.ID == "" { newErr := fmt.Errorf("failed to determine repository ID for '%s/%s'", owner, repo) if err != nil { - newErr = errors.Wrap(err, newErr.Error()) + newErr = fmt.Errorf("%s: %w", newErr, err) } return nil, newErr } diff --git a/command/issue.go b/command/issue.go index 139ab3337..566393658 100644 --- a/command/issue.go +++ b/command/issue.go @@ -1,6 +1,7 @@ package command import ( + "errors" "fmt" "io" "net/url" @@ -14,7 +15,6 @@ import ( "github.com/github/gh-cli/git" "github.com/github/gh-cli/pkg/githubtemplate" "github.com/github/gh-cli/utils" - "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" ) @@ -314,11 +314,11 @@ func issueCreate(cmd *cobra.Command, args []string) error { title, err := cmd.Flags().GetString("title") if err != nil { - return errors.Wrap(err, "could not parse title") + return fmt.Errorf("could not parse title: %w", err) } body, err := cmd.Flags().GetString("body") if err != nil { - return errors.Wrap(err, "could not parse body") + return fmt.Errorf("could not parse body: %w", err) } interactive := title == "" || body == "" @@ -326,7 +326,7 @@ func issueCreate(cmd *cobra.Command, args []string) error { if interactive { tb, err := titleBodySurvey(cmd, title, body, templateFiles) if err != nil { - return errors.Wrap(err, "could not collect title and/or body") + return fmt.Errorf("could not collect title and/or body: %w", err) } action = tb.Action diff --git a/command/pr_create.go b/command/pr_create.go index a72dff7ff..007ba8638 100644 --- a/command/pr_create.go +++ b/command/pr_create.go @@ -9,7 +9,6 @@ import ( "github.com/github/gh-cli/git" "github.com/github/gh-cli/pkg/githubtemplate" "github.com/github/gh-cli/utils" - "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -26,12 +25,12 @@ func prCreate(cmd *cobra.Command, _ []string) error { repo, err := ctx.BaseRepo() if err != nil { - return errors.Wrap(err, "could not determine GitHub repo") + return fmt.Errorf("could not determine GitHub repo: %w", err) } head, err := ctx.Branch() if err != nil { - return errors.Wrap(err, "could not determine current branch") + return fmt.Errorf("could not determine current branch: %w", err) } remote, err := guessRemote(ctx) @@ -56,7 +55,7 @@ func prCreate(cmd *cobra.Command, _ []string) error { isWeb, err := cmd.Flags().GetBool("web") if err != nil { - return errors.Wrap(err, "could not parse web") + return fmt.Errorf("could not parse web: %q", err) } if isWeb { openURL := fmt.Sprintf(`https://github.com/%s/%s/pull/%s`, repo.RepoOwner(), repo.RepoName(), head) @@ -66,11 +65,11 @@ func prCreate(cmd *cobra.Command, _ []string) error { title, err := cmd.Flags().GetString("title") if err != nil { - return errors.Wrap(err, "could not parse title") + return fmt.Errorf("could not parse title: %w", err) } body, err := cmd.Flags().GetString("body") if err != nil { - return errors.Wrap(err, "could not parse body") + return fmt.Errorf("could not parse body: %w", err) } action := SubmitAction @@ -86,7 +85,7 @@ func prCreate(cmd *cobra.Command, _ []string) error { tb, err := titleBodySurvey(cmd, title, body, templateFiles) if err != nil { - return errors.Wrap(err, "could not collect title and/or body") + return fmt.Errorf("could not collect title and/or body: %w", err) } action = tb.Action @@ -106,7 +105,7 @@ func prCreate(cmd *cobra.Command, _ []string) error { base, err := cmd.Flags().GetString("base") if err != nil { - return errors.Wrap(err, "could not parse base") + return fmt.Errorf("could not parse base: %w", err) } if base == "" { // TODO: use default branch for the repo @@ -115,12 +114,12 @@ func prCreate(cmd *cobra.Command, _ []string) error { client, err := apiClientForContext(ctx) if err != nil { - return errors.Wrap(err, "could not initialize api client") + return fmt.Errorf("could not initialize API client: %w", err) } isDraft, err := cmd.Flags().GetBool("draft") if err != nil { - return errors.Wrap(err, "could not parse draft") + return fmt.Errorf("could not parse draft: %w", err) } if action == SubmitAction { @@ -134,7 +133,7 @@ func prCreate(cmd *cobra.Command, _ []string) error { pr, err := api.CreatePullRequest(client, repo, params) if err != nil { - return errors.Wrap(err, "failed to create pull request") + return fmt.Errorf("failed to create pull request: %w", err) } fmt.Fprintln(cmd.OutOrStdout(), pr.URL) @@ -166,14 +165,14 @@ func prCreate(cmd *cobra.Command, _ []string) error { func guessRemote(ctx context.Context) (string, error) { remotes, err := ctx.Remotes() if err != nil { - return "", errors.Wrap(err, "could not read git remotes") + return "", fmt.Errorf("could not read git remotes: %w", err) } // TODO: consolidate logic with fsContext.BaseRepo // TODO: check if the GH repo that the remote points to is writeable remote, err := remotes.FindByName("upstream", "github", "origin", "*") if err != nil { - return "", errors.Wrap(err, "could not determine suitable remote") + return "", fmt.Errorf("could not determine suitable remote: %w", err) } return remote.Name, nil diff --git a/command/title_body_survey.go b/command/title_body_survey.go index 2b11c326f..914d03c0b 100644 --- a/command/title_body_survey.go +++ b/command/title_body_survey.go @@ -1,10 +1,11 @@ package command import ( + "fmt" + "github.com/AlecAivazis/survey/v2" "github.com/github/gh-cli/pkg/githubtemplate" "github.com/github/gh-cli/pkg/surveyext" - "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -42,7 +43,7 @@ func confirm() (Action, error) { err := survey.Ask(confirmQs, &confirmAnswers) if err != nil { - return -1, errors.Wrap(err, "could not prompt") + return -1, fmt.Errorf("could not prompt: %w", err) } return Action(confirmAnswers.Confirmation), nil @@ -68,7 +69,7 @@ func selectTemplate(templatePaths []string) (string, error) { }, } if err := survey.Ask(selectQs, &templateResponse); err != nil { - return "", errors.Wrap(err, "could not prompt") + return "", fmt.Errorf("could not prompt: %w", err) } } @@ -117,12 +118,12 @@ func titleBodySurvey(cmd *cobra.Command, providedTitle string, providedBody stri err := survey.Ask(qs, &inProgress) if err != nil { - return nil, errors.Wrap(err, "could not prompt") + return nil, fmt.Errorf("could not prompt: %w", err) } confirmA, err := confirm() if err != nil { - return nil, errors.Wrap(err, "unable to confirm") + return nil, fmt.Errorf("unable to confirm: %w", err) } inProgress.Action = confirmA diff --git a/go.mod b/go.mod index df49a1664..f0cf8445a 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,6 @@ require ( github.com/mattn/go-isatty v0.0.9 github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b github.com/mitchellh/go-homedir v1.1.0 - github.com/pkg/errors v0.8.1 github.com/spf13/cobra v0.0.5 github.com/spf13/pflag v1.0.5 github.com/vilmibm/go-termd v0.0.4