...

Source file src/github.com/hashicorp/go-multierror/append.go

Documentation: github.com/hashicorp/go-multierror

     1  package multierror
     2  
     3  // Append is a helper function that will append more errors
     4  // onto an Error in order to create a larger multi-error.
     5  //
     6  // If err is not a multierror.Error, then it will be turned into
     7  // one. If any of the errs are multierr.Error, they will be flattened
     8  // one level into err.
     9  // Any nil errors within errs will be ignored. If err is nil, a new
    10  // *Error will be returned.
    11  func Append(err error, errs ...error) *Error {
    12  	switch err := err.(type) {
    13  	case *Error:
    14  		// Typed nils can reach here, so initialize if we are nil
    15  		if err == nil {
    16  			err = new(Error)
    17  		}
    18  
    19  		// Go through each error and flatten
    20  		for _, e := range errs {
    21  			switch e := e.(type) {
    22  			case *Error:
    23  				if e != nil {
    24  					err.Errors = append(err.Errors, e.Errors...)
    25  				}
    26  			default:
    27  				if e != nil {
    28  					err.Errors = append(err.Errors, e)
    29  				}
    30  			}
    31  		}
    32  
    33  		return err
    34  	default:
    35  		newErrs := make([]error, 0, len(errs)+1)
    36  		if err != nil {
    37  			newErrs = append(newErrs, err)
    38  		}
    39  		newErrs = append(newErrs, errs...)
    40  
    41  		return Append(&Error{}, newErrs...)
    42  	}
    43  }
    44  

View as plain text