github.com/blend/go-sdk@v1.20220411.3/ex/nest.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 ex
     9  
    10  // Nest nests an arbitrary number of exceptions.
    11  func Nest(err ...error) error {
    12  	var ex *Ex
    13  	var last *Ex
    14  	var didSet bool
    15  
    16  	for _, e := range err {
    17  		if e != nil {
    18  			var wrappedEx *Ex
    19  			if typedEx, isTyped := e.(*Ex); !isTyped {
    20  				wrappedEx = &Ex{
    21  					Class:      e,
    22  					StackTrace: Callers(DefaultStartDepth),
    23  				}
    24  			} else {
    25  				wrappedEx = typedEx
    26  			}
    27  
    28  			if wrappedEx != ex {
    29  				if ex == nil {
    30  					ex = wrappedEx
    31  					last = wrappedEx
    32  				} else {
    33  					last.Inner = wrappedEx
    34  					last = wrappedEx
    35  				}
    36  				didSet = true
    37  			}
    38  		}
    39  	}
    40  	if didSet {
    41  		return ex
    42  	}
    43  	return nil
    44  }