From 4a874000b7129330416ffb4ed322d6ba64e54c7c Mon Sep 17 00:00:00 2001 From: greatroar <61184462+greatroar@users.noreply.github.com> Date: Sat, 1 Jun 2024 15:04:35 +0200 Subject: [PATCH 1/2] gs: Replace some errors.Wrap calls The first one in Create is already a WithStack error. The rest were referencing code that hasn't existed for quite some time. Note that errors from Google SDKs tends to start with "google:" or "googleapi:". Also, use restic/internal/errors. --- internal/backend/gs/gs.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/internal/backend/gs/gs.go b/internal/backend/gs/gs.go index 305e9b9c1..0af226f5d 100644 --- a/internal/backend/gs/gs.go +++ b/internal/backend/gs/gs.go @@ -12,12 +12,13 @@ import ( "strings" "cloud.google.com/go/storage" - "github.com/pkg/errors" + "github.com/restic/restic/internal/backend" "github.com/restic/restic/internal/backend/layout" "github.com/restic/restic/internal/backend/location" "github.com/restic/restic/internal/backend/util" "github.com/restic/restic/internal/debug" + "github.com/restic/restic/internal/errors" "golang.org/x/oauth2" "golang.org/x/oauth2/google" @@ -134,7 +135,7 @@ func Open(_ context.Context, cfg Config, rt http.RoundTripper) (backend.Backend, func Create(ctx context.Context, cfg Config, rt http.RoundTripper) (backend.Backend, error) { be, err := open(cfg, rt) if err != nil { - return nil, errors.Wrap(err, "open") + return nil, err } // Try to determine if the bucket exists. If it does not, try to create it. @@ -145,7 +146,7 @@ func Create(ctx context.Context, cfg Config, rt http.RoundTripper) (backend.Back // however, the client doesn't have storage.bucket.get permission return be, nil } - return nil, errors.Wrap(err, "service.Buckets.Get") + return nil, errors.WithStack(err) } if !exists { @@ -155,7 +156,7 @@ func Create(ctx context.Context, cfg Config, rt http.RoundTripper) (backend.Back // Bucket doesn't exist, try to create it. if err := be.bucket.Create(ctx, be.projectID, bucketAttrs); err != nil { // Always an error, as the bucket definitely doesn't exist. - return nil, errors.Wrap(err, "service.Buckets.Insert") + return nil, errors.WithStack(err) } } @@ -251,7 +252,7 @@ func (be *Backend) Save(ctx context.Context, h backend.Handle, rd backend.Rewind } if err != nil { - return errors.Wrap(err, "service.Objects.Insert") + return errors.WithStack(err) } // sanity check @@ -298,7 +299,7 @@ func (be *Backend) Stat(ctx context.Context, h backend.Handle) (bi backend.FileI attr, err := be.bucket.Object(objName).Attrs(ctx) if err != nil { - return backend.FileInfo{}, errors.Wrap(err, "service.Objects.Get") + return backend.FileInfo{}, errors.WithStack(err) } return backend.FileInfo{Size: attr.Size, Name: h.Name}, nil @@ -314,7 +315,7 @@ func (be *Backend) Remove(ctx context.Context, h backend.Handle) error { err = nil } - return errors.Wrap(err, "client.RemoveObject") + return errors.WithStack(err) } // List runs fn for each file in the backend which has the type t. When an From 10fdb914df657048a8ed04c9c745785efe418e70 Mon Sep 17 00:00:00 2001 From: greatroar <61184462+greatroar@users.noreply.github.com> Date: Sat, 1 Jun 2024 15:15:06 +0200 Subject: [PATCH 2/2] cmd: Return error in readPassword The returned error was always nil. Replaced Wrap by WithStack because the function name was stale. --- cmd/restic/global.go | 2 +- cmd/restic/global_test.go | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/cmd/restic/global.go b/cmd/restic/global.go index 6920caa8d..cc24b74c2 100644 --- a/cmd/restic/global.go +++ b/cmd/restic/global.go @@ -280,7 +280,7 @@ func readPassword(in io.Reader) (password string, err error) { sc := bufio.NewScanner(in) sc.Scan() - return sc.Text(), errors.Wrap(err, "Scan") + return sc.Text(), errors.WithStack(sc.Err()) } // readPasswordTerminal reads the password from the given reader which must be a diff --git a/cmd/restic/global_test.go b/cmd/restic/global_test.go index 4f5c29e9a..b43fdd1f3 100644 --- a/cmd/restic/global_test.go +++ b/cmd/restic/global_test.go @@ -5,6 +5,7 @@ import ( "path/filepath" "testing" + "github.com/restic/restic/internal/errors" rtest "github.com/restic/restic/internal/test" ) @@ -22,6 +23,16 @@ func Test_PrintFunctionsRespectsGlobalStdout(t *testing.T) { } } +type errorReader struct{ err error } + +func (r *errorReader) Read([]byte) (int, error) { return 0, r.err } + +func TestReadPassword(t *testing.T) { + want := errors.New("foo") + _, err := readPassword(&errorReader{want}) + rtest.Assert(t, errors.Is(err, want), "wrong error %v", err) +} + func TestReadRepo(t *testing.T) { tempDir := rtest.TempDir(t)