feat: replace github.com/pkg/errors to errors

Signed-off-by: haoyun <yun.hao@daocloud.io>
Co-authored-by: zounengren <zouyee1989@gmail.com>
This commit is contained in:
haoyun
2022-01-07 10:19:31 +08:00
parent 3ccd43c8f6
commit bbe46b8c43
299 changed files with 1896 additions and 1874 deletions

View File

@@ -32,7 +32,6 @@ import (
"github.com/containerd/containerd/mount"
"github.com/containerd/containerd/namespaces"
"github.com/containerd/containerd/snapshots"
"github.com/pkg/errors"
bolt "go.etcd.io/bbolt"
)
@@ -88,7 +87,7 @@ func (s *snapshotter) resolveKey(ctx context.Context, key string) (string, error
if err := view(ctx, s.db, func(tx *bolt.Tx) error {
id = getKey(tx, ns, s.name, key)
if id == "" {
return errors.Wrapf(errdefs.ErrNotFound, "snapshot %v does not exist", key)
return fmt.Errorf("snapshot %v does not exist: %w", key, errdefs.ErrNotFound)
}
return nil
}); err != nil {
@@ -113,18 +112,18 @@ func (s *snapshotter) Stat(ctx context.Context, key string) (snapshots.Info, err
if err := view(ctx, s.db, func(tx *bolt.Tx) error {
bkt := getSnapshotterBucket(tx, ns, s.name)
if bkt == nil {
return errors.Wrapf(errdefs.ErrNotFound, "snapshot %v does not exist", key)
return fmt.Errorf("snapshot %v does not exist: %w", key, errdefs.ErrNotFound)
}
sbkt := bkt.Bucket([]byte(key))
if sbkt == nil {
return errors.Wrapf(errdefs.ErrNotFound, "snapshot %v does not exist", key)
return fmt.Errorf("snapshot %v does not exist: %w", key, errdefs.ErrNotFound)
}
local.Labels, err = boltutil.ReadLabels(sbkt)
if err != nil {
return errors.Wrap(err, "failed to read labels")
return fmt.Errorf("failed to read labels: %w", err)
}
if err := boltutil.ReadTimestamps(sbkt, &local.Created, &local.Updated); err != nil {
return errors.Wrap(err, "failed to read timestamps")
return fmt.Errorf("failed to read timestamps: %w", err)
}
bkey = string(sbkt.Get(bucketKeyName))
local.Parent = string(sbkt.Get(bucketKeyParent))
@@ -152,7 +151,7 @@ func (s *snapshotter) Update(ctx context.Context, info snapshots.Info, fieldpath
}
if info.Name == "" {
return snapshots.Info{}, errors.Wrap(errdefs.ErrInvalidArgument, "")
return snapshots.Info{}, errdefs.ErrInvalidArgument
}
var (
@@ -165,19 +164,19 @@ func (s *snapshotter) Update(ctx context.Context, info snapshots.Info, fieldpath
if err := update(ctx, s.db, func(tx *bolt.Tx) error {
bkt := getSnapshotterBucket(tx, ns, s.name)
if bkt == nil {
return errors.Wrapf(errdefs.ErrNotFound, "snapshot %v does not exist", info.Name)
return fmt.Errorf("snapshot %v does not exist: %w", info.Name, errdefs.ErrNotFound)
}
sbkt := bkt.Bucket([]byte(info.Name))
if sbkt == nil {
return errors.Wrapf(errdefs.ErrNotFound, "snapshot %v does not exist", info.Name)
return fmt.Errorf("snapshot %v does not exist: %w", info.Name, errdefs.ErrNotFound)
}
local.Labels, err = boltutil.ReadLabels(sbkt)
if err != nil {
return errors.Wrap(err, "failed to read labels")
return fmt.Errorf("failed to read labels: %w", err)
}
if err := boltutil.ReadTimestamps(sbkt, &local.Created, &local.Updated); err != nil {
return errors.Wrap(err, "failed to read timestamps")
return fmt.Errorf("failed to read timestamps: %w", err)
}
// Handle field updates
@@ -197,7 +196,7 @@ func (s *snapshotter) Update(ctx context.Context, info snapshots.Info, fieldpath
case "labels":
local.Labels = info.Labels
default:
return errors.Wrapf(errdefs.ErrInvalidArgument, "cannot update %q field on snapshot %q", path, info.Name)
return fmt.Errorf("cannot update %q field on snapshot %q: %w", path, info.Name, errdefs.ErrInvalidArgument)
}
}
} else {
@@ -209,10 +208,10 @@ func (s *snapshotter) Update(ctx context.Context, info snapshots.Info, fieldpath
local.Updated = time.Now().UTC()
if err := boltutil.WriteTimestamps(sbkt, local.Created, local.Updated); err != nil {
return errors.Wrap(err, "failed to read timestamps")
return fmt.Errorf("failed to read timestamps: %w", err)
}
if err := boltutil.WriteLabels(sbkt, local.Labels); err != nil {
return errors.Wrap(err, "failed to read labels")
return fmt.Errorf("failed to read labels: %w", err)
}
bkey = string(sbkt.Get(bucketKeyName))
local.Parent = string(sbkt.Get(bucketKeyParent))
@@ -319,18 +318,18 @@ func (s *snapshotter) createSnapshot(ctx context.Context, key, parent string, re
// Check if target exists, if so, return already exists
if target != "" {
if tbkt := bkt.Bucket([]byte(target)); tbkt != nil {
return errors.Wrapf(errdefs.ErrAlreadyExists, "target snapshot %q", target)
return fmt.Errorf("target snapshot %q: %w", target, errdefs.ErrAlreadyExists)
}
}
if bbkt := bkt.Bucket([]byte(key)); bbkt != nil {
return errors.Wrapf(errdefs.ErrAlreadyExists, "snapshot %q", key)
return fmt.Errorf("snapshot %q: %w", key, errdefs.ErrAlreadyExists)
}
if parent != "" {
pbkt := bkt.Bucket([]byte(parent))
if pbkt == nil {
return errors.Wrapf(errdefs.ErrNotFound, "parent snapshot %v does not exist", parent)
return fmt.Errorf("parent snapshot %v does not exist: %w", parent, errdefs.ErrNotFound)
}
bparent = string(pbkt.Get(bucketKeyName))
}
@@ -378,11 +377,11 @@ func (s *snapshotter) createSnapshot(ctx context.Context, key, parent string, re
return nil
}, filter); err != nil {
return nil, errors.Wrap(err, "failed walking backend snapshots")
return nil, fmt.Errorf("failed walking backend snapshots: %w", err)
}
if tinfo == nil {
return nil, errors.Wrapf(errdefs.ErrNotFound, "target snapshot %q in backend", target)
return nil, fmt.Errorf("target snapshot %q in backend: %w", target, errdefs.ErrNotFound)
}
key = target
@@ -401,12 +400,12 @@ func (s *snapshotter) createSnapshot(ctx context.Context, key, parent string, re
}
// Propagate this error after the final update
rerr = errors.Wrapf(errdefs.ErrAlreadyExists, "target snapshot %q from snapshotter", target)
rerr = fmt.Errorf("target snapshot %q from snapshotter: %w", target, errdefs.ErrAlreadyExists)
} else {
// This condition is unexpected as the key provided is expected
// to be new and unique, return as unknown response from backend
// to avoid confusing callers handling already exists.
return nil, errors.Wrapf(errdefs.ErrUnknown, "unexpected error from snapshotter: %v", err)
return nil, fmt.Errorf("unexpected error from snapshotter: %v: %w", err, errdefs.ErrUnknown)
}
} else if err != nil {
return nil, err
@@ -420,7 +419,7 @@ func (s *snapshotter) createSnapshot(ctx context.Context, key, parent string, re
if txerr := update(ctx, s.db, func(tx *bolt.Tx) error {
bkt := getSnapshotterBucket(tx, ns, s.name)
if bkt == nil {
return errors.Wrapf(errdefs.ErrNotFound, "can not find snapshotter %q", s.name)
return fmt.Errorf("can not find snapshotter %q: %w", s.name, errdefs.ErrNotFound)
}
if err := addSnapshotLease(ctx, tx, s.name, key); err != nil {
@@ -433,7 +432,7 @@ func (s *snapshotter) createSnapshot(ctx context.Context, key, parent string, re
return err
}
if rerr == nil {
rerr = errors.Wrapf(errdefs.ErrAlreadyExists, "snapshot %q", key)
rerr = fmt.Errorf("snapshot %q: %w", key, errdefs.ErrAlreadyExists)
}
return nil
}
@@ -441,7 +440,7 @@ func (s *snapshotter) createSnapshot(ctx context.Context, key, parent string, re
if parent != "" {
pbkt := bkt.Bucket([]byte(parent))
if pbkt == nil {
return errors.Wrapf(errdefs.ErrNotFound, "parent snapshot %v does not exist", parent)
return fmt.Errorf("parent snapshot %v does not exist: %w", parent, errdefs.ErrNotFound)
}
// Ensure the backend's parent matches the metadata store's parent
@@ -451,7 +450,7 @@ func (s *snapshotter) createSnapshot(ctx context.Context, key, parent string, re
// uniqueness of the reference relationships, the metadata store
// can only error out to prevent inconsistent data.
if bparent != string(pbkt.Get(bucketKeyName)) {
return errors.Wrapf(errdefs.ErrInvalidArgument, "mismatched parent %s from target %s", parent, target)
return fmt.Errorf("mismatched parent %s from target %s: %w", parent, target, errdefs.ErrInvalidArgument)
}
cbkt, err := pbkt.CreateBucketIfNotExists(bucketKeyChildren)
@@ -516,14 +515,14 @@ func (s *snapshotter) Commit(ctx context.Context, name, key string, opts ...snap
if err := update(ctx, s.db, func(tx *bolt.Tx) error {
bkt := getSnapshotterBucket(tx, ns, s.name)
if bkt == nil {
return errors.Wrapf(errdefs.ErrNotFound,
"can not find snapshotter %q", s.name)
return fmt.Errorf("can not find snapshotter %q: %w",
s.name, errdefs.ErrNotFound)
}
bbkt, err := bkt.CreateBucket([]byte(name))
if err != nil {
if err == bolt.ErrBucketExists {
err = errors.Wrapf(errdefs.ErrAlreadyExists, "snapshot %q", name)
err = fmt.Errorf("snapshot %q: %w", name, errdefs.ErrAlreadyExists)
}
return err
}
@@ -533,7 +532,7 @@ func (s *snapshotter) Commit(ctx context.Context, name, key string, opts ...snap
obkt := bkt.Bucket([]byte(key))
if obkt == nil {
return errors.Wrapf(errdefs.ErrNotFound, "snapshot %v does not exist", key)
return fmt.Errorf("snapshot %v does not exist: %w", key, errdefs.ErrNotFound)
}
bkey := string(obkt.Get(bucketKeyName))
@@ -553,7 +552,7 @@ func (s *snapshotter) Commit(ctx context.Context, name, key string, opts ...snap
if len(parent) > 0 {
pbkt := bkt.Bucket(parent)
if pbkt == nil {
return errors.Wrapf(errdefs.ErrNotFound, "parent snapshot %v does not exist", string(parent))
return fmt.Errorf("parent snapshot %v does not exist: %w", string(parent), errdefs.ErrNotFound)
}
cbkt, err := pbkt.CreateBucketIfNotExists(bucketKeyChildren)
@@ -639,13 +638,13 @@ func (s *snapshotter) Remove(ctx context.Context, key string) error {
sbkt = bkt.Bucket([]byte(key))
}
if sbkt == nil {
return errors.Wrapf(errdefs.ErrNotFound, "snapshot %v does not exist", key)
return fmt.Errorf("snapshot %v does not exist: %w", key, errdefs.ErrNotFound)
}
cbkt := sbkt.Bucket(bucketKeyChildren)
if cbkt != nil {
if child, _ := cbkt.Cursor().First(); child != nil {
return errors.Wrap(errdefs.ErrFailedPrecondition, "cannot remove snapshot with child")
return fmt.Errorf("cannot remove snapshot with child: %w", errdefs.ErrFailedPrecondition)
}
}
@@ -653,12 +652,12 @@ func (s *snapshotter) Remove(ctx context.Context, key string) error {
if len(parent) > 0 {
pbkt := bkt.Bucket(parent)
if pbkt == nil {
return errors.Wrapf(errdefs.ErrNotFound, "parent snapshot %v does not exist", string(parent))
return fmt.Errorf("parent snapshot %v does not exist: %w", string(parent), errdefs.ErrNotFound)
}
cbkt := pbkt.Bucket(bucketKeyChildren)
if cbkt != nil {
if err := cbkt.Delete([]byte(key)); err != nil {
return errors.Wrap(err, "failed to remove child link")
return fmt.Errorf("failed to remove child link: %w", err)
}
}
}
@@ -784,7 +783,7 @@ func (s *snapshotter) Walk(ctx context.Context, fn snapshots.WalkFunc, fs ...str
func validateSnapshot(info *snapshots.Info) error {
for k, v := range info.Labels {
if err := labels.Validate(k, v); err != nil {
return errors.Wrapf(err, "info.Labels")
return fmt.Errorf("info.Labels: %w", err)
}
}