Skip to content

Replace github.com/pkg/errors with stdlib #5220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions api/approval.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ package api
import (
"bufio"
"bytes"
"fmt"
"io"

"github.com/go-playground/validator/v10"
"github.com/pkg/errors"

"k8s.io/enhancements/pkg/yaml"
)
Expand Down Expand Up @@ -51,7 +51,7 @@ type PRRApproval struct {
func (prr *PRRApproval) Validate() error {
v := validator.New()
if err := v.Struct(prr); err != nil {
return errors.Wrap(err, "running validation")
return fmt.Errorf("running validation: %w", err)
}

return nil
Expand Down Expand Up @@ -128,17 +128,17 @@ func (p *PRRHandler) Parse(in io.Reader) (*PRRApproval, error) {

approval := &PRRApproval{}
if err := scanner.Err(); err != nil {
return approval, errors.Wrap(err, "reading file")
return approval, fmt.Errorf("reading file: %w", err)
}

if err := yaml.UnmarshalStrict(body.Bytes(), &approval); err != nil {
p.Errors = append(p.Errors, errors.Wrap(err, "error unmarshalling YAML"))
return approval, errors.Wrap(err, "unmarshalling YAML")
p.Errors = append(p.Errors, fmt.Errorf("error unmarshalling YAML: %w", err))
return approval, fmt.Errorf("unmarshalling YAML: %w", err)
}

if valErr := approval.Validate(); valErr != nil {
p.Errors = append(p.Errors, errors.Wrap(valErr, "validating PRR"))
return approval, errors.Wrap(valErr, "validating PRR")
p.Errors = append(p.Errors, fmt.Errorf("validating PRR: %w", valErr))
return approval, fmt.Errorf("validating PRR: %w", valErr)
}

return approval, nil
Expand Down
2 changes: 1 addition & 1 deletion api/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package api

import (
"github.com/pkg/errors"
"errors"
)

var (
Expand Down
27 changes: 8 additions & 19 deletions api/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ package api

import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"net/http"
"regexp"
"sort"

"github.com/pkg/errors"

"k8s.io/enhancements/pkg/yaml"
)

Expand Down Expand Up @@ -92,18 +91,13 @@ var _ GroupFetcher = &RemoteGroupFetcher{}
func (f *RemoteGroupFetcher) FetchGroups() ([]string, error) {
resp, err := http.Get(f.GroupsListURL)
if err != nil {
return nil, errors.Wrap(err, "fetching SIG list")
return nil, fmt.Errorf("fetching SIG list: %w", err)
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, errors.New(
fmt.Sprintf(
"invalid status code when fetching SIG list: %d",
resp.StatusCode,
),
)
return nil, fmt.Errorf("invalid status code when fetching SIG list: %d", resp.StatusCode)
}

re := regexp.MustCompile(`- dir: (.*)$`)
Expand All @@ -118,7 +112,7 @@ func (f *RemoteGroupFetcher) FetchGroups() ([]string, error) {
}

if err := scanner.Err(); err != nil {
return nil, errors.Wrap(err, "scanning SIG list")
return nil, fmt.Errorf("scanning SIG list: %w", err)
}

sort.Strings(result)
Expand All @@ -130,31 +124,26 @@ func (f *RemoteGroupFetcher) FetchGroups() ([]string, error) {
func (f *RemoteGroupFetcher) FetchPRRApprovers() ([]string, error) {
resp, err := http.Get(f.OwnersAliasesURL)
if err != nil {
return nil, errors.Wrap(err, "fetching owners aliases")
return nil, fmt.Errorf("fetching owners aliases: %w", err)
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, errors.New(
fmt.Sprintf(
"invalid status code when fetching owners aliases: %d",
resp.StatusCode,
),
)
return nil, fmt.Errorf("invalid status code when fetching owners aliases: %d", resp.StatusCode)
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "reading owners aliases")
return nil, fmt.Errorf("reading owners aliases: %w", err)
}

config := &struct {
Data map[string][]string `json:"aliases,omitempty" yaml:"aliases,omitempty"`
}{}

if err := yaml.UnmarshalStrict(body, config); err != nil {
return nil, errors.Wrap(err, "unmarshalling owners aliases")
return nil, fmt.Errorf("unmarshalling owners aliases: %w", err)
}

var result []string
Expand Down
7 changes: 3 additions & 4 deletions api/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"strings"

"github.com/go-playground/validator/v10"
"github.com/pkg/errors"

"k8s.io/enhancements/pkg/yaml"
)
Expand Down Expand Up @@ -177,7 +176,7 @@ func (k *KEPHandler) Parse(in io.Reader) (*Proposal, error) {
}

if err := scanner.Err(); err != nil {
return kep, errors.Wrap(err, "reading file")
return kep, fmt.Errorf("reading file: %w", err)
}

// this file is just the KEP metadata
Expand All @@ -187,8 +186,8 @@ func (k *KEPHandler) Parse(in io.Reader) (*Proposal, error) {
}

if err := yaml.UnmarshalStrict(metadata, &kep); err != nil {
k.Errors = append(k.Errors, errors.Wrap(err, "error unmarshalling YAML"))
return kep, errors.Wrap(err, "unmarshalling YAML")
k.Errors = append(k.Errors, fmt.Errorf("error unmarshalling YAML: %w", err))
return kep, fmt.Errorf("unmarshalling YAML: %w", err)
}

if err := k.validateStruct(kep); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ require (
github.com/google/go-github/v33 v33.0.0
github.com/maxbrunsfeld/counterfeiter/v6 v6.3.0
github.com/olekukonko/tablewriter v0.0.5
github.com/pkg/errors v0.9.1
github.com/psampaz/go-mod-outdated v0.8.0
github.com/sirupsen/logrus v1.7.0
github.com/spf13/cobra v1.1.1
Expand Down Expand Up @@ -44,6 +43,7 @@ require (
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.6.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
Expand Down
2 changes: 1 addition & 1 deletion keps/sig-scheduling/624-scheduling-framework/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ func NewServiceAffinity(args *runtime.Unknown, h FrameworkHandle) (Plugin, error
LabelName, LabelValue string
}
if err := json.Unmarshal(args.Raw, &config); err != nil {
return nil, errors.Wrap(err, "could not parse args")
return nil, fmt.Errorf("could not parse args: %w", err)
}
//...
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/kepctl/commands/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ limitations under the License.
package commands

import (
"github.com/pkg/errors"
"fmt"

"github.com/spf13/cobra"

"k8s.io/enhancements/api"
Expand Down Expand Up @@ -128,7 +129,7 @@ func addCreate(topLevel *cobra.Command) {
func runCreate(opts *proposal.CreateOpts) error {
rc, err := repo.New(rootOpts.RepoPath)
if err != nil {
return errors.Wrap(err, "creating repo client")
return fmt.Errorf("creating repo client: %w", err)
}

opts.Repo = rc
Expand Down
5 changes: 3 additions & 2 deletions pkg/kepctl/commands/promote.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ limitations under the License.
package commands

import (
"github.com/pkg/errors"
"fmt"

"github.com/spf13/cobra"

"k8s.io/enhancements/pkg/proposal"
Expand Down Expand Up @@ -65,7 +66,7 @@ func addPromote(topLevel *cobra.Command) {
func runPromote(opts *proposal.PromoteOpts) error {
rc, err := repo.New(rootOpts.RepoPath)
if err != nil {
return errors.Wrap(err, "creating repo client")
return fmt.Errorf("creating repo client: %w", err)
}

opts.Repo = rc
Expand Down
3 changes: 1 addition & 2 deletions pkg/kepctl/commands/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"fmt"
"os"

"github.com/pkg/errors"
"github.com/spf13/cobra"

"k8s.io/enhancements/pkg/output"
Expand Down Expand Up @@ -115,7 +114,7 @@ func addQuery(topLevel *cobra.Command) {
func runQuery(opts *repo.QueryOpts) error {
rc, err := repo.New(rootOpts.RepoPath)
if err != nil {
return errors.Wrap(err, "creating repo client")
return fmt.Errorf("creating repo client: %w", err)
}
rc.TokenPath = rootOpts.TokenPath

Expand Down
24 changes: 9 additions & 15 deletions pkg/kepval/approval.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ limitations under the License.
package kepval

import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/blang/semver/v4"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"

"k8s.io/enhancements/api"
Expand All @@ -32,7 +32,7 @@ import (
func ValidatePRR(kep *api.Proposal, h *api.PRRHandler, prrDir string) error {
requiredPRRApproval, err := isPRRRequired(kep)
if err != nil {
return errors.Wrap(err, "checking if PRR is required")
return fmt.Errorf("checking if PRR is required: %w", err)
}

if !requiredPRRApproval {
Expand All @@ -55,24 +55,24 @@ func ValidatePRR(kep *api.Proposal, h *api.PRRHandler, prrDir string) error {
}

if err != nil {
return errors.Wrapf(err, "could not open file %s", prrFilepath)
return fmt.Errorf("could not open file %s: %w", prrFilepath, err)
}

// TODO: Create a context to hold the parsers
prr, prrParseErr := h.Parse(prrFile)
if prrParseErr != nil {
return errors.Wrap(prrParseErr, "parsing PRR approval file")
return fmt.Errorf("parsing PRR approval file: %w", prrParseErr)
}

// TODO: This shouldn't be required once we push the errors into the
// parser struct
if prr.Error != nil {
return errors.Wrapf(prr.Error, "%v has an error", prrFilepath)
return fmt.Errorf("%v has an error: %w", prrFilepath, prr.Error)
}

stagePRRApprover, err := prr.ApproverForStage(kep.Stage)
if err != nil {
return errors.Wrapf(err, "getting PRR approver for %s stage", kep.Stage)
return fmt.Errorf("getting PRR approver for %s stage: %w", kep.Stage, err)
}

if stagePRRApprover == "" {
Expand All @@ -85,13 +85,7 @@ func ValidatePRR(kep *api.Proposal, h *api.PRRHandler, prrDir string) error {

validApprover := api.IsOneOf(stagePRRApprover, h.PRRApprovers)
if !validApprover {
return errors.New(
fmt.Sprintf(
"this contributor (%s) is not a PRR approver (%v)",
stagePRRApprover,
h.PRRApprovers,
),
)
return fmt.Errorf("this contributor (%s) is not a PRR approver (%v)", stagePRRApprover, h.PRRApprovers)
}

return nil
Expand All @@ -116,12 +110,12 @@ func isPRRRequired(kep *api.Proposal) (required bool, err error) {
// TODO: Consider making this a function
prrRequiredAtSemVer, err := semver.ParseTolerant("v1.21")
if err != nil {
return required, errors.Wrap(err, "creating a SemVer object for PRRs")
return required, fmt.Errorf("creating a SemVer object for PRRs: %w", err)
}

latestSemVer, err := semver.ParseTolerant(kep.LatestMilestone)
if err != nil {
return required, errors.Wrap(err, "creating a SemVer object for latest milestone")
return required, fmt.Errorf("creating a SemVer object for latest milestone: %w", err)
}

if latestSemVer.LT(prrRequiredAtSemVer) {
Expand Down
3 changes: 1 addition & 2 deletions pkg/proposal/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"strings"
"time"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"

"k8s.io/enhancements/api"
Expand Down Expand Up @@ -115,7 +114,7 @@ func createKEP(kep *api.Proposal, opts *CreateOpts) error {
)

if writeErr := ioutil.WriteFile(newPath, template, os.ModePerm); writeErr != nil {
return errors.Wrapf(writeErr, "writing KEP data to file")
return fmt.Errorf("writing KEP data to file: %w", writeErr)
}

return nil
Expand Down
3 changes: 1 addition & 2 deletions pkg/proposal/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ limitations under the License.
package proposal

import (
"errors"
"fmt"
"regexp"

"github.com/pkg/errors"
)

type Options struct {
Expand Down
6 changes: 3 additions & 3 deletions pkg/repo/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ limitations under the License.
package repo

import (
"errors"
"fmt"
"regexp"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"

"k8s.io/enhancements/api"
Expand Down Expand Up @@ -95,7 +95,7 @@ func (r *Repo) Query(opts *QueryOpts) ([]*api.Proposal, error) {
if r.TokenPath != "" {
logrus.Infof("Setting GitHub token: %v", r.TokenPath)
if tokenErr := r.SetGitHubToken(r.TokenPath); tokenErr != nil {
return nil, errors.Wrapf(tokenErr, "setting GitHub token")
return nil, fmt.Errorf("setting GitHub token: %w", tokenErr)
}
}

Expand All @@ -105,7 +105,7 @@ func (r *Repo) Query(opts *QueryOpts) ([]*api.Proposal, error) {
// KEPs in the local filesystem
localKEPs, err := r.LoadLocalKEPs(sig)
if err != nil {
return nil, errors.Wrap(err, "loading local KEPs")
return nil, fmt.Errorf("loading local KEPs: %w", err)
}

allKEPs = append(allKEPs, localKEPs...)
Expand Down
Loading