mirror of
https://github.com/restic/restic.git
synced 2025-10-09 07:33:53 +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:
@@ -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)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user