Use uber/multierr for error composition

This commit is contained in:
katexochen 2022-08-01 11:54:29 +02:00 committed by Paul Meyer
parent a859accf1f
commit f28e00659c
2 changed files with 4 additions and 40 deletions

View File

@ -7,7 +7,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"math/big" "math/big"
"strings"
compute "cloud.google.com/go/compute/apiv1" compute "cloud.google.com/go/compute/apiv1"
admin "cloud.google.com/go/iam/admin/apiv1" admin "cloud.google.com/go/iam/admin/apiv1"
@ -15,6 +14,7 @@ import (
"github.com/edgelesssys/constellation/internal/cloud/cloudprovider" "github.com/edgelesssys/constellation/internal/cloud/cloudprovider"
"github.com/edgelesssys/constellation/internal/cloud/cloudtypes" "github.com/edgelesssys/constellation/internal/cloud/cloudtypes"
"github.com/edgelesssys/constellation/internal/state" "github.com/edgelesssys/constellation/internal/state"
"go.uber.org/multierr"
"golang.org/x/oauth2/google" "golang.org/x/oauth2/google"
) )
@ -461,25 +461,9 @@ func closeAll(closers []closer) error {
// close operations, even if a previous operation failed. The if multiple // close operations, even if a previous operation failed. The if multiple
// errors occur, the returned error will be composed of the error messages // errors occur, the returned error will be composed of the error messages
// of those errors. // of those errors.
var errs []error var err error
for _, closer := range closers { for _, closer := range closers {
errs = append(errs, closer.Close()) err = multierr.Append(err, closer.Close())
} }
return composeErr(errs) return err
}
// composeErr composes a list of errors to a single error.
//
// If all errs are nil, the returned error is also nil.
func composeErr(errs []error) error {
var composed strings.Builder
for i, err := range errs {
if err != nil {
composed.WriteString(fmt.Sprintf("%d: %s", i, err.Error()))
}
}
if composed.Len() != 0 {
return errors.New(composed.String())
}
return nil
} }

View File

@ -848,23 +848,3 @@ func (c *someCloser) Close() error {
c.closed = true c.closed = true
return c.closeErr return c.closeErr
} }
func TestComposedErr(t *testing.T) {
assert := assert.New(t)
noErrs := []error{nil, nil, nil}
assert.NoError(composeErr(noErrs))
someErrs := []error{
errors.New("failed 4"),
errors.New("failed 7"),
nil,
nil,
errors.New("failed 9"),
}
err := composeErr(someErrs)
assert.Error(err)
assert.Contains(err.Error(), "4")
assert.Contains(err.Error(), "7")
assert.Contains(err.Error(), "9")
}