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

@@ -17,7 +17,7 @@
// Package errdefs defines the common errors used throughout containerd
// packages.
//
// Use with errors.Wrap and error.Wrapf to add context to an error.
// Use with fmt.Errorf to add context to an error.
//
// To detect an error class, use the IsXXX functions to tell whether an error
// is of a certain type.
@@ -28,8 +28,7 @@ package errdefs
import (
"context"
"github.com/pkg/errors"
"errors"
)
// Definitions of common error types used throughout containerd. All containerd

View File

@@ -18,9 +18,9 @@ package errdefs
import (
"context"
"fmt"
"strings"
"github.com/pkg/errors"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
@@ -68,9 +68,9 @@ func ToGRPC(err error) error {
// ToGRPCf maps the error to grpc error codes, assembling the formatting string
// and combining it with the target error string.
//
// This is equivalent to errors.ToGRPC(errors.Wrapf(err, format, args...))
// This is equivalent to errdefs.ToGRPC(fmt.Errorf("%s: %w", fmt.Sprintf(format, args...), err))
func ToGRPCf(err error, format string, args ...interface{}) error {
return ToGRPC(errors.Wrapf(err, format, args...))
return ToGRPC(fmt.Errorf("%s: %w", fmt.Sprintf(format, args...), err))
}
// FromGRPC returns the underlying error from a grpc service based on the grpc error code
@@ -104,9 +104,9 @@ func FromGRPC(err error) error {
msg := rebaseMessage(cls, err)
if msg != "" {
err = errors.Wrap(cls, msg)
err = fmt.Errorf("%s: %w", msg, cls)
} else {
err = errors.WithStack(cls)
err = cls
}
return err

View File

@@ -18,12 +18,12 @@ package errdefs
import (
"context"
"errors"
"fmt"
"testing"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/pkg/errors"
)
func TestGRPCRoundTrip(t *testing.T) {
@@ -43,7 +43,7 @@ func TestGRPCRoundTrip(t *testing.T) {
cause: ErrNotFound,
},
{
input: errors.Wrapf(ErrFailedPrecondition, "test test test"),
input: fmt.Errorf("test test test: %w", ErrFailedPrecondition),
cause: ErrFailedPrecondition,
str: "test test test: failed precondition",
},
@@ -63,7 +63,7 @@ func TestGRPCRoundTrip(t *testing.T) {
str: "context canceled",
},
{
input: errors.Wrapf(context.Canceled, "this is a test cancel"),
input: fmt.Errorf("this is a test cancel: %w", context.Canceled),
cause: context.Canceled,
str: "this is a test cancel: context canceled",
},
@@ -73,7 +73,7 @@ func TestGRPCRoundTrip(t *testing.T) {
str: "context deadline exceeded",
},
{
input: errors.Wrapf(context.DeadlineExceeded, "this is a test deadline exceeded"),
input: fmt.Errorf("this is a test deadline exceeded: %w", context.DeadlineExceeded),
cause: context.DeadlineExceeded,
str: "this is a test deadline exceeded: context deadline exceeded",
},
@@ -85,9 +85,6 @@ func TestGRPCRoundTrip(t *testing.T) {
ferr := FromGRPC(gerr)
t.Logf("recovered: %v", ferr)
if errors.Cause(ferr) != testcase.cause {
t.Fatalf("unexpected cause: %v != %v", errors.Cause(ferr), testcase.cause)
}
if !errors.Is(ferr, testcase.cause) {
t.Fatalf("unexpected cause: !errors.Is(%v, %v)", ferr, testcase.cause)
}