util/multierr: new package

github.com/go-multierror/multierror served us well.
But we need a few feature from it (implement Is),
and it's not worth maintaining a fork of such a small module.

Instead, I did a clean room implementation inspired by its API.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
This commit is contained in:
Josh Bleecher Snyder
2021-11-02 14:30:48 -07:00
committed by Josh Bleecher Snyder
parent 17b5782b3a
commit 3fd5f4380f
14 changed files with 186 additions and 19 deletions

88
util/multierr/multierr.go Normal file
View File

@@ -0,0 +1,88 @@
// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package multierr provides a simple multiple-error type.
// It was inspired by github.com/go-multierror/multierror.
package multierr
import (
"errors"
"strings"
)
// An Error represents multiple errors.
type Error struct {
errs []error
}
// Error implements the error interface.
func (e Error) Error() string {
s := new(strings.Builder)
s.WriteString("multiple errors:")
for _, err := range e.errs {
s.WriteString("\n\t")
s.WriteString(err.Error())
}
return s.String()
}
// Errors returns a slice containing all errors in e.
func (e Error) Errors() []error {
return append(e.errs[:0:0], e.errs...)
}
// New returns an error composed from errs.
// Some errors in errs get special treatment:
// * nil errors are discarded
// * errors of type Error are expanded into the top level
// If the resulting slice has length 0, New returns nil.
// If the resulting slice has length 1, New returns that error.
// If the resulting slice has length > 1, New returns that slice as an Error.
func New(errs ...error) error {
dst := make([]error, 0, len(errs))
for _, e := range errs {
switch e := e.(type) {
case nil:
continue
case Error:
dst = append(dst, e.errs...)
default:
dst = append(dst, e)
}
}
// dst has been filtered and splatted.
switch len(dst) {
case 0:
return nil
case 1:
return dst[0]
}
// Zero any remaining elements of dst left over from errs, for GC.
tail := dst[len(dst):cap(dst)]
for i := range tail {
tail[i] = nil
}
return Error{errs: dst}
}
// Is reports whether any error in e matches target.
func (e Error) Is(target error) bool {
for _, err := range e.errs {
if errors.Is(err, target) {
return true
}
}
return false
}
// As finds the first error in e that matches target, and if any is found,
// sets target to that error value and returns true. Otherwise, it returns false.
func (e Error) As(target interface{}) bool {
for _, err := range e.errs {
if ok := errors.As(err, target); ok {
return true
}
}
return false
}

View File

@@ -0,0 +1,80 @@
// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package multierr_test
import (
"errors"
"testing"
qt "github.com/frankban/quicktest"
"github.com/google/go-cmp/cmp/cmpopts"
"tailscale.com/util/multierr"
)
func TestAll(t *testing.T) {
C := qt.New(t)
eqErr := qt.CmpEquals(cmpopts.EquateErrors())
type E = []error
N := multierr.New
a := errors.New("a")
b := errors.New("b")
c := errors.New("c")
d := errors.New("d")
x := errors.New("x")
abcd := E{a, b, c, d}
tests := []struct {
In E // input to New
WantNil bool // want nil returned?
WantSingle error // if non-nil, want this single error returned
WantErrors []error // if non-nil, want an Error composed of these errors returned
}{
{In: nil, WantNil: true},
{In: E{nil}, WantNil: true},
{In: E{nil, nil}, WantNil: true},
{In: E{a}, WantSingle: a},
{In: E{a, nil}, WantSingle: a},
{In: E{nil, a}, WantSingle: a},
{In: E{nil, a, nil}, WantSingle: a},
{In: E{a, b}, WantErrors: E{a, b}},
{In: E{nil, a, nil, b, nil}, WantErrors: E{a, b}},
{In: E{a, b, N(c, d)}, WantErrors: E{a, b, c, d}},
{In: E{a, N(b, c), d}, WantErrors: E{a, b, c, d}},
{In: E{N(a, b), c, d}, WantErrors: E{a, b, c, d}},
{In: E{N(a, b), N(c, d)}, WantErrors: E{a, b, c, d}},
{In: E{nil, N(a, nil, b), nil, N(c, d)}, WantErrors: E{a, b, c, d}},
{In: E{N(a, N(b, N(c, N(d))))}, WantErrors: E{a, b, c, d}},
{In: E{N(N(N(N(a), b), c), d)}, WantErrors: E{a, b, c, d}},
{In: E{N(abcd...)}, WantErrors: E{a, b, c, d}},
{In: E{N(abcd...), N(abcd...)}, WantErrors: E{a, b, c, d, a, b, c, d}},
}
for _, test := range tests {
got := multierr.New(test.In...)
if test.WantNil {
C.Assert(got, qt.IsNil)
continue
}
if test.WantSingle != nil {
C.Assert(got, eqErr, test.WantSingle)
continue
}
ee, _ := got.(multierr.Error)
C.Assert(ee.Errors(), eqErr, test.WantErrors)
for _, e := range test.WantErrors {
C.Assert(ee.Is(e), qt.IsTrue)
}
C.Assert(ee.Is(x), qt.IsFalse)
}
}