github.com/haraldrudell/parl@v0.4.176/perrors/errorglue/error-chain.go (about)

     1  /*
     2  © 2020–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  package errorglue
     7  
     8  // ErrorChain implements a chain of errors.
     9  // Error chains do exist in the Go standard library but types and interfaces are not public.
    10  // ErrorChain is used as an embedded type.
    11  // ErrorChain’s publics are Error() and Unwrap()
    12  //
    13  // ErrorChainSlice returns all errors of an error chain,
    14  // or the chain can be traversed iteratively using errors.Unwrap()
    15  type ErrorChain struct {
    16  	error // the wrapped error
    17  }
    18  
    19  var _ error = &ErrorChain{}   // ErrorChain behaves like an error
    20  var _ Wrapper = &ErrorChain{} // ErrorChain has an error chain
    21  
    22  func newErrorChain(err error) (e2 *ErrorChain) {
    23  	return &ErrorChain{err}
    24  }
    25  
    26  // Unwrap is a method required to make ErrorChain an error chain
    27  // ErrorChain.Unwrap() is used by errors.Unwrap() and ErrorChainSlice
    28  func (ec *ErrorChain) Unwrap() error {
    29  	if ec == nil {
    30  		return nil
    31  	}
    32  	return ec.error
    33  }