From 2e1613d4c634f9c982ad38d0de042279c7743f9b Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sat, 12 Feb 2022 23:31:31 +0100 Subject: [PATCH] errors: Ensure that errors.IsFatal(errors.Fatal("err")) == true This fixes a few cases where restic output "Fatal: Fatal: [...]" --- cmd/restic/main.go | 2 +- internal/errors/fatal.go | 2 ++ internal/errors/fatal_test.go | 22 ++++++++++++++++++++++ internal/restic/config.go | 2 +- 4 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 internal/errors/fatal_test.go diff --git a/cmd/restic/main.go b/cmd/restic/main.go index 0479124b1..2f962b966 100644 --- a/cmd/restic/main.go +++ b/cmd/restic/main.go @@ -102,7 +102,7 @@ func main() { 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) - case errors.IsFatal(errors.Cause(err)): + case errors.IsFatal(err): fmt.Fprintf(os.Stderr, "%v\n", err) case err != nil: fmt.Fprintf(os.Stderr, "%+v\n", err) diff --git a/internal/errors/fatal.go b/internal/errors/fatal.go index 02ffdaab4..5fb615cf1 100644 --- a/internal/errors/fatal.go +++ b/internal/errors/fatal.go @@ -23,6 +23,8 @@ type Fataler interface { // IsFatal returns true if err is a fatal message that should be printed to the // user. Then, the program should exit. func IsFatal(err error) bool { + // unwrap "Wrap" method + err = Cause(err) e, ok := err.(Fataler) return ok && e.Fatal() } diff --git a/internal/errors/fatal_test.go b/internal/errors/fatal_test.go new file mode 100644 index 000000000..41da8dee7 --- /dev/null +++ b/internal/errors/fatal_test.go @@ -0,0 +1,22 @@ +package errors_test + +import ( + "testing" + + "github.com/restic/restic/internal/errors" +) + +func TestFatal(t *testing.T) { + for _, v := range []struct { + err error + expected bool + }{ + {errors.Fatal("broken"), true}, + {errors.Fatalf("broken %d", 42), true}, + {errors.New("error"), false}, + } { + if errors.IsFatal(v.err) != v.expected { + t.Fatalf("IsFatal for %q, expected: %v, got: %v", v.err, v.expected, errors.IsFatal(v.err)) + } + } +} diff --git a/internal/restic/config.go b/internal/restic/config.go index 4f3c6c4bc..ded98ac1b 100644 --- a/internal/restic/config.go +++ b/internal/restic/config.go @@ -78,7 +78,7 @@ func LoadConfig(ctx context.Context, r JSONUnpackedLoader) (Config, error) { } if cfg.Version != RepoVersion { - return Config{}, errors.New("unsupported repository version") + return Config{}, errors.Errorf("unsupported repository version %v", cfg.Version) } if checkPolynomial {