From afbb9d51f16413bc2e12aff6e8cb5133329f5c22 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Thu, 29 Apr 2021 18:32:01 +0300 Subject: [PATCH] *: drop github.com/pkg/errors dependency Use standard error wrapping/unwrapping instead. The conversion is mostly straightforward, but see grpc/grpc-go#2934 for GRPC `status.FromError`, it doesn't currently support unwrapping/errors.As(), so we're unwrapping manually here. --- connections/pool.go | 5 +++-- downloader/download.go | 15 ++++++++++----- go.mod | 1 - tokens/bearer-token.go | 7 ++++--- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/connections/pool.go b/connections/pool.go index 8e1db20..15e22f4 100644 --- a/connections/pool.go +++ b/connections/pool.go @@ -3,13 +3,14 @@ package connections import ( "context" "crypto/ecdsa" + "errors" + "fmt" "math/rand" "sync" "time" "github.com/nspcc-dev/neofs-api-go/pkg/client" "github.com/nspcc-dev/neofs-api-go/pkg/token" - "github.com/pkg/errors" "google.golang.org/grpc" ) @@ -91,7 +92,7 @@ func new(ctx context.Context, options *PoolBuilderOptions) (Pool, error) { if epi, err := c.EndpointInfo(ctx); err == nil { address = epi.NodeInfo().Address() } - return nil, errors.Wrapf(err, "failed to create neofs session token for client %s", address) + return nil, fmt.Errorf("failed to create neofs session token for client %s: %w", address, err) } clientPacks[i] = &clientPack{client: c, sessionToken: st, healthy: true} } diff --git a/downloader/download.go b/downloader/download.go index 8d447b0..f52861b 100644 --- a/downloader/download.go +++ b/downloader/download.go @@ -2,6 +2,8 @@ package downloader import ( "context" + "errors" + "fmt" "io" "net/http" "path" @@ -14,7 +16,6 @@ import ( "github.com/nspcc-dev/neofs-api-go/pkg/object" "github.com/nspcc-dev/neofs-http-gate/neofs" "github.com/nspcc-dev/neofs-http-gate/tokens" - "github.com/pkg/errors" "github.com/valyala/fasthttp" "go.uber.org/zap" "google.golang.org/grpc/codes" @@ -84,10 +85,14 @@ func (r *request) receiveFile(options *neofs.GetOptions) { zap.Error(err), ) var ( - msg = errors.Wrap(err, "could not receive object").Error() - code = fasthttp.StatusBadRequest + msg = fmt.Sprintf("could not receive object: %v", err) + code = fasthttp.StatusBadRequest + cause = err ) - if st, ok := status.FromError(errors.Cause(err)); ok && st != nil { + for unwrap := errors.Unwrap(err); unwrap != nil; unwrap = errors.Unwrap(cause) { + cause = unwrap + } + if st, ok := status.FromError(cause); ok && st != nil { if st.Code() == codes.NotFound { code = fasthttp.StatusNotFound } @@ -144,7 +149,7 @@ func New(ctx context.Context, log *zap.Logger, plant neofs.ClientPlant) (*Downlo var err error d := &Downloader{log: log, plant: plant} if err != nil { - return nil, errors.Wrap(err, "failed to get neofs client's reusable artifacts") + return nil, fmt.Errorf("failed to get neofs client's reusable artifacts: %w", err) } return d, nil } diff --git a/go.mod b/go.mod index d2efa4f..8c2a78b 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,6 @@ require ( github.com/nspcc-dev/neo-go v0.94.0 // indirect github.com/nspcc-dev/neofs-api-go v1.25.0 github.com/nspcc-dev/neofs-crypto v0.3.0 - github.com/pkg/errors v0.9.1 github.com/prometheus/client_golang v1.9.0 github.com/prometheus/common v0.15.0 github.com/spf13/pflag v1.0.5 diff --git a/tokens/bearer-token.go b/tokens/bearer-token.go index 337c604..86e21f0 100644 --- a/tokens/bearer-token.go +++ b/tokens/bearer-token.go @@ -4,9 +4,10 @@ import ( "bytes" "context" "encoding/base64" + "errors" + "fmt" "github.com/nspcc-dev/neofs-api-go/pkg/token" - "github.com/pkg/errors" "github.com/valyala/fasthttp" ) @@ -77,10 +78,10 @@ func fetchBearerToken(ctx *fasthttp.RequestCtx) (*token.BearerToken, error) { if buf = parse(&ctx.Request.Header); buf == nil { continue } else if data, err := base64.StdEncoding.DecodeString(string(buf)); err != nil { - lastErr = errors.Wrap(err, "could not fetch marshaled from base64") + lastErr = fmt.Errorf("can't base64-decode bearer token: %w", err) continue } else if err = tkn.Unmarshal(data); err != nil { - lastErr = errors.Wrap(err, "could not unmarshal bearer token") + lastErr = fmt.Errorf("can't unmarshal bearer token: %w", err) continue } else if tkn == nil { continue