From c3040fdfc3803ef3c8ae8eb8a5c7fbd950943340 Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Thu, 23 Jun 2022 15:57:10 +0800 Subject: [PATCH] chore: move errors --- internal/aria2/add.go | 12 +++++------- internal/aria2/aria2.go | 2 +- internal/{driver/error.go => errs/errors.go} | 7 +++++-- internal/fs/errors.go | 9 --------- internal/fs/put.go | 3 ++- internal/fs/write.go | 3 ++- internal/operations/error.go | 3 --- internal/operations/fs.go | 10 +++++++--- 8 files changed, 22 insertions(+), 27 deletions(-) rename internal/{driver/error.go => errs/errors.go} (63%) delete mode 100644 internal/fs/errors.go delete mode 100644 internal/operations/error.go diff --git a/internal/aria2/add.go b/internal/aria2/add.go index 6f16674d..72d23ef5 100644 --- a/internal/aria2/add.go +++ b/internal/aria2/add.go @@ -4,8 +4,7 @@ import ( "context" "fmt" "github.com/alist-org/alist/v3/conf" - "github.com/alist-org/alist/v3/internal/driver" - "github.com/alist-org/alist/v3/internal/fs" + "github.com/alist-org/alist/v3/internal/errs" "github.com/alist-org/alist/v3/internal/operations" "github.com/alist-org/alist/v3/pkg/task" "github.com/google/uuid" @@ -21,18 +20,18 @@ func AddURI(ctx context.Context, uri string, dstDirPath string) error { } // check is it could upload if account.Config().NoUpload { - return errors.WithStack(fs.ErrUploadNotSupported) + return errors.WithStack(errs.ErrUploadNotSupported) } // check path is valid obj, err := operations.Get(ctx, account, dstDirActualPath) if err != nil { - if !errors.Is(errors.Cause(err), driver.ErrorObjectNotFound) { + if !errs.IsErrObjectNotFound(err) { return errors.WithMessage(err, "failed get object") } } else { if !obj.IsDir() { // can't add to a file - return errors.WithStack(fs.ErrNotFolder) + return errors.WithStack(errs.ErrNotFolder) } } // call aria2 rpc @@ -44,8 +43,7 @@ func AddURI(ctx context.Context, uri string, dstDirPath string) error { if err != nil { return errors.Wrapf(err, "failed to add uri %s", uri) } - // TODO add to task manager - TaskManager.Submit(task.WithCancelCtx(&task.Task[string]{ + downTaskManager.Submit(task.WithCancelCtx(&task.Task[string]{ ID: gid, Name: fmt.Sprintf("download %s to [%s](%s)", uri, account.GetAccount().VirtualPath, dstDirActualPath), Func: func(tsk *task.Task[string]) error { diff --git a/internal/aria2/aria2.go b/internal/aria2/aria2.go index 2f82b533..9955c5a4 100644 --- a/internal/aria2/aria2.go +++ b/internal/aria2/aria2.go @@ -9,7 +9,7 @@ import ( "time" ) -var TaskManager = task.NewTaskManager[string](3) +var downTaskManager = task.NewTaskManager[string](3) var notify = NewNotify() var client rpc.Client diff --git a/internal/driver/error.go b/internal/errs/errors.go similarity index 63% rename from internal/driver/error.go rename to internal/errs/errors.go index 377eb21c..5d95822c 100644 --- a/internal/driver/error.go +++ b/internal/errs/errors.go @@ -1,8 +1,7 @@ -package driver +package errs import ( "errors" - pkgerr "github.com/pkg/errors" ) @@ -11,6 +10,10 @@ var ( ErrNotImplement = errors.New("not implement") ErrNotSupport = errors.New("not support") ErrRelativePath = errors.New("access using relative path is not allowed") + + ErrMoveBetweenTwoAccounts = errors.New("can't move files between two account, try to copy") + ErrUploadNotSupported = errors.New("upload not supported") + ErrNotFolder = errors.New("not a folder") ) func IsErrObjectNotFound(err error) bool { diff --git a/internal/fs/errors.go b/internal/fs/errors.go deleted file mode 100644 index 4b4a5f35..00000000 --- a/internal/fs/errors.go +++ /dev/null @@ -1,9 +0,0 @@ -package fs - -import "errors" - -var ( - ErrMoveBetweenTwoAccounts = errors.New("can't move files between two account, try to copy") - ErrUploadNotSupported = errors.New("upload not supported") - ErrNotFolder = errors.New("not a folder") -) diff --git a/internal/fs/put.go b/internal/fs/put.go index 38e6634b..0416f6ba 100644 --- a/internal/fs/put.go +++ b/internal/fs/put.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "github.com/alist-org/alist/v3/internal/driver" + "github.com/alist-org/alist/v3/internal/errs" "github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/internal/operations" "github.com/alist-org/alist/v3/pkg/task" @@ -19,7 +20,7 @@ var UploadTaskManager = task.NewTaskManager[uint64](3, func(tid *uint64) { func Put(ctx context.Context, account driver.Driver, dstDirPath string, file model.FileStreamer) error { account, dstDirActualPath, err := operations.GetAccountAndActualPath(dstDirPath) if account.Config().NoUpload { - return errors.WithStack(ErrUploadNotSupported) + return errors.WithStack(errs.ErrUploadNotSupported) } if err != nil { return errors.WithMessage(err, "failed get account") diff --git a/internal/fs/write.go b/internal/fs/write.go index 6e060dd7..32ce2696 100644 --- a/internal/fs/write.go +++ b/internal/fs/write.go @@ -3,6 +3,7 @@ package fs import ( "context" "github.com/alist-org/alist/v3/internal/driver" + "github.com/alist-org/alist/v3/internal/errs" "github.com/alist-org/alist/v3/internal/operations" "github.com/pkg/errors" ) @@ -25,7 +26,7 @@ func Move(ctx context.Context, account driver.Driver, srcPath, dstDirPath string return errors.WithMessage(err, "failed get dst account") } if srcAccount.GetAccount() != dstAccount.GetAccount() { - return errors.WithStack(ErrMoveBetweenTwoAccounts) + return errors.WithStack(errs.ErrMoveBetweenTwoAccounts) } return operations.Move(ctx, account, srcActualPath, dstDirActualPath) } diff --git a/internal/operations/error.go b/internal/operations/error.go deleted file mode 100644 index 4c3a78ac..00000000 --- a/internal/operations/error.go +++ /dev/null @@ -1,3 +0,0 @@ -package operations - -var () diff --git a/internal/operations/fs.go b/internal/operations/fs.go index 39d2a96b..8271eac1 100644 --- a/internal/operations/fs.go +++ b/internal/operations/fs.go @@ -2,6 +2,7 @@ package operations import ( "context" + "github.com/alist-org/alist/v3/internal/errs" stdpath "path" "time" @@ -85,7 +86,7 @@ func Get(ctx context.Context, account driver.Driver, path string) (model.Obj, er return f, nil } } - return nil, errors.WithStack(driver.ErrorObjectNotFound) + return nil, errors.WithStack(errs.ErrorObjectNotFound) } var linkCache = cache.NewMemCache(cache.WithShards[*model.Link](16)) @@ -102,6 +103,9 @@ func Link(ctx context.Context, account driver.Driver, path string, args model.Li if err != nil { return nil, errors.WithMessage(err, "failed to get file") } + if file.IsDir() { + return nil, errors.New("file is dir") + } link, err := account.Link(ctx, file, args) if err != nil { return nil, errors.WithMessage(err, "failed get link") @@ -119,7 +123,7 @@ func MakeDir(ctx context.Context, account driver.Driver, path string) error { // check if dir exists f, err := Get(ctx, account, path) if err != nil { - if driver.IsErrObjectNotFound(err) { + if errs.IsErrObjectNotFound(err) { parentPath, dirName := stdpath.Split(path) err = MakeDir(ctx, account, parentPath) if err != nil { @@ -179,7 +183,7 @@ func Remove(ctx context.Context, account driver.Driver, path string) error { obj, err := Get(ctx, account, path) if err != nil { // if object not found, it's ok - if driver.IsErrObjectNotFound(err) { + if errs.IsErrObjectNotFound(err) { return nil } return errors.WithMessage(err, "failed to get object")