From 21eea923ccbca83f693682b20abe3087f59be21f Mon Sep 17 00:00:00 2001 From: yah01 Date: Tue, 28 Feb 2023 19:35:47 +0800 Subject: [PATCH] Add util to combine multiple errors so everyone checkable (#22473) Signed-off-by: yah01 --- internal/util/errutil/errors.go | 37 ++++++++++++++++++++++++++++ internal/util/errutil/errors_test.go | 31 +++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 internal/util/errutil/errors.go create mode 100644 internal/util/errutil/errors_test.go diff --git a/internal/util/errutil/errors.go b/internal/util/errutil/errors.go new file mode 100644 index 0000000000..d0a239f58a --- /dev/null +++ b/internal/util/errutil/errors.go @@ -0,0 +1,37 @@ +package errutil + +import "github.com/cockroachdb/errors" + +type multiErrors struct { + errs []error +} + +func (e multiErrors) Unwrap() error { + if len(e.errs) <= 1 { + return nil + } + return multiErrors{ + errs: e.errs[1:], + } +} + +func (e multiErrors) Error() string { + final := e.errs[0] + for i := 1; i < len(e.errs); i++ { + final = errors.Wrap(e.errs[i], final.Error()) + } + return final.Error() +} + +func (e multiErrors) Is(err error) bool { + return errors.IsAny(err, e.errs...) +} + +func Combine(errs ...error) error { + if len(errs) == 0 { + return nil + } + return multiErrors{ + errs, + } +} diff --git a/internal/util/errutil/errors_test.go b/internal/util/errutil/errors_test.go new file mode 100644 index 0000000000..99c4185754 --- /dev/null +++ b/internal/util/errutil/errors_test.go @@ -0,0 +1,31 @@ +package errutil + +import ( + "testing" + + "github.com/cockroachdb/errors" + "github.com/stretchr/testify/suite" +) + +type ErrSuite struct { + suite.Suite +} + +func (s *ErrSuite) TestCombine() { + var ( + errFirst = errors.New("first") + errSecond = errors.New("second") + errThird = errors.New("third") + ) + + err := Combine(errFirst, errSecond) + s.True(errors.Is(err, errFirst)) + s.True(errors.Is(err, errSecond)) + s.False(errors.Is(err, errThird)) + + s.Equal("first: second", err.Error()) +} + +func TestErrors(t *testing.T) { + suite.Run(t, new(ErrSuite)) +}