github.com/blend/go-sdk@v1.20220411.3/async/errors.go (about)

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