From dace0b0668976441cb7dfac6800dce9223105926 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 27 Oct 2021 12:23:31 +0200 Subject: [PATCH] agent/exec: rewrite IsTemporary() to use errors.As() This makes sure we detect temporary errors both with pkg/errors.Wrap() and with native Go error wrapping. Signed-off-by: Sebastiaan van Stijn --- agent/exec/errors.go | 12 +++--------- agent/exec/errors_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 9 deletions(-) create mode 100644 agent/exec/errors_test.go diff --git a/agent/exec/errors.go b/agent/exec/errors.go index a32a71a992..06d32707a2 100644 --- a/agent/exec/errors.go +++ b/agent/exec/errors.go @@ -70,15 +70,9 @@ func (t temporary) Temporary() bool { return true } // IsTemporary returns true if the error or a recursive cause returns true for // temporary. func IsTemporary(err error) bool { - if tmp, ok := err.(Temporary); ok && tmp.Temporary() { - return true + var tmp Temporary + if errors.As(err, &tmp) { + return tmp.Temporary() } - - cause := errors.Cause(err) - - if tmp, ok := cause.(Temporary); ok && tmp.Temporary() { - return true - } - return false } diff --git a/agent/exec/errors_test.go b/agent/exec/errors_test.go new file mode 100644 index 0000000000..45a237fdd4 --- /dev/null +++ b/agent/exec/errors_test.go @@ -0,0 +1,27 @@ +package exec + +import ( + "fmt" + "testing" + + "github.com/pkg/errors" +) + +func TestIsTemporary(t *testing.T) { + err := fmt.Errorf("err") + err1 := MakeTemporary(fmt.Errorf("err1: %w", err)) + err2 := fmt.Errorf("err2: %w", err1) + err3 := errors.Wrap(err2, "err3") + err4 := fmt.Errorf("err4: %w", err3) + err5 := errors.Wrap(err4, "err5") + + if IsTemporary(nil) { + t.Error("expected error to not be a temporary error") + } + if IsTemporary(err) { + t.Error("expected error to not be a temporary error") + } + if !IsTemporary(err5) { + t.Error("expected error to be a temporary error") + } +}