2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-22 19:08:55 +00:00

all: Move away from pkg/errors, easy cases

github.com/pkg/errors is no longer getting updates, because Go 1.13
went with the more flexible errors.{As,Is} function. Use those instead:
errors from pkg/errors already support the Unwrap interface used by 1.13
error handling. Also:

* check for io.EOF with a straight ==. That value should not be wrapped,
  and the chunker (whose error is checked in the cases changed) does not
  wrap it.
* Give custom Error methods pointer receivers, so there's no ambiguity
  when type-switching since the value type will no longer implement error.
* Make restic.ErrAlreadyLocked private, and rename it to
  alreadyLockedError to match the stdlib convention that error type
  names end in Error.
* Same with rest.ErrIsNotExist => rest.notExistError.
* Make s3.Backend.IsAccessDenied a private function.
This commit is contained in:
greatroar 2022-06-13 20:35:37 +02:00
parent 1dd4b9b60e
commit f92ecf13c9
23 changed files with 66 additions and 80 deletions

View File

@ -144,7 +144,7 @@ func init() {
func filterExisting(items []string) (result []string, err error) { func filterExisting(items []string) (result []string, err error) {
for _, item := range items { for _, item := range items {
_, err := fs.Lstat(item) _, 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) Warnf("%v does not exist, skipping\n", item)
continue continue
} }

View File

@ -116,7 +116,7 @@ func runMount(opts MountOptions, gopts GlobalOptions, args []string) error {
mountpoint := args[0] 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) Verbosef("Mountpoint %s doesn't exist\n", mountpoint)
return err return err
} }

View File

@ -297,7 +297,7 @@ func resolvePassword(opts GlobalOptions, envStr string) (string, error) {
} }
if opts.PasswordFile != "" { if opts.PasswordFile != "" {
s, err := textfile.Read(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 "", errors.Fatalf("%s does not exist", opts.PasswordFile)
} }
return strings.TrimSpace(string(s)), errors.Wrap(err, "Readfile") 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) 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) return "", errors.Fatalf("%s does not exist", opts.RepositoryFile)
} }
if err != nil { if err != nil {

View File

@ -98,7 +98,7 @@ func main() {
err := cmdRoot.Execute() err := cmdRoot.Execute()
switch { 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) fmt.Fprintf(os.Stderr, "%v\nthe `unlock` command can be used to remove stale locks\n", err)
case err == ErrInvalidSourceData: case err == ErrInvalidSourceData:
fmt.Fprintf(os.Stderr, "Warning: %v\n", err) fmt.Fprintf(os.Stderr, "Warning: %v\n", err)

View File

@ -2040,8 +2040,8 @@ func TestArchiverAbortEarlyOnError(t *testing.T) {
}) })
_, _, err := arch.Snapshot(ctx, []string{"."}, SnapshotOptions{Time: time.Now()}) _, _, err := arch.Snapshot(ctx, []string{"."}, SnapshotOptions{Time: time.Now()})
if errors.Cause(err) != test.err { if !errors.Is(err, test.err) {
t.Errorf("expected error (%v) not found, got %v", test.err, errors.Cause(err)) t.Errorf("expected error (%v) not found, got %v", test.err, err)
} }
t.Logf("Snapshot return error: %v", err) t.Logf("Snapshot return error: %v", err)

View File

@ -165,7 +165,7 @@ func (s *FileSaver) saveFile(ctx context.Context, chnker *chunker.Chunker, snPat
for { for {
buf := s.saveFilePool.Get() buf := s.saveFilePool.Get()
chunk, err := chnker.Next(buf.Data) chunk, err := chnker.Next(buf.Data)
if errors.Cause(err) == io.EOF { if err == io.EOF {
buf.Release() buf.Release()
break break
} }

View File

@ -152,7 +152,7 @@ func ParseLayout(ctx context.Context, repo Filesystem, layout, defaultLayout, pa
l, err = DetectLayout(ctx, repo, path) l, err = DetectLayout(ctx, repo, path)
// use the default layout if auto detection failed // 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) debug.Log("error: %v, use default layout %v", err, defaultLayout)
return ParseLayout(ctx, repo, defaultLayout, "", path) return ParseLayout(ctx, repo, defaultLayout, "", path)
} }

View File

@ -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. // IsNotExist returns true if the file does not exist.
func (be *MemoryBackend) IsNotExist(err error) bool { func (be *MemoryBackend) IsNotExist(err error) bool {
return errors.Cause(err) == errNotFound return errors.Is(err, errNotFound)
} }
// Save adds new Data to the backend. // Save adds new Data to the backend.

View File

@ -29,7 +29,8 @@ func newTestSuite(t testing.TB) *test.Suite {
t.Logf("Create()") t.Logf("Create()")
cfg := config.(rclone.Config) cfg := config.(rclone.Config)
be, err := rclone.Create(context.TODO(), cfg) 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) t.Skipf("program %q not found", e.Name)
return nil, nil return nil, nil
} }

