feat: remove github/pkg/errors package (#1416)

Signed-off-by: Gaius <gaius.qi@gmail.com>
This commit is contained in:
Gaius 2022-06-28 22:38:11 +08:00
parent 1d7cd9dd12
commit ad36eb64a7
No known key found for this signature in database
GPG Key ID: 8B4E5D1290FA2FFB
60 changed files with 183 additions and 193 deletions

View File

@ -18,10 +18,11 @@ package clientutil
import (
"encoding/json"
"errors"
"fmt"
"time"
"github.com/docker/go-units"
"github.com/pkg/errors"
"golang.org/x/time/rate"
"gopkg.in/yaml.v3"
)
@ -58,7 +59,7 @@ func (r *RateLimit) unmarshal(unmarshal func(in []byte, out interface{}) (err er
case string:
limit, err := units.RAMInBytes(value)
if err != nil {
return errors.WithMessage(err, "invalid rate limit")
return fmt.Errorf("invalid rate limit: %w", err)
}
r.Limit = rate.Limit(limit)
return nil

View File

@ -17,10 +17,9 @@
package config
import (
"errors"
"fmt"
"strings"
"github.com/pkg/errors"
)
var DefaultSupernodesValue = &SupernodesValue{

View File

@ -18,6 +18,7 @@ package config
import (
"encoding/json"
"errors"
"fmt"
"os"
"path"
@ -25,7 +26,6 @@ import (
"syscall"
"time"
"github.com/pkg/errors"
"golang.org/x/time/rate"
"d7y.io/dragonfly/v2/cmd/dependency/base"
@ -82,14 +82,14 @@ func validateCacheStat(cfg *CacheOption) error {
func validateCacheImport(cfg *CacheOption) error {
if err := cfg.checkInput(); err != nil {
return errors.Wrapf(dferrors.ErrInvalidArgument, "input path: %v", err)
return fmt.Errorf("input path %s: %w", err.Error(), dferrors.ErrInvalidArgument)
}
return nil
}
func ValidateCacheExport(cfg *CacheOption) error {
if err := cfg.checkOutput(); err != nil {
return errors.Wrapf(dferrors.ErrInvalidArgument, "output: %v", err)
return fmt.Errorf("output %s: %w", err.Error(), dferrors.ErrInvalidArgument)
}
return nil
}
@ -101,13 +101,13 @@ func ValidateCacheDelete(cfg *CacheOption) error {
func (cfg *CacheOption) Validate(cmd string) error {
// Some common validations
if cfg == nil {
return errors.Wrap(dferrors.ErrInvalidArgument, "runtime config")
return fmt.Errorf("runtime config: %w", dferrors.ErrInvalidArgument)
}
if cfg.Cid == "" {
return errors.Wrap(dferrors.ErrInvalidArgument, "missing Cid")
return fmt.Errorf("missing Cid: %w", dferrors.ErrInvalidArgument)
}
if strings.IsBlank(cfg.Cid) {
return errors.Wrap(dferrors.ErrInvalidArgument, "Cid are all blanks")
return fmt.Errorf("Cid are all blanks: %w", dferrors.ErrInvalidArgument)
}
// cmd specific validations
@ -121,7 +121,7 @@ func (cfg *CacheOption) Validate(cmd string) error {
case CmdDelete:
return ValidateCacheDelete(cfg)
default:
return errors.Wrapf(dferrors.ErrInvalidArgument, "unknown cache subcommand: %s", cmd)
return fmt.Errorf("unknown cache subcommand %s: %w", cmd, dferrors.ErrInvalidArgument)
}
}
@ -135,11 +135,11 @@ func convertCacheImport(cfg *CacheOption, args []string) error {
cfg.Path = args[0]
}
if cfg.Path == "" {
return errors.Wrap(dferrors.ErrInvalidArgument, "missing input file")
return fmt.Errorf("missing input file: %w", dferrors.ErrInvalidArgument)
}
if cfg.Path, err = filepath.Abs(cfg.Path); err != nil {
return errors.Wrapf(err, "get absulate path for %s", cfg.Path)
return fmt.Errorf("get absulate path for %s: %w", cfg.Path, err)
}
return nil
}
@ -150,11 +150,11 @@ func ConvertCacheExport(cfg *CacheOption, args []string) error {
cfg.Output = args[0]
}
if cfg.Output == "" {
return errors.Wrap(dferrors.ErrInvalidArgument, "missing output file")
return fmt.Errorf("missing output file: %w", dferrors.ErrInvalidArgument)
}
if cfg.Output, err = filepath.Abs(cfg.Output); err != nil {
return errors.Wrapf(err, "get absulate path for %s", cfg.Output)
return fmt.Errorf("get absulate path for %s: %w", cfg.Output, err)
}
return nil
}
@ -165,7 +165,7 @@ func ConvertCacheDelete(cfg *CacheOption, args []string) error {
func (cfg *CacheOption) Convert(cmd string, args []string) error {
if cfg == nil {
return errors.Wrap(dferrors.ErrInvalidArgument, "runtime config")
return fmt.Errorf("runtime config: %w", dferrors.ErrInvalidArgument)
}
switch cmd {
@ -178,7 +178,7 @@ func (cfg *CacheOption) Convert(cmd string, args []string) error {
case CmdDelete:
return ConvertCacheDelete(cfg, args)
default:
return errors.Wrapf(dferrors.ErrInvalidArgument, "unknown cache subcommand: %s", cmd)
return fmt.Errorf("unknown cache subcommand %s: %w", cmd, dferrors.ErrInvalidArgument)
}
}
@ -190,13 +190,13 @@ func (cfg *CacheOption) String() string {
func (cfg *CacheOption) checkInput() error {
stat, err := os.Stat(cfg.Path)
if err != nil {
return errors.Wrapf(err, "stat input path %q", cfg.Path)
return fmt.Errorf("stat input path %q: %w", cfg.Path, err)
}
if stat.IsDir() {
return fmt.Errorf("path[%q] is directory but requires file path", cfg.Path)
}
if err := syscall.Access(cfg.Path, syscall.O_RDONLY); err != nil {
return errors.Wrapf(err, "access %q", cfg.Path)
return fmt.Errorf("access %q: %w", cfg.Path, err)
}
return nil
}

View File

@ -28,8 +28,6 @@ import (
"syscall"
"time"
"github.com/pkg/errors"
"d7y.io/dragonfly/v2/client/clientutil"
"d7y.io/dragonfly/v2/cmd/dependency/base"
"d7y.io/dragonfly/v2/internal/dferrors"
@ -140,11 +138,11 @@ func NewDfgetConfig() *ClientOption {
func (cfg *ClientOption) Validate() error {
if cfg == nil {
return errors.Wrap(dferrors.ErrInvalidArgument, "runtime config")
return fmt.Errorf("runtime config: %w", dferrors.ErrInvalidArgument)
}
if !url.IsValid(cfg.URL) {
return errors.Wrapf(dferrors.ErrInvalidArgument, "url: %v", cfg.URL)
return fmt.Errorf("url %s: %w", cfg.URL, dferrors.ErrInvalidArgument)
}
if _, err := regexp.Compile(cfg.RecursiveAcceptRegex); err != nil {
@ -156,15 +154,15 @@ func (cfg *ClientOption) Validate() error {
}
if err := cfg.checkOutput(); err != nil {
return errors.Wrapf(dferrors.ErrInvalidArgument, "output: %v", err)
return fmt.Errorf("output %s: %w", err.Error(), dferrors.ErrInvalidArgument)
}
if err := cfg.checkHeader(); err != nil {
return errors.Wrapf(dferrors.ErrInvalidHeader, "output: %v", err)
return fmt.Errorf("output %s: %w", err.Error(), dferrors.ErrInvalidHeader)
}
if int64(cfg.RateLimit.Limit) < DefaultMinRate.ToNumber() {
return errors.Wrapf(dferrors.ErrInvalidArgument, "rate limit must be greater than %s", DefaultMinRate.String())
return fmt.Errorf("rate limit must be greater than %s: %w", DefaultMinRate.String(), dferrors.ErrInvalidArgument)
}
return nil

View File

@ -17,13 +17,13 @@
package config
import (
"errors"
"os"
"path/filepath"
"testing"
"time"
"github.com/golang/mock/gomock"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

View File

@ -17,13 +17,13 @@
package config
import (
"errors"
"fmt"
"strconv"
"strings"
"time"
"github.com/docker/go-units"
"github.com/pkg/errors"
"golang.org/x/time/rate"
"d7y.io/dragonfly/v2/client/clientutil"

View File

@ -21,6 +21,7 @@ import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"fmt"
"net"
"net/url"
@ -30,7 +31,6 @@ import (
"strings"
"time"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
"d7y.io/dragonfly/v2/client/clientutil"
@ -132,11 +132,11 @@ func (p *DaemonOption) Validate() error {
}
if int64(p.Download.TotalRateLimit.Limit) < DefaultMinRate.ToNumber() {
return errors.Errorf("rate limit must be greater than %s", DefaultMinRate.String())
return fmt.Errorf("rate limit must be greater than %s", DefaultMinRate.String())
}
if int64(p.Upload.RateLimit.Limit) < DefaultMinRate.ToNumber() {
return errors.Errorf("rate limit must be greater than %s", DefaultMinRate.String())
return fmt.Errorf("rate limit must be greater than %s", DefaultMinRate.String())
}
if p.ObjectStorage.Enable {
@ -723,10 +723,10 @@ func certPoolFromFiles(files ...string) (*x509.CertPool, error) {
for _, f := range files {
cert, err := os.ReadFile(f)
if err != nil {
return nil, errors.Wrapf(err, "read cert file %s", f)
return nil, fmt.Errorf("read cert file %s: %w", f, err)
}
if !roots.AppendCertsFromPEM(cert) {
return nil, errors.Errorf("invalid cert: %s", f)
return nil, fmt.Errorf("invalid cert: %s", f)
}
}
return roots, nil
@ -745,7 +745,7 @@ type ProxyRule struct {
func NewProxyRule(regx string, useHTTPS bool, direct bool, redirect string) (*ProxyRule, error) {
exp, err := NewRegexp(regx)
if err != nil {
return nil, errors.Wrap(err, "invalid regexp")
return nil, fmt.Errorf("invalid regexp: %w", err)
}
return &ProxyRule{

View File

@ -20,6 +20,7 @@ import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"net"
"net/http"
@ -30,7 +31,6 @@ import (
"time"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"golang.org/x/sync/errgroup"
"golang.org/x/time/rate"
@ -155,7 +155,7 @@ func New(opt *config.DaemonOption, d dfpath.Dfpath) (Daemon, error) {
}
sched, err := schedulerclient.GetClientByAddr(addrs, opts...)
if err != nil {
return nil, errors.Wrap(err, "failed to get schedulers")
return nil, fmt.Errorf("failed to get schedulers: %w", err)
}
// Storage.Option.DataPath is same with Daemon DataDir

View File

@ -19,6 +19,7 @@ package objectstorage
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"math"
@ -35,7 +36,6 @@ import (
"github.com/gin-gonic/gin"
"github.com/go-http-utils/headers"
ginprometheus "github.com/mcuadros/go-gin-prometheus"
"github.com/pkg/errors"
"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
"d7y.io/dragonfly/v2/client/clientutil"

View File

@ -19,13 +19,13 @@ package peer
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"runtime/debug"
"sync"
"time"
"github.com/pkg/errors"
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
"go.opentelemetry.io/otel/trace"
"go.uber.org/atomic"
@ -317,7 +317,7 @@ func (pt *peerTaskConductor) register() error {
Content: piece.PieceContent,
}
} else {
err = errors.Errorf("scheduler return tiny piece but can not parse piece content")
err = errors.New("scheduler return tiny piece but can not parse piece content")
// when peer register failed, some actions need to do with peerPacketStream
pt.peerPacketStream = &dummyPeerPacketStream{}
pt.span.RecordError(err)

View File

@ -18,13 +18,13 @@ package peer
import (
"context"
"errors"
"fmt"
"io"
"sync"
"time"
"github.com/go-http-utils/headers"
"github.com/pkg/errors"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace"
"golang.org/x/time/rate"

View File

@ -18,12 +18,12 @@ package peer
import (
"context"
"errors"
"fmt"
"io"
"sync"
"time"
"github.com/pkg/errors"
"go.opentelemetry.io/otel/trace"
"go.uber.org/atomic"
"google.golang.org/grpc/codes"

View File

@ -18,6 +18,7 @@ package peer
import (
"context"
"errors"
"fmt"
"io"
"net"
@ -26,7 +27,6 @@ import (
"os"
"time"
"github.com/pkg/errors"
"golang.org/x/time/rate"
"d7y.io/dragonfly/v2/client/clientutil"

View File

@ -19,6 +19,7 @@ package proxy
import (
"crypto/tls"
"encoding/base64"
"errors"
"fmt"
"io"
"net"
@ -31,7 +32,6 @@ import (
"github.com/go-http-utils/headers"
"github.com/golang/groupcache/lru"
"github.com/pkg/errors"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"

View File

@ -27,7 +27,6 @@ import (
"os"
"reflect"
"github.com/pkg/errors"
"github.com/spf13/viper"
"d7y.io/dragonfly/v2/client/config"
@ -101,7 +100,7 @@ func NewProxyManager(peerHost *scheduler.PeerHost, peerTaskManager peer.TaskMana
if hijackHTTPS.Cert != "" && hijackHTTPS.Key != "" {
cert, err := certFromFile(hijackHTTPS.Cert, hijackHTTPS.Key)
if err != nil {
return nil, errors.Wrap(err, "cert from file")
return nil, fmt.Errorf("cert from file: %w", err)
}
if cert.Leaf != nil && cert.Leaf.IsCA {
logger.Debugf("hijack https request with CA <%s>", cert.Leaf.Subject.CommonName)
@ -112,7 +111,7 @@ func NewProxyManager(peerHost *scheduler.PeerHost, peerTaskManager peer.TaskMana
p, err := NewProxy(options...)
if err != nil {
return nil, errors.Wrap(err, "create proxy")
return nil, fmt.Errorf("create proxy: %w", err)
}
return &proxyManager{
@ -198,14 +197,14 @@ func certFromFile(certFile string, keyFile string) (*tls.Certificate, error) {
// cert.Certificate is a chain of one or more certificates, leaf first.
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return nil, errors.Wrap(err, "load cert")
return nil, fmt.Errorf("load cert: %w", err)
}
logger.Infof("use self-signed certificate (%s, %s) for https hijacking", certFile, keyFile)
// leaf is CA cert or server cert
leaf, err := x509.ParseCertificate(cert.Certificate[0])
if err != nil {
return nil, errors.Wrap(err, "load leaf cert")
return nil, fmt.Errorf("load leaf cert: %w", err)
}
cert.Leaf = leaf

View File

@ -18,6 +18,7 @@ package proxy
import (
"crypto/tls"
"errors"
"fmt"
"net"
"net/http"
@ -26,7 +27,6 @@ import (
"time"
"github.com/golang/groupcache/lru"
"github.com/pkg/errors"
logger "d7y.io/dragonfly/v2/internal/dflog"
)

View File

@ -18,6 +18,7 @@ package rpcserver
import (
"context"
"errors"
"fmt"
"io"
"math"
@ -25,7 +26,6 @@ import (
"os"
"time"
"github.com/pkg/errors"
"go.uber.org/atomic"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"

View File

@ -17,9 +17,8 @@
package storage
import (
"errors"
"os"
"github.com/pkg/errors"
)
const (

View File

@ -19,6 +19,7 @@ package storage
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
@ -31,7 +32,6 @@ import (
"time"
"github.com/docker/go-units"
"github.com/pkg/errors"
"github.com/shirou/gopsutil/v3/disk"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace"

View File

@ -18,13 +18,12 @@ package dfcache
import (
"context"
"errors"
"fmt"
"net/url"
"os"
"time"
"github.com/pkg/errors"
"d7y.io/dragonfly/v2/client/config"
"d7y.io/dragonfly/v2/internal/dferrors"
logger "d7y.io/dragonfly/v2/internal/dflog"
@ -52,7 +51,7 @@ func Stat(cfg *config.DfcacheConfig, client daemonclient.DaemonClient) error {
)
if err := cfg.Validate(config.CmdStat); err != nil {
return errors.Wrap(err, "validate stat option failed")
return fmt.Errorf("validate stat option failed: %w", err)
}
wLog := logger.With("Cid", cfg.Cid, "Tag", cfg.Tag)
@ -72,7 +71,7 @@ func Stat(cfg *config.DfcacheConfig, client daemonclient.DaemonClient) error {
<-ctx.Done()
if ctx.Err() == context.DeadlineExceeded {
return errors.Errorf("stat timeout(%s)", cfg.Timeout)
return fmt.Errorf("stat timeout(%s)", cfg.Timeout)
}
return statError
}
@ -118,7 +117,7 @@ func Import(cfg *config.DfcacheConfig, client daemonclient.DaemonClient) error {
)
if err := cfg.Validate(config.CmdImport); err != nil {
return errors.Wrap(err, "validate import option failed")
return fmt.Errorf("validate import option failed: %w", err)
}
wLog := logger.With("Cid", cfg.Cid, "Tag", cfg.Tag, "file", cfg.Path)
@ -138,7 +137,7 @@ func Import(cfg *config.DfcacheConfig, client daemonclient.DaemonClient) error {
<-ctx.Done()
if ctx.Err() == context.DeadlineExceeded {
return errors.Errorf("import timeout(%s)", cfg.Timeout)
return fmt.Errorf("import timeout(%s)", cfg.Timeout)
}
return importError
}
@ -180,7 +179,7 @@ func Export(cfg *config.DfcacheConfig, client daemonclient.DaemonClient) error {
)
if err := cfg.Validate(config.CmdExport); err != nil {
return errors.Wrap(err, "validate export option failed")
return fmt.Errorf("validate export option failed: %w", err)
}
wLog := logger.With("Cid", cfg.Cid, "Tag", cfg.Tag, "output", cfg.Output)
@ -200,7 +199,7 @@ func Export(cfg *config.DfcacheConfig, client daemonclient.DaemonClient) error {
<-ctx.Done()
if ctx.Err() == context.DeadlineExceeded {
return errors.Errorf("export timeout(%s)", cfg.Timeout)
return fmt.Errorf("export timeout(%s)", cfg.Timeout)
}
return exportError
}
@ -250,7 +249,7 @@ func Delete(cfg *config.DfcacheConfig, client daemonclient.DaemonClient) error {
)
if err := cfg.Validate(config.CmdDelete); err != nil {
return errors.Wrap(err, "validate delete option failed")
return fmt.Errorf("validate delete option failed: %w", err)
}
wLog := logger.With("Cid", cfg.Cid, "Tag", cfg.Tag)
@ -270,7 +269,7 @@ func Delete(cfg *config.DfcacheConfig, client daemonclient.DaemonClient) error {
<-ctx.Done()
if ctx.Err() == context.DeadlineExceeded {
return errors.Errorf("delete timeout(%s)", cfg.Timeout)
return fmt.Errorf("delete timeout(%s)", cfg.Timeout)
}
return deleteError
}

View File

@ -18,6 +18,7 @@ package dfget
import (
"context"
"errors"
"fmt"
"io"
"net/url"
@ -29,7 +30,6 @@ import (
"time"
"github.com/go-http-utils/headers"
"github.com/pkg/errors"
"github.com/schollz/progressbar/v3"
"d7y.io/dragonfly/v2/client/config"
@ -68,7 +68,7 @@ func Download(cfg *config.DfgetConfig, client daemonclient.DaemonClient) error {
<-ctx.Done()
if ctx.Err() == context.DeadlineExceeded {
return errors.Errorf("download timeout(%s)", cfg.Timeout)
return fmt.Errorf("download timeout(%s)", cfg.Timeout)
}
return downError
}
@ -185,13 +185,13 @@ func downloadFromSource(ctx context.Context, cfg *config.DfgetConfig, hdr map[st
}
if encoded != "" && encoded != d.Encoded {
return errors.Errorf("%s digest is not matched: real[%s] expected[%s]", d.Algorithm, encoded, d.Encoded)
return fmt.Errorf("%s digest is not matched: real[%s] expected[%s]", d.Algorithm, encoded, d.Encoded)
}
}
// change file owner
if err = os.Chown(target.Name(), basic.UserID, basic.UserGroup); err != nil {
return errors.Wrapf(err, "change file owner to uid[%d] gid[%d]", basic.UserID, basic.UserGroup)
return fmt.Errorf("change file owner to uid[%d] gid[%d]: %w", basic.UserID, basic.UserGroup, err)
}
if err = os.Rename(target.Name(), cfg.Output); err != nil {

View File

@ -31,7 +31,6 @@ import (
"github.com/go-echarts/statsview/viewer"
"github.com/mitchellh/mapstructure"
"github.com/phayes/freeport"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.opentelemetry.io/otel"
@ -72,7 +71,7 @@ func InitCobra(cmd *cobra.Command, useConfigFile bool, config interface{}) {
// Bind common flags
if err := viper.BindPFlags(flags); err != nil {
panic(errors.Wrap(err, "bind common flags to viper"))
panic(fmt.Errorf("bind common flags to viper: %w", err))
}
// Config for binding env
@ -178,12 +177,12 @@ func initConfig(useConfigFile bool, name string, config interface{}) {
}
}
if !ignoreErr {
panic(errors.Wrap(err, "viper read config"))
panic(fmt.Errorf("viper read config: %w", err))
}
}
}
if err := viper.Unmarshal(config, initDecoderConfig); err != nil {
panic(errors.Wrap(err, "unmarshal config to struct"))
panic(fmt.Errorf("unmarshal config to struct: %w", err))
}
}

View File

@ -21,7 +21,6 @@ import (
"io/fs"
"os"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
)
@ -67,7 +66,7 @@ func (g *genDocCommand) runDoc() error {
}
if !file.IsDir() {
return errors.Errorf("path %s is not dir, please check it", g.path)
return fmt.Errorf("path %s is not dir, please check it", g.path)
}
return doc.GenMarkdownTree(g.cmd.Parent(), g.path)

View File

@ -17,7 +17,8 @@
package cmd
import (
"github.com/pkg/errors"
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@ -50,7 +51,7 @@ func initExport() {
flags.StringVarP(&dfcacheConfig.Output, "output", "O", "", "export file path")
flags.BoolVarP(&dfcacheConfig.LocalOnly, "local", "l", false, "only export file from local cache")
if err := viper.BindPFlags(flags); err != nil {
panic(errors.Wrap(err, "bind cache export flags to viper"))
panic(fmt.Errorf("bind cache export flags to viper: %w", err))
}
}

View File

@ -17,7 +17,8 @@
package cmd
import (
"github.com/pkg/errors"
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@ -49,7 +50,7 @@ func initImport() {
flags := importCmd.Flags()
flags.StringVarP(&dfcacheConfig.Path, "input", "I", "", "import the given file into P2P network")
if err := viper.BindPFlags(flags); err != nil {
panic(errors.Wrap(err, "bind cache import flags to viper"))
panic(fmt.Errorf("bind cache import flags to viper: %w", err))
}
}

View File

@ -18,10 +18,10 @@ package cmd
import (
"context"
"errors"
"fmt"
"os"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/yaml.v3"
@ -88,7 +88,7 @@ func init() {
// Bind common flags
if err := viper.BindPFlags(flags); err != nil {
panic(errors.Wrap(err, "bind cache common flags to viper"))
panic(fmt.Errorf("bind cache common flags to viper: %w", err))
}
initStat()
@ -125,7 +125,7 @@ func runDfcacheSubcmd(cmdName string, args []string) error {
// Initialize logger
if err := logger.InitDfcache(dfcacheConfig.Console, d.LogDir()); err != nil {
return errors.Wrap(err, "init client dfcache logger")
return fmt.Errorf("init client dfcache logger: %w", err)
}
logger.Infof("Version:\n%s", version.Version())

View File

@ -17,7 +17,8 @@
package cmd
import (
"github.com/pkg/errors"
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@ -49,7 +50,7 @@ func initStat() {
flags := statCmd.Flags()
flags.BoolVarP(&dfcacheConfig.LocalOnly, "local", "l", dfcacheConfig.LocalOnly, "only check task exists locally, and don't check other peers in P2P network")
if err := viper.BindPFlags(flags); err != nil {
panic(errors.Wrap(err, "bind cache stat flags to viper"))
panic(fmt.Errorf("bind cache stat flags to viper: %w", err))
}
}

View File

@ -18,12 +18,13 @@ package cmd
import (
"context"
"errors"
"fmt"
"os"
"path"
"time"
"github.com/gofrs/flock"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/yaml.v3"
@ -62,7 +63,7 @@ it supports container engine, wget and other downloading tools through proxy fun
// Initialize logger
if err := logger.InitDaemon(cfg.Verbose, cfg.Console, d.LogDir()); err != nil {
return errors.Wrap(err, "init client daemon logger")
return fmt.Errorf("init client daemon logger: %w", err)
}
logger.RedirectStdoutAndStderr(cfg.Console, path.Join(d.LogDir(), "daemon"))

View File

@ -18,6 +18,7 @@ package cmd
import (
"context"
"errors"
"fmt"
"os"
"os/exec"
@ -26,7 +27,6 @@ import (
"time"
"github.com/gofrs/flock"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/yaml.v3"
@ -77,7 +77,7 @@ var rootCmd = &cobra.Command{
// Initialize logger
if err := logger.InitDfget(dfgetConfig.Verbose, dfgetConfig.Console, d.LogDir()); err != nil {
return errors.Wrap(err, "init client dfget logger")
return fmt.Errorf("init client dfget logger: %w", err)
}
// update plugin directory
@ -99,17 +99,18 @@ var rootCmd = &cobra.Command{
fmt.Printf("output path: %s\n", dfgetConfig.Output)
// do get file
var errInfo string
err = runDfget(d.DfgetLockPath(), d.DaemonSockPath())
if err != nil {
errInfo = fmt.Sprintf("error: %v", err)
}
msg := fmt.Sprintf("download success: %t cost: %d ms %s", err == nil, time.Since(start).Milliseconds(), errInfo)
msg := fmt.Sprintf("download success: %t cost: %d ms error: %s", false, time.Since(start).Milliseconds(), err.Error())
logger.With("url", dfgetConfig.URL).Info(msg)
fmt.Println(msg)
return fmt.Errorf("download url %s: %w", dfgetConfig.URL, err)
}
return errors.Wrapf(err, "download url: %s", dfgetConfig.URL)
msg := fmt.Sprintf("download success: %t cost: %d ms", true, time.Since(start).Milliseconds())
logger.With("url", dfgetConfig.URL).Info(msg)
fmt.Println(msg)
return nil
},
}
@ -190,7 +191,7 @@ func init() {
// Bind cmd flags
if err := viper.BindPFlags(flagSet); err != nil {
panic(errors.Wrap(err, "bind dfget flags to viper"))
panic(fmt.Errorf("bind dfget flags to viper: %w", err))
}
}

View File

@ -17,10 +17,10 @@
package cmd
import (
"fmt"
"os"
"path"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
@ -54,7 +54,7 @@ for managing schedulers and seed peers, offering http apis and portal, etc.`,
// Initialize logger
if err := logger.InitManager(cfg.Verbose, cfg.Console, d.LogDir()); err != nil {
return errors.Wrap(err, "init manager logger")
return fmt.Errorf("init manager logger: %w", err)
}
logger.RedirectStdoutAndStderr(cfg.Console, path.Join(d.LogDir(), "manager"))

View File

@ -18,10 +18,10 @@ package cmd
import (
"context"
"fmt"
"os"
"path"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
@ -58,7 +58,7 @@ generate and maintain a P2P network during the download process, and push suitab
// Initialize logger
if err := logger.InitScheduler(cfg.Verbose, cfg.Console, d.LogDir()); err != nil {
return errors.Wrap(err, "init scheduler logger")
return fmt.Errorf("init scheduler logger: %w", err)
}
logger.RedirectStdoutAndStderr(cfg.Console, path.Join(d.LogDir(), "scheduler"))

2
go.mod
View File

@ -45,7 +45,6 @@ require (
github.com/onsi/ginkgo/v2 v2.1.4
github.com/onsi/gomega v1.19.0
github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.12.2
github.com/schollz/progressbar/v3 v3.8.6
github.com/serialx/hashring v0.0.0-20200727003509-22c0c7ab6b1b
@ -164,6 +163,7 @@ require (
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/power-devops/perfstat v0.0.0-20220216144756-c35f1ee13d7c // indirect
github.com/prometheus/client_model v0.2.0 // indirect

View File

@ -17,10 +17,9 @@
package dferrors
import (
"errors"
"fmt"
"github.com/pkg/errors"
"d7y.io/dragonfly/v2/pkg/rpc/base"
)

View File

@ -19,6 +19,7 @@ package job
import (
"context"
"encoding/json"
"errors"
"fmt"
"reflect"
"time"
@ -28,7 +29,6 @@ import (
machineryv1log "github.com/RichardKnop/machinery/v1/log"
machineryv1tasks "github.com/RichardKnop/machinery/v1/tasks"
"github.com/go-redis/redis/v8"
"github.com/pkg/errors"
)
const (

View File

@ -17,9 +17,8 @@
package job
import (
"errors"
"fmt"
"github.com/pkg/errors"
)
type Queue string

View File

@ -17,13 +17,13 @@
package middlewares
import (
"errors"
"net/http"
"github.com/VividCortex/mysqlerr"
"github.com/gin-gonic/gin"
"github.com/go-sql-driver/mysql"
redigo "github.com/gomodule/redigo/redis"
"github.com/pkg/errors"
"golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
@ -55,8 +55,9 @@ func Error() gin.HandlerFunc {
}
// RPC error handler
if err, ok := errors.Cause(err.Err).(*dferrors.DfError); ok {
switch err.Code {
var dferr *dferrors.DfError
if errors.As(err.Err, &dferr) {
switch dferr.Code {
case base.Code_InvalidResourceType:
c.JSON(http.StatusBadRequest, ErrorResponse{
Message: http.StatusText(http.StatusBadRequest),
@ -91,8 +92,9 @@ func Error() gin.HandlerFunc {
}
// Mysql error handler
if err, ok := errors.Cause(err.Err).(*mysql.MySQLError); ok {
switch err.Number {
var merr *mysql.MySQLError
if errors.As(err.Err, &merr) {
switch merr.Number {
case mysqlerr.ER_DUP_ENTRY:
c.JSON(http.StatusConflict, ErrorResponse{
Message: http.StatusText(http.StatusConflict),

View File

@ -18,11 +18,11 @@ package service
import (
"context"
"errors"
"fmt"
"github.com/VividCortex/mysqlerr"
"github.com/go-sql-driver/mysql"
"github.com/pkg/errors"
"golang.org/x/crypto/bcrypt"
manageroauth "d7y.io/dragonfly/v2/manager/auth/oauth"
@ -179,7 +179,8 @@ func (s *service) OauthSigninCallback(ctx context.Context, name, code string) (*
State: model.UserStateEnabled,
}
if err := s.db.WithContext(ctx).Create(&user).Error; err != nil {
if err, ok := errors.Cause(err).(*mysql.MySQLError); ok && err.Number == mysqlerr.ER_DUP_ENTRY {
var merr *mysql.MySQLError
if errors.As(err, &merr) && merr.Number == mysqlerr.ER_DUP_ENTRY {
return &user, nil
}

View File

@ -18,8 +18,8 @@ package dfnet
import (
"encoding/json"
"errors"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
)

View File

@ -17,12 +17,11 @@
package dfpath
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"sync"
"github.com/pkg/errors"
)
// Dfpath is the interface used for init project path
@ -110,7 +109,7 @@ func New(options ...Option) (Dfpath, error) {
for name, dir := range map[string]string{"workHome": d.workHome, "cacheDir": d.cacheDir, "logDir": d.logDir, "dataDir": d.dataDir,
"pluginDir": d.pluginDir} {
if err := os.MkdirAll(dir, fs.FileMode(0755)); err != nil {
cache.errs = append(cache.errs, errors.Errorf("create %s dir %s failed: %v", name, dir, err))
cache.errs = append(cache.errs, fmt.Errorf("create %s dir %s failed: %v", name, dir, err))
}
}
@ -118,7 +117,7 @@ func New(options ...Option) (Dfpath, error) {
})
if len(cache.errs) > 0 {
return nil, errors.Errorf("create dfpath failed: %s", cache.errs)
return nil, fmt.Errorf("create dfpath failed: %s", cache.errs)
}
d := *cache.d

View File

@ -24,12 +24,11 @@ import (
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"errors"
"fmt"
"hash"
"io"
"github.com/pkg/errors"
logger "d7y.io/dragonfly/v2/internal/dflog"
)

View File

@ -20,8 +20,6 @@ import (
"fmt"
"strconv"
"strings"
"github.com/pkg/errors"
)
const (
@ -72,7 +70,7 @@ func GetRange(rangeStr string) (r *Range, err error) {
// length is file total length
func ParseRange(rangeStr string, length uint64) (*Range, error) {
if strings.Count(rangeStr, "-") != 1 {
return nil, errors.Errorf("invalid range: %s, should be like 0-1023", rangeStr)
return nil, fmt.Errorf("invalid range: %s, should be like 0-1023", rangeStr)
}
// -{endIndex}
@ -103,11 +101,11 @@ func ParseRange(rangeStr string, length uint64) (*Range, error) {
func handlePrefixRange(rangeStr string, length uint64) (*Range, error) {
downLength, err := strconv.ParseUint(strings.TrimPrefix(rangeStr, "-"), 10, 64)
if err != nil {
return nil, errors.Errorf("failed to parse range: %s to int: %v", rangeStr, err)
return nil, fmt.Errorf("failed to parse range: %s to int: %v", rangeStr, err)
}
if downLength > length {
return nil, errors.Errorf("range: %s, the downLength is larger than length", rangeStr)
return nil, fmt.Errorf("range: %s, the downLength is larger than length", rangeStr)
}
return &Range{
@ -119,11 +117,11 @@ func handlePrefixRange(rangeStr string, length uint64) (*Range, error) {
func handleSuffixRange(rangeStr string, length uint64) (*Range, error) {
startIndex, err := strconv.ParseUint(strings.TrimSuffix(rangeStr, "-"), 10, 64)
if err != nil {
return nil, errors.Errorf("failed to parse range: %s to uint: %v", rangeStr, err)
return nil, fmt.Errorf("failed to parse range: %s to uint: %v", rangeStr, err)
}
if startIndex > length {
return nil, errors.Errorf("range: %s, the startIndex is larger than length", rangeStr)
return nil, fmt.Errorf("range: %s, the startIndex is larger than length", rangeStr)
}
return &Range{
@ -137,15 +135,15 @@ func handlePairRange(rangeStr string, length uint64) (*Range, error) {
startIndex, err := strconv.ParseUint(rangePair[0], 10, 64)
if err != nil {
return nil, errors.Errorf("failed to parse range: %s to uint: %v", rangeStr, err)
return nil, fmt.Errorf("failed to parse range: %s to uint: %v", rangeStr, err)
}
if startIndex > length {
return nil, errors.Errorf("range: %s, the startIndex is larger than length", rangeStr)
return nil, fmt.Errorf("range: %s, the startIndex is larger than length", rangeStr)
}
endIndex, err := strconv.ParseUint(rangePair[1], 10, 64)
if err != nil {
return nil, errors.Errorf("failed to parse range: %s to uint: %v", rangeStr, err)
return nil, fmt.Errorf("failed to parse range: %s to uint: %v", rangeStr, err)
}
if endIndex >= length {
//attention
@ -153,7 +151,7 @@ func handlePairRange(rangeStr string, length uint64) (*Range, error) {
}
if endIndex < startIndex {
return nil, errors.Errorf("range: %s, the start is larger the end", rangeStr)
return nil, fmt.Errorf("range: %s, the start is larger the end", rangeStr)
}
return &Range{

View File

@ -17,11 +17,10 @@
package ip
import (
"fmt"
"net"
"os"
"github.com/pkg/errors"
logger "d7y.io/dragonfly/v2/internal/dflog"
)
@ -59,7 +58,7 @@ func externalIPv4() (string, error) {
}
if len(externalIPs) == 0 {
return "", errors.Errorf("can not found external ipv4")
return "", fmt.Errorf("can not found external ipv4")
}
return externalIPs[0], nil

View File

@ -18,6 +18,7 @@ package objectstorage
import (
"context"
"errors"
"fmt"
"io"
"net/http"
@ -26,7 +27,6 @@ import (
aliyunoss "github.com/aliyun/aliyun-oss-go-sdk/oss"
"github.com/go-http-utils/headers"
"github.com/pkg/errors"
)
type oss struct {
@ -96,7 +96,8 @@ func (o *oss) GetObjectMetadata(ctx context.Context, bucketName, objectKey strin
header, err := bucket.GetObjectDetailedMeta(objectKey)
if err != nil {
if serr, ok := errors.Cause(err).(aliyunoss.ServiceError); ok && serr.StatusCode == http.StatusNotFound {
var serr *aliyunoss.ServiceError
if errors.As(err, &serr) && serr.StatusCode == http.StatusNotFound {
return nil, false, nil
}

View File

@ -18,10 +18,10 @@ package client
import (
"context"
"fmt"
"sync"
"time"
"github.com/pkg/errors"
"google.golang.org/grpc"
logger "d7y.io/dragonfly/v2/internal/dflog"
@ -81,7 +81,7 @@ var _ CdnClient = (*cdnClient)(nil)
func (cc *cdnClient) getCdnClient(key string, stick bool) (cdnsystem.SeederClient, string, error) {
clientConn, err := cc.Connection.GetClientConn(key, stick)
if err != nil {
return nil, "", errors.Wrapf(err, "get ClientConn for hashKey %s", key)
return nil, "", fmt.Errorf("get ClientConn for hashKey %s: %w", key, err)
}
return cdnsystem.NewSeederClient(clientConn), clientConn.Target(), nil
}

View File

@ -18,8 +18,9 @@ package client
import (
"context"
"errors"
"fmt"
"github.com/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
@ -76,8 +77,8 @@ func (pss *PieceSeedStream) initStream() error {
return client.ObtainSeeds(pss.ctx, pss.sr, pss.opts...)
}, pss.InitBackoff, pss.MaxBackOff, pss.MaxAttempts, nil)
if err != nil {
if errors.Cause(err) == dferrors.ErrNoCandidateNode {
return errors.Wrapf(err, "get grpc server instance failed")
if errors.Is(err, dferrors.ErrNoCandidateNode) {
return fmt.Errorf("get grpc server instance failed: %w", err)
}
logger.WithTaskID(pss.hashKey).Errorf("initStream: invoke cdn node %s ObtainSeeds failed: %v", target, err)
return pss.replaceClient(pss.hashKey, err)

View File

@ -22,7 +22,6 @@ import (
"sync"
"time"
"github.com/pkg/errors"
"github.com/serialx/hashring"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
@ -292,7 +291,7 @@ func (conn *Connection) GetClientConnByTarget(node string) (*grpc.ClientConn, er
defer conn.rwMutex.RUnlock()
clientConn, err := conn.loadOrCreateClientConnByNode(node)
if err != nil {
return nil, errors.Wrapf(err, "get client conn by conn %s", node)
return nil, fmt.Errorf("get client conn by conn %s: %w", node, err)
}
logger.GrpcLogger.With("conn", conn.name).Debugf("successfully get %s client conn", node)
return clientConn, nil
@ -301,7 +300,7 @@ func (conn *Connection) GetClientConnByTarget(node string) (*grpc.ClientConn, er
func (conn *Connection) loadOrCreateClientConnByNode(node string) (clientConn *grpc.ClientConn, err error) {
defer func() {
if desc := recover(); desc != nil {
err = errors.Errorf("%v", desc)
err = fmt.Errorf("%v", desc)
}
}()
conn.accessNodeMap.Store(node, time.Now())
@ -320,7 +319,7 @@ func (conn *Connection) loadOrCreateClientConnByNode(node string) (clientConn *g
return clientConn, nil
}
return nil, errors.Wrapf(err, "cannot found clientConn associated with node %s and create client conn failed", node)
return nil, fmt.Errorf("cannot found clientConn associated with node %s and create client conn failed: %w", node, err)
}
// GetClientConn get conn or bind hashKey to candidate node, don't do the migrate action
@ -354,7 +353,7 @@ func (conn *Connection) GetClientConn(hashKey string, stick bool) (clientConn *g
defer conn.rwMutex.Unlock()
client, err := conn.findCandidateClientConn(hashKey, sets.NewString())
if err != nil {
return nil, errors.Wrapf(err, "prob candidate client conn for hash key %s", hashKey)
return nil, fmt.Errorf("prob candidate client conn for hash key %s: %w", hashKey, err)
}
conn.key2NodeMap.Store(hashKey, client.node)
conn.node2ClientMap.Store(client.node, client.Ref)
@ -388,7 +387,7 @@ func (conn *Connection) TryMigrate(key string, cause error, exclusiveNodes []str
defer conn.rwMutex.Unlock()
client, err := conn.findCandidateClientConn(key, sets.NewString(exclusiveNodes...))
if err != nil {
return "", errors.Wrapf(err, "find candidate client conn for hash key %s", key)
return "", fmt.Errorf("find candidate client conn for hash key %s: %w", key, err)
}
logger.GrpcLogger.With("conn", conn.name).Infof("successfully migrate hash key %s from server node %s to %s", key, currentNode, client.node)
conn.key2NodeMap.Store(key, client.node)

View File

@ -18,12 +18,12 @@ package client
import (
"context"
"errors"
"fmt"
"sync"
"time"
"github.com/google/uuid"
"github.com/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/emptypb"

View File

@ -18,8 +18,9 @@ package client
import (
"context"
"errors"
"fmt"
"github.com/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
@ -75,8 +76,8 @@ func (drs *DownResultStream) initStream() error {
return client.Download(drs.ctx, drs.req, drs.opts...)
}, drs.InitBackoff, drs.MaxBackOff, drs.MaxAttempts, nil)
if err != nil {
if errors.Cause(err) == dferrors.ErrNoCandidateNode {
return errors.Wrapf(err, "get grpc server instance failed")
if errors.Is(err, dferrors.ErrNoCandidateNode) {
return fmt.Errorf("get grpc server instance failed: %w", err)
}
logger.WithTaskID(drs.hashKey).Infof("initStream: invoke daemon node %s Download failed: %v", target, err)
return drs.replaceClient(err)

View File

@ -20,9 +20,9 @@ package client
import (
"context"
"errors"
"time"
"github.com/pkg/errors"
"google.golang.org/grpc"
logger "d7y.io/dragonfly/v2/internal/dflog"

View File

@ -24,7 +24,6 @@ import (
"strings"
"github.com/mdlayher/vsock"
"github.com/pkg/errors"
"google.golang.org/grpc"
"d7y.io/dragonfly/v2/pkg/dfnet"
@ -41,16 +40,16 @@ func VsockDialer(_ctx context.Context, address string) (net.Conn, error) {
cid, err := strconv.ParseUint(addr[0], 10, 32)
if err != nil {
return nil, errors.Wrapf(err, "failed to convert %q to vsock cid", addr[0])
return nil, fmt.Errorf("failed to convert %q to vsock cid: %w", addr[0], err)
}
port, err := strconv.ParseUint(addr[1], 10, 32)
if err != nil {
return nil, errors.Wrapf(err, "failed to convert %q to vsock port", addr[1])
return nil, fmt.Errorf("failed to convert %q to vsock port: %w", addr[1], err)
}
conn, err := vsock.Dial(uint32(cid), uint32(port), nil)
if err != nil {
return nil, errors.Wrapf(err, "failed to dial vsock %v:%v, address %s", uint32(cid), uint32(port), address)
return nil, fmt.Errorf("failed to dial vsock %v:%v, address %s: %w", uint32(cid), uint32(port), address, err)
}
return conn, nil
}

View File

@ -17,6 +17,7 @@
package hdfsprotocol
import (
"fmt"
"io"
"net/url"
"os/user"
@ -25,7 +26,6 @@ import (
"time"
"github.com/colinmarc/hdfs/v2"
"github.com/pkg/errors"
"d7y.io/dragonfly/v2/pkg/net/http"
"d7y.io/dragonfly/v2/pkg/source"
@ -127,7 +127,7 @@ func (h *hdfsSourceClient) Download(request *source.Request) (*source.Response,
// default read all data when rang is nil
var limitReadN = fileInfo.Size()
if limitReadN < 0 {
return nil, errors.Errorf("file length is illegal, length: %d", limitReadN)
return nil, fmt.Errorf("file length is illegal, length: %d", limitReadN)
}
if request.Header.Get(source.Range) != "" {
@ -203,7 +203,7 @@ func (h *hdfsSourceClient) getHDFSClient(url *url.URL) (*hdfs.Client, error) {
func (h *hdfsSourceClient) getHDFSClientAndPath(url *url.URL) (*hdfs.Client, string, error) {
client, err := h.getHDFSClient(url)
if err != nil {
return nil, "", errors.Errorf("hdfs create client failed, url is %s", url)
return nil, "", fmt.Errorf("hdfs create client failed, url is %s", url)
}
return client, url.Path, nil
}

View File

@ -20,6 +20,7 @@
package hdfsprotocol
import (
"errors"
"io"
"os"
"reflect"
@ -28,7 +29,6 @@ import (
"github.com/agiledragon/gomonkey/v2"
"github.com/colinmarc/hdfs/v2"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"d7y.io/dragonfly/v2/pkg/net/http"

View File

@ -26,7 +26,6 @@ import (
"github.com/go-http-utils/headers"
"github.com/jarcoal/httpmock"
"github.com/pkg/errors"
"github.com/stretchr/testify/suite"
nethttp "d7y.io/dragonfly/v2/pkg/net/http"
@ -196,7 +195,7 @@ func (suite *HTTPSourceClientTestSuite) TestHttpSourceClientDownloadWithResponse
request: errorRequest,
content: "",
expireInfo: nil,
wantErr: errors.Errorf("Get \"https://error.com\": error"),
wantErr: fmt.Errorf("Get \"https://error.com\": error"),
},
}
for _, tt := range tests {

View File

@ -17,6 +17,7 @@
package ossprotocol
import (
"errors"
"fmt"
"net/http"
"strconv"
@ -25,7 +26,6 @@ import (
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"github.com/go-http-utils/headers"
"github.com/pkg/errors"
"d7y.io/dragonfly/v2/pkg/source"
"d7y.io/dragonfly/v2/pkg/strings"
@ -95,15 +95,15 @@ func (osc *ossSourceClient) GetContentLength(request *source.Request) (int64, er
}
bucket, err := client.Bucket(request.URL.Host)
if err != nil {
return source.UnknownSourceFileLen, errors.Wrapf(err, "get oss bucket: %s", request.URL.Host)
return source.UnknownSourceFileLen, fmt.Errorf("get oss bucket %s: %w", request.URL.Host, err)
}
header, err := bucket.GetObjectMeta(request.URL.Path, getOptions(request.Header)...)
if err != nil {
return source.UnknownSourceFileLen, errors.Wrapf(err, "get oss object %s meta", request.URL.Path)
return source.UnknownSourceFileLen, fmt.Errorf("get oss object %s meta: %w", request.URL.Path, err)
}
contentLen, err := strconv.ParseInt(header.Get(oss.HTTPHeaderContentLength), 10, 64)
if err != nil {
return source.UnknownSourceFileLen, errors.Wrapf(err, "parse content-length str to int64")
return source.UnknownSourceFileLen, fmt.Errorf("parse content-length str to int64: %w", err)
}
return contentLen, nil
}
@ -114,11 +114,11 @@ func (osc *ossSourceClient) IsSupportRange(request *source.Request) (bool, error
}
client, err := osc.getClient(request.Header)
if err != nil {
return false, errors.Wrap(err, "get oss client")
return false, fmt.Errorf("get oss client: %w", err)
}
bucket, err := client.Bucket(request.URL.Host)
if err != nil {
return false, errors.Wrapf(err, "get oss bucket: %s", request.URL.Host)
return false, fmt.Errorf("get oss bucket %s: %w", request.URL.Host, err)
}
exist, err := bucket.IsObjectExist(request.URL.Path, getOptions(request.Header)...)
if err != nil {
@ -133,11 +133,11 @@ func (osc *ossSourceClient) IsSupportRange(request *source.Request) (bool, error
func (osc *ossSourceClient) IsExpired(request *source.Request, info *source.ExpireInfo) (bool, error) {
client, err := osc.getClient(request.Header)
if err != nil {
return false, errors.Wrap(err, "get oss client")
return false, fmt.Errorf("get oss client: %w", err)
}
bucket, err := client.Bucket(request.URL.Host)
if err != nil {
return false, errors.Wrapf(err, "get oss bucket: %s", request.URL.Host)
return false, fmt.Errorf("get oss bucket %s: %w", request.URL.Host, err)
}
resHeader, err := bucket.GetObjectMeta(request.URL.Path, getOptions(request.Header)...)
if err != nil {
@ -149,15 +149,15 @@ func (osc *ossSourceClient) IsExpired(request *source.Request, info *source.Expi
func (osc *ossSourceClient) Download(request *source.Request) (*source.Response, error) {
client, err := osc.getClient(request.Header)
if err != nil {
return nil, errors.Wrapf(err, "get oss client")
return nil, fmt.Errorf("get oss client: %w", err)
}
bucket, err := client.Bucket(request.URL.Host)
if err != nil {
return nil, errors.Wrapf(err, "get oss bucket: %s", request.URL.Host)
return nil, fmt.Errorf("get oss bucket %s: %w", request.URL.Host, err)
}
objectResult, err := bucket.DoGetObject(&oss.GetObjectRequest{ObjectKey: request.URL.Path}, getOptions(request.Header))
if err != nil {
return nil, errors.Wrapf(err, "get oss Object: %s", request.URL.Path)
return nil, fmt.Errorf("get oss Object %s: %w", request.URL.Path, err)
}
err = source.CheckResponseCode(objectResult.Response.StatusCode, []int{http.StatusOK, http.StatusPartialContent})
if err != nil {
@ -178,11 +178,11 @@ func (osc *ossSourceClient) Download(request *source.Request) (*source.Response,
func (osc *ossSourceClient) GetLastModified(request *source.Request) (int64, error) {
client, err := osc.getClient(request.Header)
if err != nil {
return -1, errors.Wrap(err, "get oss client")
return -1, fmt.Errorf("get oss client: %w", err)
}
bucket, err := client.Bucket(request.URL.Host)
if err != nil {
return -1, errors.Wrapf(err, "get oss bucket: %s", request.URL.Host)
return -1, fmt.Errorf("get oss bucket %s: %w", request.URL.Host, err)
}
header, err := bucket.GetObjectMeta(request.URL.Path, getOptions(request.Header)...)
if err != nil {

View File

@ -18,9 +18,8 @@ package source
import (
"context"
"errors"
"net/url"
"github.com/pkg/errors"
)
type Request struct {

View File

@ -19,6 +19,7 @@ package source
import (
"context"
"errors"
"fmt"
"net/url"
"strconv"
@ -26,8 +27,6 @@ import (
"sync"
"time"
"github.com/pkg/errors"
logger "d7y.io/dragonfly/v2/internal/dflog"
)
@ -162,7 +161,7 @@ func (m *clientManager) Register(scheme string, resourceClient ResourceClient, a
defer m.mu.Unlock()
if client, ok := m.clients[scheme]; ok {
if client.(*clientWrapper).rc != resourceClient {
return errors.Errorf("client with scheme %s already exist, current client: %#v", scheme, client)
return fmt.Errorf("client with scheme %s already exist, current client: %#v", scheme, client)
}
logger.Warnf("client with scheme %s already exist, no need register again", scheme)
return nil
@ -279,7 +278,7 @@ func (c *clientWrapper) GetLastModified(request *Request) (int64, error) {
func GetContentLength(request *Request) (int64, error) {
client, ok := _defaultManager.GetClient(request.URL.Scheme)
if !ok {
return UnknownSourceFileLen, errors.Wrapf(ErrNoClientFound, "scheme: %s", request.URL.Scheme)
return UnknownSourceFileLen, fmt.Errorf("scheme %s: %w", request.URL.Scheme, ErrNoClientFound)
}
if _, ok := request.Context().Deadline(); !ok {
ctx, cancel := context.WithTimeout(context.Background(), contextTimeout)
@ -292,7 +291,7 @@ func GetContentLength(request *Request) (int64, error) {
func IsSupportRange(request *Request) (bool, error) {
client, ok := _defaultManager.GetClient(request.URL.Scheme)
if !ok {
return false, errors.Wrapf(ErrNoClientFound, "scheme: %s", request.URL.Scheme)
return false, fmt.Errorf("scheme %s: %w", request.URL.Scheme, ErrNoClientFound)
}
if _, ok := request.Context().Deadline(); !ok {
ctx, cancel := context.WithTimeout(context.Background(), contextTimeout)
@ -308,7 +307,7 @@ func IsSupportRange(request *Request) (bool, error) {
func IsExpired(request *Request, info *ExpireInfo) (bool, error) {
client, ok := _defaultManager.GetClient(request.URL.Scheme)
if !ok {
return false, errors.Wrapf(ErrNoClientFound, "scheme: %s", request.URL.Scheme)
return false, fmt.Errorf("scheme %s: %w", request.URL.Scheme, ErrNoClientFound)
}
if _, ok := request.Context().Deadline(); !ok {
ctx, cancel := context.WithTimeout(context.Background(), contextTimeout)
@ -321,7 +320,7 @@ func IsExpired(request *Request, info *ExpireInfo) (bool, error) {
func GetLastModified(request *Request) (int64, error) {
client, ok := _defaultManager.GetClient(request.URL.Scheme)
if !ok {
return -1, errors.Wrapf(ErrNoClientFound, "scheme: %s", request.URL.Scheme)
return -1, fmt.Errorf("scheme %s: %w", request.URL.Scheme, ErrNoClientFound)
}
if _, ok := request.Context().Deadline(); !ok {
ctx, cancel := context.WithTimeout(context.Background(), contextTimeout)
@ -334,7 +333,7 @@ func GetLastModified(request *Request) (int64, error) {
func Download(request *Request) (*Response, error) {
client, ok := _defaultManager.GetClient(request.URL.Scheme)
if !ok {
return nil, errors.Wrapf(ErrNoClientFound, "scheme: %s", request.URL.Scheme)
return nil, fmt.Errorf("scheme %s: %w", request.URL.Scheme, ErrNoClientFound)
}
return client.Download(request)
}
@ -342,11 +341,11 @@ func Download(request *Request) (*Response, error) {
func List(request *Request) ([]*url.URL, error) {
client, ok := _defaultManager.GetClient(request.URL.Scheme)
if !ok {
return nil, errors.Wrapf(ErrNoClientFound, "scheme: %s", request.URL.Scheme)
return nil, fmt.Errorf("scheme %s: %w", request.URL.Scheme, ErrNoClientFound)
}
lister, ok := client.(ResourceLister)
if !ok {
return nil, errors.Wrapf(ErrClientNotSupportList, "scheme: %s", request.URL.Scheme)
return nil, fmt.Errorf("scheme %s: %w", request.URL.Scheme, ErrClientNotSupportList)
}
return lister.List(request)
}

View File

@ -18,11 +18,11 @@ package unit
import (
"encoding/json"
"errors"
"fmt"
"regexp"
"strconv"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
pkgstrings "d7y.io/dragonfly/v2/pkg/strings"
@ -101,7 +101,7 @@ func parseSize(fsize string) (Bytes, error) {
matches := sizeRegexp.FindStringSubmatch(fsize)
if len(matches) == 0 {
return 0, errors.Errorf("parse size %s: invalid format", fsize)
return 0, fmt.Errorf("parse size %s: invalid format", fsize)
}
var unit Bytes
@ -124,7 +124,7 @@ func parseSize(fsize string) (Bytes, error) {
num, err := strconv.ParseInt(matches[1], 0, 64)
if err != nil {
return 0, errors.Wrapf(err, "failed to parse size: %s", fsize)
return 0, fmt.Errorf("failed to parse size %s: %w", fsize, err)
}
return ToBytes(num) * unit, nil
@ -161,7 +161,7 @@ func (f *Bytes) unmarshal(unmarshal func(in []byte, out interface{}) (err error)
case string:
size, err := parseSize(value)
if err != nil {
return errors.WithMessage(err, "invalid byte size")
return fmt.Errorf("invalid byte size: %w", err)
}
*f = size
return nil

View File

@ -17,10 +17,9 @@
package config
import (
"errors"
"time"
"github.com/pkg/errors"
"d7y.io/dragonfly/v2/cmd/dependency/base"
"d7y.io/dragonfly/v2/pkg/net/fqdn"
"d7y.io/dragonfly/v2/pkg/net/ip"

View File

@ -17,13 +17,13 @@
package config
import (
"errors"
"os"
"path/filepath"
"testing"
"time"
"github.com/golang/mock/gomock"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"d7y.io/dragonfly/v2/pkg/rpc/manager"

View File

@ -20,10 +20,9 @@ package resource
import (
"context"
"fmt"
"time"
"github.com/pkg/errors"
"d7y.io/dragonfly/v2/pkg/rpc/base/common"
"d7y.io/dragonfly/v2/pkg/rpc/cdnsystem"
rpcscheduler "d7y.io/dragonfly/v2/pkg/rpc/scheduler"
@ -149,7 +148,7 @@ func (s *seedPeer) initSeedPeer(task *Task, ps *cdnsystem.PieceSeed) (*Peer, err
host, ok := s.hostManager.Load(ps.HostId)
if !ok {
task.Log.Errorf("can not find seed host id: %s", ps.HostId)
return nil, errors.Errorf("can not find host id: %s", ps.HostId)
return nil, fmt.Errorf("can not find host id: %s", ps.HostId)
}
// New seed peer.