mirror of
https://github.com/octoleo/restic.git
synced 2024-11-26 14:56:29 +00:00
Merge pull request #3798 from greatroar/errors
all: Move away from pkg/errors, easy cases
This commit is contained in:
commit
2c893fe43c
@ -144,7 +144,7 @@ func init() {
|
||||
func filterExisting(items []string) (result []string, err error) {
|
||||
for _, item := range items {
|
||||
_, err := fs.Lstat(item)
|
||||
if err != nil && os.IsNotExist(errors.Cause(err)) {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
Warnf("%v does not exist, skipping\n", item)
|
||||
continue
|
||||
}
|
||||
|
@ -116,7 +116,7 @@ func runMount(opts MountOptions, gopts GlobalOptions, args []string) error {
|
||||
|
||||
mountpoint := args[0]
|
||||
|
||||
if _, err := resticfs.Stat(mountpoint); os.IsNotExist(errors.Cause(err)) {
|
||||
if _, err := resticfs.Stat(mountpoint); errors.Is(err, os.ErrNotExist) {
|
||||
Verbosef("Mountpoint %s doesn't exist\n", mountpoint)
|
||||
return err
|
||||
}
|
||||
|
@ -297,7 +297,7 @@ func resolvePassword(opts GlobalOptions, envStr string) (string, error) {
|
||||
}
|
||||
if opts.PasswordFile != "" {
|
||||
s, err := textfile.Read(opts.PasswordFile)
|
||||
if os.IsNotExist(errors.Cause(err)) {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return "", errors.Fatalf("%s does not exist", opts.PasswordFile)
|
||||
}
|
||||
return strings.TrimSpace(string(s)), errors.Wrap(err, "Readfile")
|
||||
@ -398,7 +398,7 @@ func ReadRepo(opts GlobalOptions) (string, error) {
|
||||
}
|
||||
|
||||
s, err := textfile.Read(opts.RepositoryFile)
|
||||
if os.IsNotExist(errors.Cause(err)) {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return "", errors.Fatalf("%s does not exist", opts.RepositoryFile)
|
||||
}
|
||||
if err != nil {
|
||||
|
@ -98,7 +98,7 @@ func main() {
|
||||
err := cmdRoot.Execute()
|
||||
|
||||
switch {
|
||||
case restic.IsAlreadyLocked(errors.Cause(err)):
|
||||
case restic.IsAlreadyLocked(err):
|
||||
fmt.Fprintf(os.Stderr, "%v\nthe `unlock` command can be used to remove stale locks\n", err)
|
||||
case err == ErrInvalidSourceData:
|
||||
fmt.Fprintf(os.Stderr, "Warning: %v\n", err)
|
||||
|
@ -2040,8 +2040,8 @@ func TestArchiverAbortEarlyOnError(t *testing.T) {
|
||||
})
|
||||
|
||||
_, _, err := arch.Snapshot(ctx, []string{"."}, SnapshotOptions{Time: time.Now()})
|
||||
if errors.Cause(err) != test.err {
|
||||
t.Errorf("expected error (%v) not found, got %v", test.err, errors.Cause(err))
|
||||
if !errors.Is(err, test.err) {
|
||||
t.Errorf("expected error (%v) not found, got %v", test.err, err)
|
||||
}
|
||||
|
||||
t.Logf("Snapshot return error: %v", err)
|
||||
|
@ -165,7 +165,7 @@ func (s *FileSaver) saveFile(ctx context.Context, chnker *chunker.Chunker, snPat
|
||||
for {
|
||||
buf := s.saveFilePool.Get()
|
||||
chunk, err := chnker.Next(buf.Data)
|
||||
if errors.Cause(err) == io.EOF {
|
||||
if err == io.EOF {
|
||||
buf.Release()
|
||||
break
|
||||
}
|
||||
|
@ -152,7 +152,7 @@ func ParseLayout(ctx context.Context, repo Filesystem, layout, defaultLayout, pa
|
||||
l, err = DetectLayout(ctx, repo, path)
|
||||
|
||||
// use the default layout if auto detection failed
|
||||
if errors.Cause(err) == ErrLayoutDetectionFailed && defaultLayout != "" {
|
||||
if errors.Is(err, ErrLayoutDetectionFailed) && defaultLayout != "" {
|
||||
debug.Log("error: %v, use default layout %v", err, defaultLayout)
|
||||
return ParseLayout(ctx, repo, defaultLayout, "", path)
|
||||
}
|
||||
|
@ -71,7 +71,7 @@ func (be *MemoryBackend) Test(ctx context.Context, h restic.Handle) (bool, error
|
||||
|
||||
// IsNotExist returns true if the file does not exist.
|
||||
func (be *MemoryBackend) IsNotExist(err error) bool {
|
||||
return errors.Cause(err) == errNotFound
|
||||
return errors.Is(err, errNotFound)
|
||||
}
|
||||
|
||||
// Save adds new Data to the backend.
|
||||
|
@ -29,7 +29,8 @@ func newTestSuite(t testing.TB) *test.Suite {
|
||||
t.Logf("Create()")
|
||||
cfg := config.(rclone.Config)
|
||||
be, err := rclone.Create(context.TODO(), cfg)
|
||||
if e, ok := errors.Cause(err).(*exec.Error); ok && e.Err == exec.ErrNotFound {
|
||||
var e *exec.Error
|
||||
if errors.As(err, &e) && e.Err == exec.ErrNotFound {
|
||||
t.Skipf("program %q not found", e.Name)
|
||||
return nil, nil
|
||||
}
|
||||
|
@ -18,7 +18,8 @@ func TestRcloneExit(t *testing.T) {
|
||||
cfg := NewConfig()
|
||||
cfg.Remote = dir
|
||||
be, err := Open(cfg, nil)
|
||||
if e, ok := errors.Cause(err).(*exec.Error); ok && e.Err == exec.ErrNotFound {
|
||||
var e *exec.Error
|
||||
if errors.As(err, &e) && e.Err == exec.ErrNotFound {
|
||||
t.Skipf("program %q not found", e.Name)
|
||||
return
|
||||
}
|
||||
|
@ -169,21 +169,20 @@ func (b *Backend) Save(ctx context.Context, h restic.Handle, rd restic.RewindRea
|
||||
return errors.Wrap(cerr, "Close")
|
||||
}
|
||||
|
||||
// ErrIsNotExist is returned whenever the requested file does not exist on the
|
||||
// notExistError is returned whenever the requested file does not exist on the
|
||||
// server.
|
||||
type ErrIsNotExist struct {
|
||||
type notExistError struct {
|
||||
restic.Handle
|
||||
}
|
||||
|
||||
func (e ErrIsNotExist) Error() string {
|
||||
func (e *notExistError) Error() string {
|
||||
return fmt.Sprintf("%v does not exist", e.Handle)
|
||||
}
|
||||
|
||||
// IsNotExist returns true if the error was caused by a non-existing file.
|
||||
func (b *Backend) IsNotExist(err error) bool {
|
||||
err = errors.Cause(err)
|
||||
_, ok := err.(ErrIsNotExist)
|
||||
return ok
|
||||
var e *notExistError
|
||||
return errors.As(err, &e)
|
||||
}
|
||||
|
||||
// Load runs fn with a reader that yields the contents of the file at h at the
|
||||
@ -296,7 +295,7 @@ func (b *Backend) openReader(ctx context.Context, h restic.Handle, length int, o
|
||||
|
||||
if resp.StatusCode == http.StatusNotFound {
|
||||
_ = resp.Body.Close()
|
||||
return nil, ErrIsNotExist{h}
|
||||
return nil, ¬ExistError{h}
|
||||
}
|
||||
|
||||
if resp.StatusCode != 200 && resp.StatusCode != 206 {
|
||||
@ -341,7 +340,7 @@ func (b *Backend) Stat(ctx context.Context, h restic.Handle) (restic.FileInfo, e
|
||||
|
||||
if resp.StatusCode == http.StatusNotFound {
|
||||
_ = resp.Body.Close()
|
||||
return restic.FileInfo{}, ErrIsNotExist{h}
|
||||
return restic.FileInfo{}, ¬ExistError{h}
|
||||
}
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
@ -392,7 +391,7 @@ func (b *Backend) Remove(ctx context.Context, h restic.Handle) error {
|
||||
|
||||
if resp.StatusCode == http.StatusNotFound {
|
||||
_ = resp.Body.Close()
|
||||
return ErrIsNotExist{h}
|
||||
return ¬ExistError{h}
|
||||
}
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
|
@ -137,7 +137,7 @@ func Create(ctx context.Context, cfg Config, rt http.RoundTripper) (restic.Backe
|
||||
}
|
||||
found, err := be.client.BucketExists(ctx, cfg.Bucket)
|
||||
|
||||
if err != nil && be.IsAccessDenied(err) {
|
||||
if err != nil && isAccessDenied(err) {
|
||||
err = nil
|
||||
found = true
|
||||
}
|
||||
@ -158,29 +158,23 @@ func Create(ctx context.Context, cfg Config, rt http.RoundTripper) (restic.Backe
|
||||
return be, nil
|
||||
}
|
||||
|
||||
// IsAccessDenied returns true if the error is caused by Access Denied.
|
||||
func (be *Backend) IsAccessDenied(err error) bool {
|
||||
debug.Log("IsAccessDenied(%T, %#v)", err, err)
|
||||
// isAccessDenied returns true if the error is caused by Access Denied.
|
||||
func isAccessDenied(err error) bool {
|
||||
debug.Log("isAccessDenied(%T, %#v)", err, err)
|
||||
|
||||
if e, ok := errors.Cause(err).(minio.ErrorResponse); ok && e.Code == "AccessDenied" {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
var e minio.ErrorResponse
|
||||
return errors.As(err, &e) && e.Code == "Access Denied"
|
||||
}
|
||||
|
||||
// IsNotExist returns true if the error is caused by a not existing file.
|
||||
func (be *Backend) IsNotExist(err error) bool {
|
||||
debug.Log("IsNotExist(%T, %#v)", err, err)
|
||||
if os.IsNotExist(errors.Cause(err)) {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return true
|
||||
}
|
||||
|
||||
if e, ok := errors.Cause(err).(minio.ErrorResponse); ok && e.Code == "NoSuchKey" {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
var e minio.ErrorResponse
|
||||
return errors.As(err, &e) && e.Code == "NoSuchKey"
|
||||
}
|
||||
|
||||
// Join combines path components with slashes.
|
||||
|
@ -183,7 +183,6 @@ func (r *SFTP) ReadDir(ctx context.Context, dir string) ([]os.FileInfo, error) {
|
||||
|
||||
// IsNotExist returns true if the error is caused by a not existing file.
|
||||
func (r *SFTP) IsNotExist(err error) bool {
|
||||
err = errors.Cause(err)
|
||||
return errors.Is(err, os.ErrNotExist)
|
||||
}
|
||||
|
||||
@ -496,7 +495,7 @@ func (r *SFTP) Test(ctx context.Context, h restic.Handle) (bool, error) {
|
||||
defer r.sem.ReleaseToken()
|
||||
|
||||
_, err := r.c.Lstat(r.Filename(h))
|
||||
if os.IsNotExist(errors.Cause(err)) {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
|
@ -20,7 +20,7 @@ func findSFTPServerBinary() string {
|
||||
for _, dir := range strings.Split(rtest.TestSFTPPath, ":") {
|
||||
testpath := filepath.Join(dir, "sftp-server")
|
||||
_, err := os.Stat(testpath)
|
||||
if !os.IsNotExist(errors.Cause(err)) {
|
||||
if !errors.Is(err, os.ErrNotExist) {
|
||||
return testpath
|
||||
}
|
||||
}
|
||||
|
@ -311,11 +311,8 @@ func (be *beSwift) removeKeys(ctx context.Context, t restic.FileType) error {
|
||||
|
||||
// IsNotExist returns true if the error is caused by a not existing file.
|
||||
func (be *beSwift) IsNotExist(err error) bool {
|
||||
if e, ok := errors.Cause(err).(*swift.Error); ok {
|
||||
return e.StatusCode == http.StatusNotFound
|
||||
}
|
||||
|
||||
return false
|
||||
var e *swift.Error
|
||||
return errors.As(err, &e) && e.StatusCode == http.StatusNotFound
|
||||
}
|
||||
|
||||
// Delete removes all restic objects in the container.
|
||||
|
@ -361,8 +361,8 @@ func (s *Suite) TestListCancel(t *testing.T) {
|
||||
return nil
|
||||
})
|
||||
|
||||
if errors.Cause(err) != context.Canceled {
|
||||
t.Fatalf("expected error not found, want %v, got %v", context.Canceled, errors.Cause(err))
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("expected error not found, want %v, got %v", context.Canceled, err)
|
||||
}
|
||||
})
|
||||
|
||||
@ -380,7 +380,7 @@ func (s *Suite) TestListCancel(t *testing.T) {
|
||||
return nil
|
||||
})
|
||||
|
||||
if errors.Cause(err) != context.Canceled {
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("expected error not found, want %v, got %v", context.Canceled, err)
|
||||
}
|
||||
|
||||
@ -403,7 +403,7 @@ func (s *Suite) TestListCancel(t *testing.T) {
|
||||
return nil
|
||||
})
|
||||
|
||||
if errors.Cause(err) != context.Canceled {
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("expected error not found, want %v, got %v", context.Canceled, err)
|
||||
}
|
||||
|
||||
@ -429,7 +429,7 @@ func (s *Suite) TestListCancel(t *testing.T) {
|
||||
return nil
|
||||
})
|
||||
|
||||
if errors.Cause(err) != context.DeadlineExceeded {
|
||||
if !errors.Is(err, context.DeadlineExceeded) {
|
||||
t.Fatalf("expected error not found, want %#v, got %#v", context.DeadlineExceeded, err)
|
||||
}
|
||||
|
||||
|
@ -160,18 +160,15 @@ type PackError struct {
|
||||
Err error
|
||||
}
|
||||
|
||||
func (e PackError) Error() string {
|
||||
func (e *PackError) Error() string {
|
||||
return "pack " + e.ID.Str() + ": " + e.Err.Error()
|
||||
}
|
||||
|
||||
// IsOrphanedPack returns true if the error describes a pack which is not
|
||||
// contained in any index.
|
||||
func IsOrphanedPack(err error) bool {
|
||||
if e, ok := errors.Cause(err).(PackError); ok && e.Orphaned {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
var e *PackError
|
||||
return errors.As(err, &e) && e.Orphaned
|
||||
}
|
||||
|
||||
// Packs checks that all packs referenced in the index are still available and
|
||||
@ -204,7 +201,7 @@ func (c *Checker) Packs(ctx context.Context, errChan chan<- error) {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case errChan <- PackError{ID: id, Err: errors.New("does not exist")}:
|
||||
case errChan <- &PackError{ID: id, Err: errors.New("does not exist")}:
|
||||
}
|
||||
continue
|
||||
}
|
||||
@ -214,7 +211,7 @@ func (c *Checker) Packs(ctx context.Context, errChan chan<- error) {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case errChan <- PackError{ID: id, Err: errors.Errorf("unexpected file size: got %d, expected %d", reposize, size)}:
|
||||
case errChan <- &PackError{ID: id, Err: errors.Errorf("unexpected file size: got %d, expected %d", reposize, size)}:
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -224,7 +221,7 @@ func (c *Checker) Packs(ctx context.Context, errChan chan<- error) {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case errChan <- PackError{ID: orphanID, Orphaned: true, Err: errors.New("not referenced in any index")}:
|
||||
case errChan <- &PackError{ID: orphanID, Orphaned: true, Err: errors.New("not referenced in any index")}:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -109,7 +109,7 @@ func TestMissingPack(t *testing.T) {
|
||||
test.Assert(t, len(errs) == 1,
|
||||
"expected exactly one error, got %v", len(errs))
|
||||
|
||||
if err, ok := errs[0].(checker.PackError); ok {
|
||||
if err, ok := errs[0].(*checker.PackError); ok {
|
||||
test.Equals(t, packHandle.Name, err.ID.String())
|
||||
} else {
|
||||
t.Errorf("expected error returned by checker.Packs() to be PackError, got %v", err)
|
||||
@ -145,7 +145,7 @@ func TestUnreferencedPack(t *testing.T) {
|
||||
test.Assert(t, len(errs) == 1,
|
||||
"expected exactly one error, got %v", len(errs))
|
||||
|
||||
if err, ok := errs[0].(checker.PackError); ok {
|
||||
if err, ok := errs[0].(*checker.PackError); ok {
|
||||
test.Equals(t, packID, err.ID.String())
|
||||
} else {
|
||||
t.Errorf("expected error returned by checker.Packs() to be PackError, got %v", err)
|
||||
|
@ -52,4 +52,6 @@ func Cause(err error) error {
|
||||
|
||||
// Go 1.13-style error handling.
|
||||
|
||||
func As(err error, tgt interface{}) bool { return errors.As(err, tgt) }
|
||||
|
||||
func Is(x, y error) bool { return errors.Is(x, y) }
|
||||
|
@ -154,7 +154,7 @@ func SearchKey(ctx context.Context, s *Repository, password string, maxKeys int,
|
||||
debug.Log("key %v returned error %v", fi.Name, err)
|
||||
|
||||
// ErrUnauthenticated means the password is wrong, try the next key
|
||||
if errors.Cause(err) == crypto.ErrUnauthenticated {
|
||||
if errors.Is(err, crypto.ErrUnauthenticated) {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -38,13 +38,13 @@ type Lock struct {
|
||||
lockID *ID
|
||||
}
|
||||
|
||||
// ErrAlreadyLocked is returned when NewLock or NewExclusiveLock are unable to
|
||||
// alreadyLockedError is returned when NewLock or NewExclusiveLock are unable to
|
||||
// acquire the desired lock.
|
||||
type ErrAlreadyLocked struct {
|
||||
type alreadyLockedError struct {
|
||||
otherLock *Lock
|
||||
}
|
||||
|
||||
func (e ErrAlreadyLocked) Error() string {
|
||||
func (e *alreadyLockedError) Error() string {
|
||||
s := ""
|
||||
if e.otherLock.Exclusive {
|
||||
s = "exclusively "
|
||||
@ -52,25 +52,23 @@ func (e ErrAlreadyLocked) Error() string {
|
||||
return fmt.Sprintf("repository is already locked %sby %v", s, e.otherLock)
|
||||
}
|
||||
|
||||
// IsAlreadyLocked returns true iff err is an instance of ErrAlreadyLocked.
|
||||
// IsAlreadyLocked returns true iff err indicates that a repository is
|
||||
// already locked.
|
||||
func IsAlreadyLocked(err error) bool {
|
||||
if _, ok := errors.Cause(err).(ErrAlreadyLocked); ok {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
var e *alreadyLockedError
|
||||
return errors.As(err, &e)
|
||||
}
|
||||
|
||||
// NewLock returns a new, non-exclusive lock for the repository. If an
|
||||
// exclusive lock is already held by another process, ErrAlreadyLocked is
|
||||
// returned.
|
||||
// exclusive lock is already held by another process, it returns an error
|
||||
// that satisfies IsAlreadyLocked.
|
||||
func NewLock(ctx context.Context, repo Repository) (*Lock, error) {
|
||||
return newLock(ctx, repo, false)
|
||||
}
|
||||
|
||||
// NewExclusiveLock returns a new, exclusive lock for the repository. If
|
||||
// another lock (normal and exclusive) is already held by another process,
|
||||
// ErrAlreadyLocked is returned.
|
||||
// it returns an error that satisfies IsAlreadyLocked.
|
||||
func NewExclusiveLock(ctx context.Context, repo Repository) (*Lock, error) {
|
||||
return newLock(ctx, repo, true)
|
||||
}
|
||||
@ -147,11 +145,11 @@ func (l *Lock) checkForOtherLocks(ctx context.Context) error {
|
||||
}
|
||||
|
||||
if l.Exclusive {
|
||||
return ErrAlreadyLocked{otherLock: lock}
|
||||
return &alreadyLockedError{otherLock: lock}
|
||||
}
|
||||
|
||||
if !l.Exclusive && lock.Exclusive {
|
||||
return ErrAlreadyLocked{otherLock: lock}
|
||||
return &alreadyLockedError{otherLock: lock}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -9,8 +9,6 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/restic/restic/internal/errors"
|
||||
|
||||
"github.com/restic/chunker"
|
||||
)
|
||||
|
||||
@ -44,7 +42,7 @@ func (fs *fakeFileSystem) saveFile(ctx context.Context, rd io.Reader) (blobs IDs
|
||||
blobs = IDs{}
|
||||
for {
|
||||
chunk, err := fs.chunker.Next(fs.buf)
|
||||
if errors.Cause(err) == io.EOF {
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
|
||||
|
@ -175,7 +175,7 @@ func ResetReadOnly(t testing.TB, dir string) {
|
||||
|
||||
return nil
|
||||
})
|
||||
if os.IsNotExist(errors.Cause(err)) {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
err = nil
|
||||
}
|
||||
OK(t, err)
|
||||
@ -186,7 +186,7 @@ func ResetReadOnly(t testing.TB, dir string) {
|
||||
func RemoveAll(t testing.TB, path string) {
|
||||
ResetReadOnly(t, path)
|
||||
err := os.RemoveAll(path)
|
||||
if os.IsNotExist(errors.Cause(err)) {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
err = nil
|
||||
}
|
||||
OK(t, err)
|
||||
|
Loading…
Reference in New Issue
Block a user