From c78cc953d9e95992575305b4e8648392c6c9b9e6 Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Wed, 16 Jun 2021 19:26:15 +0300 Subject: [PATCH] fix: implement `errors.Is` for all errors in the set Previously it matched only a single error if the set consists of a single error. But the retry might fail many times on some expected error followed by unexpected error which we want to test for with 'errors.Is'. Signed-off-by: Andrey Smirnov --- retry/retry.go | 8 +++++++- retry/retry_test.go | 6 ++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/retry/retry.go b/retry/retry.go index 788d72e..69be3a9 100644 --- a/retry/retry.go +++ b/retry/retry.go @@ -91,7 +91,13 @@ func (e *ErrorSet) Is(err error) bool { e.mu.Lock() defer e.mu.Unlock() - return len(e.errs) == 1 && errors.Is(e.errs[0], err) + for _, ee := range e.errs { + if errors.Is(ee, err) { + return true + } + } + + return false } // TimeoutError represents a timeout error. diff --git a/retry/retry_test.go b/retry/retry_test.go index 37a4baf..86a8e47 100644 --- a/retry/retry_test.go +++ b/retry/retry_test.go @@ -130,6 +130,12 @@ func Test_errors(t *testing.T) { t.Fatal("error set should wrap errors") } + errSet.Append(errors.New("foo")) + + if !errors.Is(&errSet, e) { + t.Fatal("error set should wrap errors") + } + errSet = ErrorSet{} errSet.Append(UnexpectedError(e))