Remove github.com/pkg/errors and replace with std library version

This is possible now that we no longer support go1.12 and brings
rclone into line with standard practices in the Go world.

This also removes errors.New and errors.Errorf from lib/errors and
prefers the stdlib errors package over lib/errors.
This commit is contained in:
Nick Craig-Wood
2021-11-04 10:12:57 +00:00
parent 97328e5755
commit e43b5ce5e5
233 changed files with 1673 additions and 1695 deletions

View File

@@ -74,6 +74,7 @@ Which is control chars + [' ', '*', '.', '/', ':', '<', '>', '?', '|']
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
@@ -83,7 +84,6 @@ import (
"strings"
"time"
"github.com/pkg/errors"
"github.com/rclone/rclone/backend/sharefile/api"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/config"
@@ -144,7 +144,7 @@ func init() {
subdomain := auth.Form.Get("subdomain")
apicp := auth.Form.Get("apicp")
if subdomain == "" || apicp == "" {
return errors.Errorf("subdomain or apicp not found in response: %+v", auth.Form)
return fmt.Errorf("subdomain or apicp not found in response: %+v", auth.Form)
}
endpoint := "https://" + subdomain + "." + apicp
m.Set("endpoint", endpoint)
@@ -334,7 +334,7 @@ func (f *Fs) readMetaDataForIDPath(ctx context.Context, id, path string, directo
}
return nil, fs.ErrorDirNotFound
}
return nil, errors.Wrap(err, "couldn't find item")
return nil, fmt.Errorf("couldn't find item: %w", err)
}
if directoriesOnly && item.Type != api.ItemTypeFolder {
return nil, fs.ErrorIsFile
@@ -386,10 +386,10 @@ func errorHandler(resp *http.Response) error {
func checkUploadChunkSize(cs fs.SizeSuffix) error {
if cs < minChunkSize {
return errors.Errorf("ChunkSize: %s is less than %s", cs, minChunkSize)
return fmt.Errorf("ChunkSize: %s is less than %s", cs, minChunkSize)
}
if cs > maxChunkSize {
return errors.Errorf("ChunkSize: %s is greater than %s", cs, maxChunkSize)
return fmt.Errorf("ChunkSize: %s is greater than %s", cs, maxChunkSize)
}
return nil
}
@@ -444,7 +444,7 @@ func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, e
var ts *oauthutil.TokenSource
client, ts, err = oauthutil.NewClient(ctx, name, m, oauthConfig)
if err != nil {
return nil, errors.Wrap(err, "failed to configure sharefile")
return nil, fmt.Errorf("failed to configure sharefile: %w", err)
}
ci := fs.GetConfig(ctx)
@@ -477,23 +477,23 @@ func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, e
const serverTimezone = "America/New_York"
timezone, err := tzdata.Open(serverTimezone)
if err != nil {
return nil, errors.Wrap(err, "failed to open timezone db")
return nil, fmt.Errorf("failed to open timezone db: %w", err)
}
tzdata, err := ioutil.ReadAll(timezone)
if err != nil {
return nil, errors.Wrap(err, "failed to read timezone")
return nil, fmt.Errorf("failed to read timezone: %w", err)
}
_ = timezone.Close()
f.location, err = time.LoadLocationFromTZData(serverTimezone, tzdata)
if err != nil {
return nil, errors.Wrap(err, "failed to load location from timezone")
return nil, fmt.Errorf("failed to load location from timezone: %w", err)
}
// Find ID of user's root folder
if opt.RootFolderID == "" {
item, err := f.readMetaDataForID(ctx, opt.RootFolderID, true, false)
if err != nil {
return nil, errors.Wrap(err, "couldn't find root ID")
return nil, fmt.Errorf("couldn't find root ID: %w", err)
}
f.rootID = item.ID
} else {
@@ -639,7 +639,7 @@ func (f *Fs) CreateDir(ctx context.Context, pathID, leaf string) (newID string,
return shouldRetry(ctx, resp, err)
})
if err != nil {
return "", errors.Wrap(err, "CreateDir")
return "", fmt.Errorf("CreateDir: %w", err)
}
return info.ID, nil
}
@@ -671,7 +671,7 @@ func (f *Fs) listAll(ctx context.Context, dirID string, directoriesOnly bool, fi
return shouldRetry(ctx, resp, err)
})
if err != nil {
return found, errors.Wrap(err, "couldn't list files")
return found, fmt.Errorf("couldn't list files: %w", err)
}
for i := range result.Value {
item := &result.Value[i]
@@ -825,7 +825,7 @@ func (f *Fs) purgeCheck(ctx context.Context, dir string, check bool) error {
return true
})
if err != nil {
return errors.Wrap(err, "purgeCheck")
return fmt.Errorf("purgeCheck: %w", err)
}
if found {
return fs.ErrorDirectoryNotEmpty
@@ -900,7 +900,7 @@ func (f *Fs) updateItem(ctx context.Context, id, leaf, directoryID string, modTi
// Parse it back into a time
newModTime, err := time.Parse(time.RFC3339Nano, isoTime)
if err != nil {
return nil, errors.Wrap(err, "updateItem: time parse")
return nil, fmt.Errorf("updateItem: time parse: %w", err)
}
modTime = &newModTime
}
@@ -934,7 +934,7 @@ func (f *Fs) move(ctx context.Context, isFile bool, id, oldLeaf, newLeaf, oldDir
// To demonstrate bug
// item, err = f.updateItem(ctx, id, newLeaf, newDirectoryID, nil)
// if err != nil {
// return nil, errors.Wrap(err, "Move rename leaf")
// return nil, fmt.Errorf("Move rename leaf: %w", err)
// }
// return item, nil
doRenameLeaf := oldLeaf != newLeaf
@@ -947,7 +947,7 @@ func (f *Fs) move(ctx context.Context, isFile bool, id, oldLeaf, newLeaf, oldDir
tmpLeaf := newLeaf + "." + random.String(8)
item, err = f.updateItem(ctx, id, tmpLeaf, "", nil)
if err != nil {
return nil, errors.Wrap(err, "Move rename leaf")
return nil, fmt.Errorf("Move rename leaf: %w", err)
}
}
@@ -956,7 +956,7 @@ func (f *Fs) move(ctx context.Context, isFile bool, id, oldLeaf, newLeaf, oldDir
if doMove {
item, err = f.updateItem(ctx, id, "", newDirectoryID, nil)
if err != nil {
return nil, errors.Wrap(err, "Move directory")
return nil, fmt.Errorf("Move directory: %w", err)
}
}
@@ -964,7 +964,7 @@ func (f *Fs) move(ctx context.Context, isFile bool, id, oldLeaf, newLeaf, oldDir
if doRenameLeaf {
item, err = f.updateItem(ctx, id, newLeaf, "", nil)
if err != nil {
return nil, errors.Wrap(err, "Move rename leaf")
return nil, fmt.Errorf("Move rename leaf: %w", err)
}
}
@@ -1079,7 +1079,7 @@ func (f *Fs) Copy(ctx context.Context, src fs.Object, remote string) (dst fs.Obj
sameName := strings.ToLower(srcLeaf) == strings.ToLower(dstLeaf)
if sameName && srcParentID == dstParentID {
return nil, errors.Errorf("copy: can't copy to a file in the same directory whose name only differs in case: %q vs %q", srcLeaf, dstLeaf)
return nil, fmt.Errorf("copy: can't copy to a file in the same directory whose name only differs in case: %q vs %q", srcLeaf, dstLeaf)
}
// Discover whether we can just copy directly or not
@@ -1095,7 +1095,7 @@ func (f *Fs) Copy(ctx context.Context, src fs.Object, remote string) (dst fs.Obj
if err == fs.ErrorObjectNotFound || err == fs.ErrorDirNotFound {
directCopy = true
} else if err != nil {
return nil, errors.Wrap(err, "copy: failed to examine destination dir")
return nil, fmt.Errorf("copy: failed to examine destination dir: %w", err)
} else {
// otherwise need to copy via a temporary directory
}
@@ -1109,17 +1109,17 @@ func (f *Fs) Copy(ctx context.Context, src fs.Object, remote string) (dst fs.Obj
tmpDir := "rclone-temp-dir-" + random.String(16)
err = f.Mkdir(ctx, tmpDir)
if err != nil {
return nil, errors.Wrap(err, "copy: failed to make temp dir")
return nil, fmt.Errorf("copy: failed to make temp dir: %w", err)
}
defer func() {
rmdirErr := f.Rmdir(ctx, tmpDir)
if rmdirErr != nil && err == nil {
err = errors.Wrap(rmdirErr, "copy: failed to remove temp dir")
err = fmt.Errorf("copy: failed to remove temp dir: %w", rmdirErr)
}
}()
tmpDirID, err := f.dirCache.FindDir(ctx, tmpDir, false)
if err != nil {
return nil, errors.Wrap(err, "copy: failed to find temp dir")
return nil, fmt.Errorf("copy: failed to find temp dir: %w", err)
}
copyTargetDirID = tmpDirID
}
@@ -1221,7 +1221,7 @@ func (o *Object) Size() int64 {
// setMetaData sets the metadata from info
func (o *Object) setMetaData(info *api.Item) (err error) {
if info.Type != api.ItemTypeFile {
return errors.Wrapf(fs.ErrorNotAFile, "%q is %q", o.remote, info.Type)
return fmt.Errorf("%q is %q: %w", o.remote, info.Type, fs.ErrorNotAFile)
}
o.hasMetaData = true
o.size = info.Size
@@ -1302,7 +1302,7 @@ func (o *Object) Open(ctx context.Context, options ...fs.OpenOption) (in io.Read
return shouldRetry(ctx, resp, err)
})
if err != nil {
return nil, errors.Wrap(err, "open: fetch download specification")
return nil, fmt.Errorf("open: fetch download specification: %w", err)
}
fs.FixRangeOption(options, o.size)
@@ -1317,7 +1317,7 @@ func (o *Object) Open(ctx context.Context, options ...fs.OpenOption) (in io.Read
return shouldRetry(ctx, resp, err)
})
if err != nil {
return nil, errors.Wrap(err, "open")
return nil, fmt.Errorf("open: %w", err)
}
return resp.Body, err
}
@@ -1373,7 +1373,7 @@ func (o *Object) Update(ctx context.Context, in io.Reader, src fs.ObjectInfo, op
return shouldRetry(ctx, resp, err)
})
if err != nil {
return errors.Wrap(err, "upload get specification")
return fmt.Errorf("upload get specification: %w", err)
}
// If file is large then upload in parts
@@ -1398,7 +1398,7 @@ func (o *Object) Update(ctx context.Context, in io.Reader, src fs.ObjectInfo, op
return shouldRetry(ctx, resp, err)
})
if err != nil {
return errors.Wrap(err, "upload file")
return fmt.Errorf("upload file: %w", err)
}
return o.checkUploadResponse(ctx, &finish)
}
@@ -1434,7 +1434,7 @@ func (f *Fs) remove(ctx context.Context, id string) (err error) {
return shouldRetry(ctx, resp, err)
})
if err != nil {
return errors.Wrap(err, "remove")
return fmt.Errorf("remove: %w", err)
}
return nil
}
@@ -1443,7 +1443,7 @@ func (f *Fs) remove(ctx context.Context, id string) (err error) {
func (o *Object) Remove(ctx context.Context) error {
err := o.readMetaData(ctx)
if err != nil {
return errors.Wrap(err, "Remove: Failed to read metadata")
return fmt.Errorf("Remove: Failed to read metadata: %w", err)
}
return o.fs.remove(ctx, o.id)
}