github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/updater/util/error.go (about) 1 // Copyright 2015 Keybase, Inc. All rights reserved. Use of 2 // this source code is governed by the included BSD license. 3 4 package util 5 6 import ( 7 "fmt" 8 "strings" 9 ) 10 11 // removeNilErrors returns error slice with nil errors removed 12 func removeNilErrors(errs []error) []error { 13 if len(errs) == 0 { 14 return nil 15 } 16 var r []error 17 for _, err := range errs { 18 if err != nil { 19 r = append(r, err) 20 } 21 } 22 return r 23 } 24 25 // CombineErrors returns a single error for multiple errors, or nil if none 26 func CombineErrors(errs ...error) error { 27 errs = removeNilErrors(errs) 28 if len(errs) == 0 { 29 return nil 30 } else if len(errs) == 1 { 31 return errs[0] 32 } 33 34 // Combine multiple errors 35 msgs := []string{} 36 for _, err := range errs { 37 msgs = append(msgs, err.Error()) 38 } 39 return fmt.Errorf("There were multiple errors: %s", strings.Join(msgs, "; ")) 40 }