go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/async/errors.go (about) 1 /* 2 3 Copyright (c) 2023 - Present. Will Charczuk. All rights reserved. 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository. 5 6 */ 7 8 package async 9 10 import ( 11 "go.charczuk.com/sdk/errutil" 12 ) 13 14 // Errors is a channel for errors 15 type Errors chan error 16 17 // First returns the first (non-nil) error. 18 func (e Errors) First() error { 19 if errorCount := len(e); errorCount > 0 { 20 var err error 21 for x := 0; x < errorCount; x++ { 22 err = <-e 23 if err != nil { 24 return err 25 } 26 } 27 return nil 28 } 29 return nil 30 } 31 32 // All returns all the non-nil errors in the channel 33 // as a multi-error. 34 func (e Errors) All() error { 35 if errorCount := len(e); errorCount > 0 { 36 var errors []error 37 for x := 0; x < errorCount; x++ { 38 err := <-e 39 if err != nil { 40 errors = append(errors, err) 41 } 42 } 43 if len(errors) > 0 { 44 return errutil.Append(nil, errors...) 45 } 46 return nil 47 } 48 return nil 49 }