From a67dabb3d005df8ecd0b14d85308df798f0063d0 Mon Sep 17 00:00:00 2001 From: Henry Wang Date: Tue, 26 Mar 2024 16:56:29 +0000 Subject: [PATCH] use errors package in golang v20 to join multiple errors Signed-off-by: Henry Wang --- libcni/api.go | 3 ++- libcni/multierror.go | 58 -------------------------------------------- 2 files changed, 2 insertions(+), 59 deletions(-) delete mode 100644 libcni/multierror.go diff --git a/libcni/api.go b/libcni/api.go index 5c7f3b02..25a49927 100644 --- a/libcni/api.go +++ b/libcni/api.go @@ -23,6 +23,7 @@ package libcni import ( "context" "encoding/json" + "errors" "fmt" "os" "path/filepath" @@ -815,7 +816,7 @@ func (c *CNIConfig) GCNetworkList(ctx context.Context, list *NetworkConfigList, } } - return joinErrors(errs...) + return errors.Join(errs...) } func (c *CNIConfig) gcNetwork(ctx context.Context, net *NetworkConfig) error { diff --git a/libcni/multierror.go b/libcni/multierror.go deleted file mode 100644 index 100fb839..00000000 --- a/libcni/multierror.go +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright 2022 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// Copyright the CNI authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// Adapted from errors/join.go from go 1.20 -// This package can be removed once the toolchain is updated to 1.20 - -package libcni - -func joinErrors(errs ...error) error { - n := 0 - for _, err := range errs { - if err != nil { - n++ - } - } - if n == 0 { - return nil - } - e := &multiError{ - errs: make([]error, 0, n), - } - for _, err := range errs { - if err != nil { - e.errs = append(e.errs, err) - } - } - return e -} - -type multiError struct { - errs []error -} - -func (e *multiError) Error() string { - var b []byte - for i, err := range e.errs { - if i > 0 { - b = append(b, '\n') - } - b = append(b, err.Error()...) - } - return string(b) -}