View File

@ -18,7 +18,8 @@ func TestRcloneExit(t *testing.T) {
cfg := NewConfig() cfg := NewConfig()
cfg.Remote = dir cfg.Remote = dir
be, err := Open(cfg, nil) 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) t.Skipf("program %q not found", e.Name)
return return
} }

View File

@ -169,21 +169,20 @@ func (b *Backend) Save(ctx context.Context, h restic.Handle, rd restic.RewindRea
return errors.Wrap(cerr, "Close") 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. // server.
type ErrIsNotExist struct { type notExistError struct {
restic.Handle restic.Handle
} }
func (e ErrIsNotExist) Error() string { func (e *notExistError) Error() string {
return fmt.Sprintf("%v does not exist", e.Handle) return fmt.Sprintf("%v does not exist", e.Handle)
} }
// IsNotExist returns true if the error was caused by a non-existing file. // IsNotExist returns true if the error was caused by a non-existing file.
func (b *Backend) IsNotExist(err error) bool { func (b *Backend) IsNotExist(err error) bool {
err = errors.Cause(err) var e *notExistError
_, ok := err.(ErrIsNotExist) return errors.As(err, &e)
return ok
} }
// Load runs fn with a reader that yields the contents of the file at h at the // 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 { if resp.StatusCode == http.StatusNotFound {
_ = resp.Body.Close() _ = resp.Body.Close()
return nil, ErrIsNotExist{h} return nil, &notExistError{h}
} }
if resp.StatusCode != 200 && resp.StatusCode != 206 { 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 { if resp.StatusCode == http.StatusNotFound {
_ = resp.Body.Close() _ = resp.Body.Close()
return restic.FileInfo{}, ErrIsNotExist{h} return restic.FileInfo{}, &notExistError{h}
} }
if resp.StatusCode != 200 { if resp.StatusCode != 200 {
@ -392,7 +391,7 @@ func (b *Backend) Remove(ctx context.Context, h restic.Handle) error {
if resp.StatusCode == http.StatusNotFound { if resp.StatusCode == http.StatusNotFound {
_ = resp.Body.Close() _ = resp.Body.Close()
return ErrIsNotExist{h} return &notExistError{h}
} }
if resp.StatusCode != 200 { if resp.StatusCode != 200 {

View File

@ -137,7 +137,7 @@ func Create(ctx context.Context, cfg Config, rt http.RoundTripper) (restic.Backe
} }
found, err := be.client.BucketExists(ctx, cfg.Bucket) found, err := be.client.BucketExists(ctx, cfg.Bucket)
if err != nil && be.IsAccessDenied(err) { if err != nil && isAccessDenied(err) {
err = nil err = nil
found = true found = true
} }
@ -158,29 +158,23 @@ func Create(ctx context.Context, cfg Config, rt http.RoundTripper) (restic.Backe
return be, nil return be, nil
} }
// IsAccessDenied returns true if the error is caused by Access Denied. // isAccessDenied returns true if the error is caused by Access Denied.
func (be *Backend) IsAccessDenied(err error) bool { func isAccessDenied(err error) bool {
debug.Log("IsAccessDenied(%T, %#v)", err, err) debug.Log("isAccessDenied(%T, %#v)", err, err)
if e, ok := errors.Cause(err).(minio.ErrorResponse); ok && e.Code == "AccessDenied" { var e minio.ErrorResponse
return true return errors.As(err, &e) && e.Code == "Access Denied"
}
return false
} }
// IsNotExist returns true if the error is caused by a not existing file. // IsNotExist returns true if the error is caused by a not existing file.
func (be *Backend) IsNotExist(err error) bool { func (be *Backend) IsNotExist(err error) bool {
debug.Log("IsNotExist(%T, %#v)", err, err) debug.Log("IsNotExist(%T, %#v)", err, err)
if os.IsNotExist(errors.Cause(err)) { if errors.Is(err, os.ErrNotExist) {
return true return true
} }
if e, ok := errors.Cause(err).(minio.ErrorResponse); ok && e.Code == "NoSuchKey" { var e minio.ErrorResponse
return true return errors.As(err, &e) && e.Code == "NoSuchKey"
}
return false
} }
// Join combines path components with slashes. // Join combines path components with slashes.

View File

@ -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. // IsNotExist returns true if the error is caused by a not existing file.
func (r *SFTP) IsNotExist(err error) bool { func (r *SFTP) IsNotExist(err error) bool {
err = errors.Cause(err)
return errors.Is(err, os.ErrNotExist) 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() defer r.sem.ReleaseToken()
_, err := r.c.Lstat(r.Filename(h)) _, err := r.c.Lstat(r.Filename(h))
if os.IsNotExist(errors.Cause(err)) { if errors.Is(err, os.ErrNotExist) {
return false, nil return false, nil
} }

View File

@ -20,7 +20,7 @@ func findSFTPServerBinary() string {
for _, dir := range strings.Split(rtest.TestSFTPPath, ":") { for _, dir := range strings.Split(rtest.TestSFTPPath, ":") {
testpath := filepath.Join(dir, "sftp-server") testpath := filepath.Join(dir, "sftp-server")
_, err := os.Stat(testpath) _, err := os.Stat(testpath)
if !os.IsNotExist(errors.Cause(err)) { if !errors.Is(err, os.ErrNotExist) {
return testpath return testpath
} }
} }

View File

@ -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. // IsNotExist returns true if the error is caused by a not existing file.
func (be *beSwift) IsNotExist(err error) bool { func (be *beSwift) IsNotExist(err error) bool {
if e, ok := errors.Cause(err).(*swift.Error); ok { var e *swift.Error
return e.StatusCode == http.StatusNotFound return errors.As(err, &e) && e.StatusCode == http.StatusNotFound
}
return false
} }
// Delete removes all restic objects in the container. // Delete removes all restic objects in the container.

View File

@ -361,8 +361,8 @@ func (s *Suite) TestListCancel(t *testing.T) {
return nil 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, errors.Cause(err)) 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 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) 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 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) 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 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) t.Fatalf("expected error not found, want %#v, got %#v", context.DeadlineExceeded, err)
} }

View File

@ -160,18 +160,15 @@ type PackError struct {
Err error Err error
} }
func (e PackError) Error() string { func (e *PackError) Error() string {
return "pack " + e.ID.Str() + ": " + e.Err.Error() return "pack " + e.ID.Str() + ": " + e.Err.Error()
} }
// IsOrphanedPack returns true if the error describes a pack which is not // IsOrphanedPack returns true if the error describes a pack which is not
// contained in any index. // contained in any index.
func IsOrphanedPack(err error) bool { func IsOrphanedPack(err error) bool {
if e, ok := errors.Cause(err).(PackError); ok && e.Orphaned { var e *PackError
return true return errors.As(err, &e) && e.Orphaned
}
return false
} }
// Packs checks that all packs referenced in the index are still available and // 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 { select {
case <-ctx.Done(): case <-ctx.Done():
return return
case errChan <- PackError{ID: id, Err: errors.New("does not exist")}: case errChan <- &PackError{ID: id, Err: errors.New("does not exist")}:
} }
continue continue
} }
@ -214,7 +211,7 @@ func (c *Checker) Packs(ctx context.Context, errChan chan<- error) {
select { select {
case <-ctx.Done(): case <-ctx.Done():
return 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 { select {
case <-ctx.Done(): case <-ctx.Done():
return 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")}:
} }
} }
} }

