From ba44666704387119c1003fa55c534f994c971191 Mon Sep 17 00:00:00 2001 From: greatroar <61184462+greatroar@users.noreply.github.com> Date: Sun, 9 Oct 2022 11:23:07 +0200 Subject: [PATCH 1/4] errors: Drop WithMessage --- cmd/restic/cmd_backup.go | 2 +- cmd/restic/lock.go | 4 ++-- internal/errors/errors.go | 4 ---- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/cmd/restic/cmd_backup.go b/cmd/restic/cmd_backup.go index c435682bc..c82ba8ff4 100644 --- a/cmd/restic/cmd_backup.go +++ b/cmd/restic/cmd_backup.go @@ -457,7 +457,7 @@ func collectTargets(opts BackupOptions, args []string) (targets []string, err er var expanded []string expanded, err := filepath.Glob(line) if err != nil { - return nil, errors.WithMessage(err, fmt.Sprintf("pattern: %s", line)) + return nil, fmt.Errorf("pattern: %s: %w", line, err) } if len(expanded) == 0 { Warnf("pattern %q does not match any files, skipping\n", line) diff --git a/cmd/restic/lock.go b/cmd/restic/lock.go index 851dcefe8..43bc22b4a 100644 --- a/cmd/restic/lock.go +++ b/cmd/restic/lock.go @@ -2,11 +2,11 @@ package main import ( "context" + "fmt" "sync" "time" "github.com/restic/restic/internal/debug" - "github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/restic" ) @@ -45,7 +45,7 @@ func lockRepository(ctx context.Context, repo restic.Repository, exclusive bool) lock, err := lockFn(ctx, repo) if err != nil { - return nil, ctx, errors.WithMessage(err, "unable to create lock in backend") + return nil, ctx, fmt.Errorf("unable to create lock in backend: %w", err) } debug.Log("create lock %p (exclusive %v)", lock, exclusive) diff --git a/internal/errors/errors.go b/internal/errors/errors.go index 4fd94cbf2..0327ea0da 100644 --- a/internal/errors/errors.go +++ b/internal/errors/errors.go @@ -22,10 +22,6 @@ var Wrap = errors.Wrap // nil, Wrapf returns nil. var Wrapf = errors.Wrapf -// WithMessage annotates err with a new message. If err is nil, WithMessage -// returns nil. -var WithMessage = errors.WithMessage - var WithStack = errors.WithStack // Go 1.13-style error handling. From feb790f497aa9d5a1d8327f9a337e9d21da6a34a Mon Sep 17 00:00:00 2001 From: greatroar <61184462+greatroar@users.noreply.github.com> Date: Fri, 14 Oct 2022 14:07:20 +0200 Subject: [PATCH 2/4] internal/restic: Use errors.New when no formatting is needed --- internal/restic/tree.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/restic/tree.go b/internal/restic/tree.go index d62354b3a..373b36746 100644 --- a/internal/restic/tree.go +++ b/internal/restic/tree.go @@ -145,7 +145,7 @@ func SaveTree(ctx context.Context, r BlobSaver, t *Tree) (ID, error) { return id, err } -var ErrTreeNotOrdered = errors.Errorf("nodes are not ordered or duplicate") +var ErrTreeNotOrdered = errors.New("nodes are not ordered or duplicate") type TreeJSONBuilder struct { buf bytes.Buffer From 09c14f33c866cda92daee68fb49e5111cb06aa0c Mon Sep 17 00:00:00 2001 From: greatroar <61184462+greatroar@users.noreply.github.com> Date: Fri, 14 Oct 2022 14:13:32 +0200 Subject: [PATCH 3/4] internal/checker: Pass Error.Error pointer receiver --- internal/checker/checker.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/internal/checker/checker.go b/internal/checker/checker.go index ba04e5e0b..d880a8d70 100644 --- a/internal/checker/checker.go +++ b/internal/checker/checker.go @@ -290,7 +290,7 @@ type Error struct { Err error } -func (e Error) Error() string { +func (e *Error) Error() string { if !e.TreeID.IsNull() { return "tree " + e.TreeID.String() + ": " + e.Err.Error() } @@ -404,12 +404,12 @@ func (c *Checker) checkTree(id restic.ID, tree *restic.Tree) (errs []error) { switch node.Type { case "file": if node.Content == nil { - errs = append(errs, Error{TreeID: id, Err: errors.Errorf("file %q has nil blob list", node.Name)}) + errs = append(errs, &Error{TreeID: id, Err: errors.Errorf("file %q has nil blob list", node.Name)}) } for b, blobID := range node.Content { if blobID.IsNull() { - errs = append(errs, Error{TreeID: id, Err: errors.Errorf("file %q blob %d has null ID", node.Name, b)}) + errs = append(errs, &Error{TreeID: id, Err: errors.Errorf("file %q blob %d has null ID", node.Name, b)}) continue } // Note that we do not use the blob size. The "obvious" check @@ -420,7 +420,7 @@ func (c *Checker) checkTree(id restic.ID, tree *restic.Tree) (errs []error) { _, found := c.repo.LookupBlobSize(blobID, restic.DataBlob) if !found { debug.Log("tree %v references blob %v which isn't contained in index", id, blobID) - errs = append(errs, Error{TreeID: id, Err: errors.Errorf("file %q blob %v not found in index", node.Name, blobID)}) + errs = append(errs, &Error{TreeID: id, Err: errors.Errorf("file %q blob %v not found in index", node.Name, blobID)}) } } @@ -440,12 +440,12 @@ func (c *Checker) checkTree(id restic.ID, tree *restic.Tree) (errs []error) { case "dir": if node.Subtree == nil { - errs = append(errs, Error{TreeID: id, Err: errors.Errorf("dir node %q has no subtree", node.Name)}) + errs = append(errs, &Error{TreeID: id, Err: errors.Errorf("dir node %q has no subtree", node.Name)}) continue } if node.Subtree.IsNull() { - errs = append(errs, Error{TreeID: id, Err: errors.Errorf("dir node %q subtree id is null", node.Name)}) + errs = append(errs, &Error{TreeID: id, Err: errors.Errorf("dir node %q subtree id is null", node.Name)}) continue } @@ -453,11 +453,11 @@ func (c *Checker) checkTree(id restic.ID, tree *restic.Tree) (errs []error) { // nothing to check default: - errs = append(errs, Error{TreeID: id, Err: errors.Errorf("node %q with invalid type %q", node.Name, node.Type)}) + errs = append(errs, &Error{TreeID: id, Err: errors.Errorf("node %q with invalid type %q", node.Name, node.Type)}) } if node.Name == "" { - errs = append(errs, Error{TreeID: id, Err: errors.New("node with empty name")}) + errs = append(errs, &Error{TreeID: id, Err: errors.New("node with empty name")}) } } From 16849d53618b2d7ee633c0664f46484c45ed5803 Mon Sep 17 00:00:00 2001 From: greatroar <61184462+greatroar@users.noreply.github.com> Date: Fri, 14 Oct 2022 14:18:52 +0200 Subject: [PATCH 4/4] internal/archiver: Missing argument to errors.Errorf --- internal/archiver/archiver.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/archiver/archiver.go b/internal/archiver/archiver.go index 24a0dc135..59392e028 100644 --- a/internal/archiver/archiver.go +++ b/internal/archiver/archiver.go @@ -420,7 +420,7 @@ func (arch *Archiver) Save(ctx context.Context, snPath, target string, previous // make sure it's still a file if !fs.IsRegularFile(fi) { - err = errors.Errorf("file %v changed type, refusing to archive") + err = errors.Errorf("file %v changed type, refusing to archive", fi.Name()) _ = file.Close() err = arch.error(abstarget, err) if err != nil {