From 9ec6d5be7af1d97d17c29dce96015df92c375417 Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Wed, 31 Aug 2022 20:58:57 +0800 Subject: [PATCH] chore: just use std errors in drivers --- drivers/aliyundrive/driver.go | 3 +-- drivers/aliyundrive/util.go | 4 ++-- drivers/local/driver.go | 29 ++++++++++++++-------------- drivers/onedrive/driver.go | 3 +-- drivers/onedrive/util.go | 4 ++-- drivers/pikpak/driver.go | 3 +-- drivers/pikpak/util.go | 2 +- drivers/template/driver.go | 3 +-- drivers/virtual/driver.go | 3 +-- internal/operations/fs.go | 35 +++++++++++++++++----------------- internal/operations/storage.go | 12 ++++++------ 11 files changed, 49 insertions(+), 52 deletions(-) diff --git a/drivers/aliyundrive/driver.go b/drivers/aliyundrive/driver.go index 3fa1ab13..0907ff57 100644 --- a/drivers/aliyundrive/driver.go +++ b/drivers/aliyundrive/driver.go @@ -22,7 +22,6 @@ import ( "github.com/alist-org/alist/v3/pkg/cron" "github.com/alist-org/alist/v3/pkg/utils" "github.com/go-resty/resty/v2" - "github.com/pkg/errors" log "github.com/sirupsen/logrus" ) @@ -46,7 +45,7 @@ func (d *AliDrive) Init(ctx context.Context, storage model.Storage) error { d.Storage = storage err := utils.Json.UnmarshalFromString(d.Storage.Addition, &d.Addition) if err != nil { - return errors.Wrap(err, "error while unmarshal addition") + return err } // TODO login / refresh token //operations.MustSaveDriverStorage(d) diff --git a/drivers/aliyundrive/util.go b/drivers/aliyundrive/util.go index 113c522e..61c58ec1 100644 --- a/drivers/aliyundrive/util.go +++ b/drivers/aliyundrive/util.go @@ -1,6 +1,7 @@ package local import ( + "errors" "fmt" "net/http" @@ -8,7 +9,6 @@ import ( "github.com/alist-org/alist/v3/internal/operations" "github.com/alist-org/alist/v3/pkg/utils" "github.com/go-resty/resty/v2" - "github.com/pkg/errors" ) // do others that not defined in Driver interface @@ -51,7 +51,7 @@ func (d *AliDrive) request(url, method string, callback func(*resty.Request), re req.SetError(&e) res, err := req.Execute(method, url) if err != nil { - return nil, errors.WithStack(err), e + return nil, err, e } if e.Code != "" { if e.Code == "AccessTokenInvalid" { diff --git a/drivers/local/driver.go b/drivers/local/driver.go index 32a1941c..afff61a9 100644 --- a/drivers/local/driver.go +++ b/drivers/local/driver.go @@ -3,6 +3,8 @@ package local import ( "bytes" "context" + "errors" + "fmt" "io" "io/ioutil" "net/http" @@ -20,7 +22,6 @@ import ( "github.com/alist-org/alist/v3/pkg/utils" "github.com/alist-org/alist/v3/server/common" "github.com/disintegration/imaging" - "github.com/pkg/errors" ) type Local struct { @@ -36,15 +37,15 @@ func (d *Local) Init(ctx context.Context, storage model.Storage) error { d.Storage = storage err := utils.Json.UnmarshalFromString(d.Storage.Addition, &d.Addition) if err != nil { - return errors.Wrap(err, "error while unmarshal addition") + return err } if !utils.Exists(d.RootFolder) { - err = errors.Errorf("root folder %s not exists", d.RootFolder) + err = fmt.Errorf("root folder %s not exists", d.RootFolder) } else { if !filepath.IsAbs(d.RootFolder) { d.RootFolder, err = filepath.Abs(d.RootFolder) if err != nil { - return errors.Wrap(err, "error while get abs path") + return err } } } @@ -64,7 +65,7 @@ func (d *Local) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([ fullPath := dir.GetID() rawFiles, err := ioutil.ReadDir(fullPath) if err != nil { - return nil, errors.Wrapf(err, "error while read dir %s", fullPath) + return nil, err } var files []model.Obj for _, f := range rawFiles { @@ -97,9 +98,9 @@ func (d *Local) Get(ctx context.Context, path string) (model.Obj, error) { f, err := os.Stat(path) if err != nil { if strings.Contains(err.Error(), "cannot find the file") { - return nil, errors.WithStack(errs.ObjectNotFound) + return nil, errs.ObjectNotFound } - return nil, errors.Wrapf(err, "error while stat %s", path) + return nil, err } file := model.Object{ ID: path, @@ -145,7 +146,7 @@ func (d *Local) MakeDir(ctx context.Context, parentDir model.Obj, dirName string fullPath := filepath.Join(parentDir.GetID(), dirName) err := os.MkdirAll(fullPath, 0700) if err != nil { - return errors.Wrapf(err, "error while make dir %s", fullPath) + return err } return nil } @@ -155,7 +156,7 @@ func (d *Local) Move(ctx context.Context, srcObj, dstDir model.Obj) error { dstPath := filepath.Join(dstDir.GetID(), srcObj.GetName()) err := os.Rename(srcPath, dstPath) if err != nil { - return errors.Wrapf(err, "error while move %s to %s", srcPath, dstPath) + return err } return nil } @@ -165,7 +166,7 @@ func (d *Local) Rename(ctx context.Context, srcObj model.Obj, newName string) er dstPath := filepath.Join(filepath.Dir(srcPath), newName) err := os.Rename(srcPath, dstPath) if err != nil { - return errors.Wrapf(err, "error while rename %s to %s", srcPath, dstPath) + return err } return nil } @@ -180,7 +181,7 @@ func (d *Local) Copy(ctx context.Context, srcObj, dstDir model.Obj) error { err = copyFile(srcPath, dstPath) } if err != nil { - return errors.Wrapf(err, "error while copy %s to %s", srcPath, dstPath) + return err } return nil } @@ -193,7 +194,7 @@ func (d *Local) Remove(ctx context.Context, obj model.Obj) error { err = os.Remove(obj.GetID()) } if err != nil { - return errors.Wrapf(err, "error while remove %s", obj.GetID()) + return err } return nil } @@ -202,7 +203,7 @@ func (d *Local) Put(ctx context.Context, dstDir model.Obj, stream model.FileStre fullPath := filepath.Join(dstDir.GetID(), stream.GetName()) out, err := os.Create(fullPath) if err != nil { - return errors.Wrapf(err, "error while create file %s", fullPath) + return err } defer func() { _ = out.Close() @@ -212,7 +213,7 @@ func (d *Local) Put(ctx context.Context, dstDir model.Obj, stream model.FileStre }() err = utils.CopyWithCtx(ctx, out, stream) if err != nil { - return errors.Wrapf(err, "error while copy file %s", fullPath) + return err } return nil } diff --git a/drivers/onedrive/driver.go b/drivers/onedrive/driver.go index 113f9f83..8cf70c04 100644 --- a/drivers/onedrive/driver.go +++ b/drivers/onedrive/driver.go @@ -11,7 +11,6 @@ import ( "github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/pkg/utils" "github.com/go-resty/resty/v2" - "github.com/pkg/errors" ) type Onedrive struct { @@ -32,7 +31,7 @@ func (d *Onedrive) Init(ctx context.Context, storage model.Storage) error { d.Storage = storage err := utils.Json.UnmarshalFromString(d.Storage.Addition, &d.Addition) if err != nil { - return errors.Wrap(err, "error while unmarshal addition") + return err } return d.refreshToken() } diff --git a/drivers/onedrive/util.go b/drivers/onedrive/util.go index bf519be4..0b60c0d0 100644 --- a/drivers/onedrive/util.go +++ b/drivers/onedrive/util.go @@ -3,6 +3,7 @@ package onedrive import ( "bytes" "context" + "errors" "fmt" "io" "net/http" @@ -17,7 +18,6 @@ import ( "github.com/alist-org/alist/v3/pkg/utils" "github.com/go-resty/resty/v2" jsoniter "github.com/json-iterator/go" - "github.com/pkg/errors" log "github.com/sirupsen/logrus" ) @@ -109,7 +109,7 @@ func (d *Onedrive) Request(url string, method string, callback func(*resty.Reque req.SetError(&e) res, err := req.Execute(method, url) if err != nil { - return nil, errors.WithStack(err) + return nil, err } if e.Error.Code != "" { if e.Error.Code == "InvalidAuthenticationToken" { diff --git a/drivers/pikpak/driver.go b/drivers/pikpak/driver.go index 2d2e94b5..cfa04dfe 100644 --- a/drivers/pikpak/driver.go +++ b/drivers/pikpak/driver.go @@ -22,7 +22,6 @@ import ( "github.com/aws/aws-sdk-go/service/s3/s3manager" "github.com/go-resty/resty/v2" jsoniter "github.com/json-iterator/go" - "github.com/pkg/errors" log "github.com/sirupsen/logrus" ) @@ -45,7 +44,7 @@ func (d *PikPak) Init(ctx context.Context, storage model.Storage) error { d.Storage = storage err := utils.Json.UnmarshalFromString(d.Storage.Addition, &d.Addition) if err != nil { - return errors.Wrap(err, "error while unmarshal addition") + return err } return d.login() } diff --git a/drivers/pikpak/util.go b/drivers/pikpak/util.go index 391f1bc1..b7e84b41 100644 --- a/drivers/pikpak/util.go +++ b/drivers/pikpak/util.go @@ -1,13 +1,13 @@ package local import ( + "errors" "net/http" "github.com/alist-org/alist/v3/drivers/base" "github.com/alist-org/alist/v3/internal/operations" "github.com/go-resty/resty/v2" jsoniter "github.com/json-iterator/go" - "github.com/pkg/errors" ) // do others that not defined in Driver interface diff --git a/drivers/template/driver.go b/drivers/template/driver.go index ceb3cc02..a10eed4c 100644 --- a/drivers/template/driver.go +++ b/drivers/template/driver.go @@ -7,7 +7,6 @@ import ( "github.com/alist-org/alist/v3/internal/errs" "github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/pkg/utils" - "github.com/pkg/errors" ) type Template struct { @@ -27,7 +26,7 @@ func (d *Template) Init(ctx context.Context, storage model.Storage) error { d.Storage = storage err := utils.Json.UnmarshalFromString(d.Storage.Addition, &d.Addition) if err != nil { - return errors.Wrap(err, "error while unmarshal addition") + return err } // TODO login / refresh token //operations.MustSaveDriverStorage(d) diff --git a/drivers/virtual/driver.go b/drivers/virtual/driver.go index 58e220ef..ec035c38 100644 --- a/drivers/virtual/driver.go +++ b/drivers/virtual/driver.go @@ -10,7 +10,6 @@ import ( "github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/pkg/utils" "github.com/alist-org/alist/v3/pkg/utils/random" - "github.com/pkg/errors" ) type Virtual struct { @@ -26,7 +25,7 @@ func (d *Virtual) Init(ctx context.Context, storage model.Storage) error { d.Storage = storage err := utils.Json.UnmarshalFromString(storage.Addition, &d.Addition) if err != nil { - return errors.Wrap(err, "error while unmarshal addition") + return err } return nil } diff --git a/internal/operations/fs.go b/internal/operations/fs.go index 0845a361..523d22ae 100644 --- a/internal/operations/fs.go +++ b/internal/operations/fs.go @@ -19,12 +19,12 @@ import ( // In order to facilitate adding some other things before and after file operations -var filesCache = cache.NewMemCache(cache.WithShards[[]model.Obj](64)) -var filesG singleflight.Group[[]model.Obj] +var listCache = cache.NewMemCache(cache.WithShards[[]model.Obj](64)) +var listG singleflight.Group[[]model.Obj] func ClearCache(storage driver.Driver, path string) { key := stdpath.Join(storage.GetStorage().MountPath, path) - filesCache.Del(key) + listCache.Del(key) } // List files in storage, not contains virtual file @@ -39,23 +39,24 @@ func List(ctx context.Context, storage driver.Driver, path string, args model.Li return nil, errors.WithStack(errs.NotFolder) } if storage.Config().NoCache { - return storage.List(ctx, dir, args) + objs, err := storage.List(ctx, dir, args) + return objs, errors.WithStack(err) } key := stdpath.Join(storage.GetStorage().MountPath, path) if len(refresh) == 0 || !refresh[0] { - if files, ok := filesCache.Get(key); ok { + if files, ok := listCache.Get(key); ok { return files, nil } } - files, err, _ := filesG.Do(key, func() ([]model.Obj, error) { + objs, err, _ := listG.Do(key, func() ([]model.Obj, error) { files, err := storage.List(ctx, dir, args) if err != nil { - return nil, errors.WithMessage(err, "failed to list files") + return nil, errors.Wrapf(err, "failed to list objs") } - filesCache.Set(key, files, cache.WithEx[[]model.Obj](time.Minute*time.Duration(storage.GetStorage().CacheExpiration))) + listCache.Set(key, files, cache.WithEx[[]model.Obj](time.Minute*time.Duration(storage.GetStorage().CacheExpiration))) return files, nil }) - return files, err + return objs, err } func isRoot(path, rootFolderPath string) bool { @@ -140,7 +141,7 @@ func Link(ctx context.Context, storage driver.Driver, path string, args model.Li fn := func() (*model.Link, error) { link, err := storage.Link(ctx, file, args) if err != nil { - return nil, errors.WithMessage(err, "failed get link") + return nil, errors.Wrapf(err, "failed get link") } if link.Expiration != nil { linkCache.Set(key, link, cache.WithEx[*model.Link](*link.Expiration)) @@ -179,7 +180,7 @@ func MakeDir(ctx context.Context, storage driver.Driver, path string) error { if err != nil { return errors.WithMessagef(err, "failed to get parent dir [%s]", parentPath) } - return storage.MakeDir(ctx, parentDir, dirName) + return errors.WithStack(storage.MakeDir(ctx, parentDir, dirName)) } else { return errors.WithMessage(err, "failed to check if dir exists") } @@ -203,7 +204,7 @@ func Move(ctx context.Context, storage driver.Driver, srcPath, dstDirPath string if err != nil { return errors.WithMessage(err, "failed to get dst dir") } - return storage.Move(ctx, srcObj, dstDir) + return errors.WithStack(storage.Move(ctx, srcObj, dstDir)) } func Rename(ctx context.Context, storage driver.Driver, srcPath, dstName string) error { @@ -211,7 +212,7 @@ func Rename(ctx context.Context, storage driver.Driver, srcPath, dstName string) if err != nil { return errors.WithMessage(err, "failed to get src object") } - return storage.Rename(ctx, srcObj, dstName) + return errors.WithStack(storage.Rename(ctx, srcObj, dstName)) } // Copy Just copy file[s] in a storage @@ -221,7 +222,7 @@ func Copy(ctx context.Context, storage driver.Driver, srcPath, dstDirPath string return errors.WithMessage(err, "failed to get src object") } dstDir, err := Get(ctx, storage, dstDirPath) - return storage.Copy(ctx, srcObj, dstDir) + return errors.WithStack(storage.Copy(ctx, srcObj, dstDir)) } func Remove(ctx context.Context, storage driver.Driver, path string) error { @@ -233,7 +234,7 @@ func Remove(ctx context.Context, storage driver.Driver, path string) error { } return errors.WithMessage(err, "failed to get object") } - return storage.Remove(ctx, obj) + return errors.WithStack(storage.Remove(ctx, obj)) } func Put(ctx context.Context, storage driver.Driver, dstDirPath string, file model.FileStreamer, up driver.UpdateProgress) error { @@ -282,7 +283,7 @@ func Put(ctx context.Context, storage driver.Driver, dstDirPath string, file mod up(100) // clear cache key := stdpath.Join(storage.GetStorage().MountPath, dstDirPath) - filesCache.Del(key) + listCache.Del(key) } - return err + return errors.WithStack(err) } diff --git a/internal/operations/storage.go b/internal/operations/storage.go index 922c9c5f..7281d034 100644 --- a/internal/operations/storage.go +++ b/internal/operations/storage.go @@ -52,7 +52,7 @@ func CreateStorage(ctx context.Context, storage model.Storage) error { if err != nil { storageDriver.GetStorage().SetStatus(fmt.Sprintf("%+v", err.Error())) MustSaveDriverStorage(storageDriver) - return errors.WithMessage(err, "failed init storage but storage is already created") + return errors.Wrapf(err, "failed init storage but storage is already created") } else { storageDriver.GetStorage().SetStatus("work") MustSaveDriverStorage(storageDriver) @@ -74,7 +74,7 @@ func LoadStorage(ctx context.Context, storage model.Storage) error { storageDriver := driverNew() err = storageDriver.Init(ctx, storage) if err != nil { - return errors.WithMessage(err, "failed init storage but storage is already created") + return errors.Wrapf(err, "failed init storage but storage is already created") } log.Debugf("storage %+v is created", storageDriver) storagesMap.Store(storage.MountPath, storageDriver) @@ -120,7 +120,7 @@ func DisableStorage(ctx context.Context, id uint) error { } // drop the storage in the driver if err := storageDriver.Drop(ctx); err != nil { - return errors.WithMessage(err, "failed drop storage") + return errors.Wrapf(err, "failed drop storage") } // delete the storage in the memory storagesMap.Delete(storage.MountPath) @@ -162,11 +162,11 @@ func UpdateStorage(ctx context.Context, storage model.Storage) error { } err = storageDriver.Drop(ctx) if err != nil { - return errors.WithMessage(err, "failed drop storage") + return errors.Wrapf(err, "failed drop storage") } err = storageDriver.Init(ctx, storage) if err != nil { - return errors.WithMessage(err, "failed init storage") + return errors.Wrapf(err, "failed init storage") } storagesMap.Store(storage.MountPath, storageDriver) return nil @@ -183,7 +183,7 @@ func DeleteStorageById(ctx context.Context, id uint) error { } // drop the storage in the driver if err := storageDriver.Drop(ctx); err != nil { - return errors.WithMessage(err, "failed drop storage") + return errors.Wrapf(err, "failed drop storage") } // delete the storage in the database if err := db.DeleteStorageById(id); err != nil {