chore: just use std errors in drivers

This commit is contained in:
Noah Hsu
2022-08-31 20:58:57 +08:00
parent 817d63597e
commit 9ec6d5be7a
11 changed files with 49 additions and 52 deletions

View File

@ -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)

View File

@ -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" {

View File

@ -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
}

View File

@ -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()
}

View File

@ -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" {

View File

@ -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()
}

View File

@ -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

View File

@ -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)

View File

@ -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
}