Skip to content

Commit 85d18c6

Browse files
skittdims
andauthored
Use built-in error wrapping instead of pkg/errors (#3689)
github.com/pkg/errors is archived and unmaintained; this replaces that as follows: * instead of extracting the cause for os.IsNotExist, use errors.Is directly (which unwraps errors itself) * wrap errors using fmt.Errorf (wrapped errors using pkg/errors add the error message after a colon, so this changes the format to match) * errors.Wrapf is replaced with fmt.Errorf Signed-off-by: Stephen Kitt <[email protected]> Co-authored-by: Davanum Srinivas <[email protected]>
1 parent 09f7fba commit 85d18c6

File tree

6 files changed

+14
-15
lines changed

6 files changed

+14
-15
lines changed

container/common/helpers.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
package common
1616

1717
import (
18+
"errors"
1819
"fmt"
20+
"io/fs"
1921
"math"
2022
"os"
2123
"path"
@@ -25,7 +27,6 @@ import (
2527

2628
"github.com/karrick/godirwalk"
2729
"github.com/opencontainers/cgroups"
28-
"github.com/pkg/errors"
2930
"golang.org/x/sys/unix"
3031

3132
"github.com/google/cadvisor/container"
@@ -308,7 +309,7 @@ func listDirectories(dirpath string, parent string, recursive bool, output map[s
308309
dirents, err := godirwalk.ReadDirents(dirpath, buf)
309310
if err != nil {
310311
// Ignore if this hierarchy does not exist.
311-
if os.IsNotExist(errors.Cause(err)) {
312+
if errors.Is(err, fs.ErrNotExist) {
312313
err = nil
313314
}
314315
return err

container/containerd/identifiers/validate.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@
3838
package identifiers
3939

4040
import (
41+
"fmt"
4142
"regexp"
4243

4344
"github.com/containerd/errdefs"
44-
"github.com/pkg/errors"
4545
)
4646

4747
const (
@@ -64,15 +64,15 @@ var (
6464
// In general identifiers that pass this validation should be safe for use as filesystem path components.
6565
func Validate(s string) error {
6666
if len(s) == 0 {
67-
return errors.Wrapf(errdefs.ErrInvalidArgument, "identifier must not be empty")
67+
return fmt.Errorf("identifier must not be empty: %w", errdefs.ErrInvalidArgument)
6868
}
6969

7070
if len(s) > maxLength {
71-
return errors.Wrapf(errdefs.ErrInvalidArgument, "identifier %q greater than maximum length (%d characters)", s, maxLength)
71+
return fmt.Errorf("identifier %q greater than maximum length (%d characters): %w", s, maxLength, errdefs.ErrInvalidArgument)
7272
}
7373

7474
if !identifierRe.MatchString(s) {
75-
return errors.Wrapf(errdefs.ErrInvalidArgument, "identifier %q must match %v", s, identifierRe)
75+
return fmt.Errorf("identifier %q must match %v: %w", s, identifierRe, errdefs.ErrInvalidArgument)
7676
}
7777
return nil
7878
}

container/containerd/namespaces/context.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ package namespaces
3131

3232
import (
3333
"context"
34+
"fmt"
3435
"os"
3536

3637
"github.com/containerd/errdefs"
37-
"github.com/pkg/errors"
3838

3939
"github.com/google/cadvisor/container/containerd/identifiers"
4040
)
@@ -83,10 +83,10 @@ func Namespace(ctx context.Context) (string, bool) {
8383
func NamespaceRequired(ctx context.Context) (string, error) {
8484
namespace, ok := Namespace(ctx)
8585
if !ok || namespace == "" {
86-
return "", errors.Wrapf(errdefs.ErrFailedPrecondition, "namespace is required")
86+
return "", fmt.Errorf("namespace is required: %w", errdefs.ErrFailedPrecondition)
8787
}
8888
if err := identifiers.Validate(namespace); err != nil {
89-
return "", errors.Wrap(err, "namespace validation")
89+
return "", fmt.Errorf("namespace validation: %w", err)
9090
}
9191
return namespace, nil
9292
}

container/containerd/pkg/dialer/dialer.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,9 @@ package dialer
3131

3232
import (
3333
"context"
34+
"fmt"
3435
"net"
3536
"time"
36-
37-
"github.com/pkg/errors"
3837
)
3938

4039
type dialResult struct {
@@ -87,6 +86,6 @@ func timeoutDialer(address string, timeout time.Duration) (net.Conn, error) {
8786
dr.c.Close()
8887
}
8988
}()
90-
return nil, errors.Errorf("dial %s: timeout", address)
89+
return nil, fmt.Errorf("dial %s: timeout", address)
9190
}
9291
}

container/podman/podman.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
dockercontainer "github.com/docker/docker/api/types/container"
2727
dockerimage "github.com/docker/docker/api/types/image"
2828
dockersystem "github.com/docker/docker/api/types/system"
29-
"github.com/pkg/errors"
3029

3130
"github.com/google/cadvisor/container/docker"
3231
"github.com/google/cadvisor/container/docker/utils"
@@ -53,7 +52,7 @@ func validateResponse(gotError error, response *http.Response) error {
5352
}
5453

5554
if gotError != nil {
56-
err = errors.Wrap(gotError, err.Error())
55+
err = fmt.Errorf("%s: %w", err.Error(), gotError)
5756
}
5857

5958
return err

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ require (
2525
github.com/opencontainers/cgroups v0.0.2
2626
github.com/opencontainers/runc v1.3.0
2727
github.com/opencontainers/runtime-spec v1.2.1
28-
github.com/pkg/errors v0.9.1
2928
github.com/prometheus/client_golang v1.22.0
3029
github.com/prometheus/client_model v0.6.2
3130
github.com/prometheus/common v0.64.0
@@ -71,6 +70,7 @@ require (
7170
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
7271
github.com/opencontainers/go-digest v1.0.0 // indirect
7372
github.com/opencontainers/image-spec v1.1.1 // indirect
73+
github.com/pkg/errors v0.9.1 // indirect
7474
github.com/pmezard/go-difflib v1.0.0 // indirect
7575
github.com/prometheus/procfs v0.16.1 // indirect
7676
github.com/sirupsen/logrus v1.9.3 // indirect

0 commit comments

Comments
 (0)