Skip to content

Commit

Permalink
chore: just use std errors in drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Aug 31, 2022
1 parent 817d635 commit 9ec6d5b
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 52 deletions.
3 changes: 1 addition & 2 deletions drivers/aliyundrive/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions drivers/aliyundrive/util.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package local

import (
"errors"
"fmt"
"net/http"

"github.com/alist-org/alist/v3/drivers/base"
"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
Expand Down Expand Up @@ -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" {
Expand Down
29 changes: 15 additions & 14 deletions drivers/local/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package local
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
Expand All @@ -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 {
Expand All @@ -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
}
}
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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()
Expand All @@ -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
}
Expand Down
3 changes: 1 addition & 2 deletions drivers/onedrive/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
}
Expand Down
4 changes: 2 additions & 2 deletions drivers/onedrive/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package onedrive
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/http"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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" {
Expand Down
3 changes: 1 addition & 2 deletions drivers/pikpak/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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()
}
Expand Down
2 changes: 1 addition & 1 deletion drivers/pikpak/util.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 1 addition & 2 deletions drivers/template/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions drivers/virtual/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
35 changes: 18 additions & 17 deletions internal/operations/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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")
}
Expand All @@ -203,15 +204,15 @@ 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 {
srcObj, err := Get(ctx, storage, srcPath)
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
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
Loading

0 comments on commit 9ec6d5b

Please sign in to comment.