View File

@ -109,7 +109,7 @@ func TestMissingPack(t *testing.T) {
test.Assert(t, len(errs) == 1, test.Assert(t, len(errs) == 1,
"expected exactly one error, got %v", len(errs)) "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()) test.Equals(t, packHandle.Name, err.ID.String())
} else { } else {
t.Errorf("expected error returned by checker.Packs() to be PackError, got %v", err) 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, test.Assert(t, len(errs) == 1,
"expected exactly one error, got %v", len(errs)) "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()) test.Equals(t, packID, err.ID.String())
} else { } else {
t.Errorf("expected error returned by checker.Packs() to be PackError, got %v", err) t.Errorf("expected error returned by checker.Packs() to be PackError, got %v", err)

View File

@ -52,4 +52,6 @@ func Cause(err error) error {
// Go 1.13-style error handling. // 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) } func Is(x, y error) bool { return errors.Is(x, y) }

View File

@ -153,7 +153,7 @@ func SearchKey(ctx context.Context, s *Repository, password string, maxKeys int,
debug.Log("key %v returned error %v", fi.Name, err) debug.Log("key %v returned error %v", fi.Name, err)
// ErrUnauthenticated means the password is wrong, try the next key // ErrUnauthenticated means the password is wrong, try the next key
if errors.Cause(err) == crypto.ErrUnauthenticated { if errors.Is(err, crypto.ErrUnauthenticated) {
return nil return nil
} }

View File

@ -38,13 +38,13 @@ type Lock struct {
lockID *ID 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. // acquire the desired lock.
type ErrAlreadyLocked struct { type alreadyLockedError struct {
otherLock *Lock otherLock *Lock
} }
func (e ErrAlreadyLocked) Error() string { func (e *alreadyLockedError) Error() string {
s := "" s := ""
if e.otherLock.Exclusive { if e.otherLock.Exclusive {
s = "exclusively " s = "exclusively "
@ -52,25 +52,23 @@ func (e ErrAlreadyLocked) Error() string {
return fmt.Sprintf("repository is already locked %sby %v", s, e.otherLock) 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 { func IsAlreadyLocked(err error) bool {
if _, ok := errors.Cause(err).(ErrAlreadyLocked); ok { var e *alreadyLockedError
return true return errors.As(err, &e)
}
return false
} }
// NewLock returns a new, non-exclusive lock for the repository. If an // NewLock returns a new, non-exclusive lock for the repository. If an
// exclusive lock is already held by another process, ErrAlreadyLocked is // exclusive lock is already held by another process, it returns an error
// returned. // that satisfies IsAlreadyLocked.
func NewLock(ctx context.Context, repo Repository) (*Lock, error) { func NewLock(ctx context.Context, repo Repository) (*Lock, error) {
return newLock(ctx, repo, false) return newLock(ctx, repo, false)
} }
// NewExclusiveLock returns a new, exclusive lock for the repository. If // NewExclusiveLock returns a new, exclusive lock for the repository. If
// another lock (normal and exclusive) is already held by another process, // 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) { func NewExclusiveLock(ctx context.Context, repo Repository) (*Lock, error) {
return newLock(ctx, repo, true) return newLock(ctx, repo, true)
} }
@ -147,11 +145,11 @@ func (l *Lock) checkForOtherLocks(ctx context.Context) error {
} }
if l.Exclusive { if l.Exclusive {
return ErrAlreadyLocked{otherLock: lock} return &alreadyLockedError{otherLock: lock}
} }
if !l.Exclusive && lock.Exclusive { if !l.Exclusive && lock.Exclusive {
return ErrAlreadyLocked{otherLock: lock} return &alreadyLockedError{otherLock: lock}
} }
return nil return nil

View File

@ -9,8 +9,6 @@ import (
"testing" "testing"
"time" "time"
"github.com/restic/restic/internal/errors"
"github.com/restic/chunker" "github.com/restic/chunker"
) )
@ -44,7 +42,7 @@ func (fs *fakeFileSystem) saveFile(ctx context.Context, rd io.Reader) (blobs IDs
blobs = IDs{} blobs = IDs{}
for { for {
chunk, err := fs.chunker.Next(fs.buf) chunk, err := fs.chunker.Next(fs.buf)
if errors.Cause(err) == io.EOF { if err == io.EOF {
break break
} }

View File

@ -175,7 +175,7 @@ func ResetReadOnly(t testing.TB, dir string) {
return nil return nil
}) })
if os.IsNotExist(errors.Cause(err)) { if errors.Is(err, os.ErrNotExist) {
err = nil err = nil
} }
OK(t, err) OK(t, err)
@ -186,7 +186,7 @@ func ResetReadOnly(t testing.TB, dir string) {
func RemoveAll(t testing.TB, path string) { func RemoveAll(t testing.TB, path string) {
ResetReadOnly(t, path) ResetReadOnly(t, path)
err := os.RemoveAll(path) err := os.RemoveAll(path)
if os.IsNotExist(errors.Cause(err)) { if errors.Is(err, os.ErrNotExist) {
err = nil err = nil
} }
OK(t, err) OK(t, err)