From f28e00659c894670093a5b2edaff141eb068b995 Mon Sep 17 00:00:00 2001 From: katexochen <49727155+katexochen@users.noreply.github.com> Date: Mon, 1 Aug 2022 11:54:29 +0200 Subject: [PATCH] Use uber/multierr for error composition --- cli/internal/gcp/client/client.go | 24 ++++-------------------- cli/internal/gcp/client/client_test.go | 20 -------------------- 2 files changed, 4 insertions(+), 40 deletions(-) diff --git a/cli/internal/gcp/client/client.go b/cli/internal/gcp/client/client.go index f98297725..17bb36313 100644 --- a/cli/internal/gcp/client/client.go +++ b/cli/internal/gcp/client/client.go @@ -7,7 +7,6 @@ import ( "errors" "fmt" "math/big" - "strings" compute "cloud.google.com/go/compute/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/cloudtypes" "github.com/edgelesssys/constellation/internal/state" + "go.uber.org/multierr" "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 // errors occur, the returned error will be composed of the error messages // of those errors. - var errs []error + var err error for _, closer := range closers { - errs = append(errs, closer.Close()) + err = multierr.Append(err, closer.Close()) } - return composeErr(errs) -} - -// 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 + return err } diff --git a/cli/internal/gcp/client/client_test.go b/cli/internal/gcp/client/client_test.go index 464bf0a4e..a10feb45f 100644 --- a/cli/internal/gcp/client/client_test.go +++ b/cli/internal/gcp/client/client_test.go @@ -848,23 +848,3 @@ func (c *someCloser) Close() error { c.closed = true 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") -}