github.com/ronaksoft/rony@v0.16.26-0.20230807065236-1743dbfe6959/errors/multi.go (about)

     1  package errors
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  )
     7  
     8  /*
     9     Creation Time: 2021 - May - 21
    10     Created by:  (ehsan)
    11     Maintainers:
    12        1.  Ehsan N. Moosa (E2)
    13     Auditor: Ehsan N. Moosa (E2)
    14     Copyright Ronak Software Group 2020
    15  */
    16  
    17  type Multi struct {
    18  	sync.Mutex
    19  	errs []error
    20  }
    21  
    22  func (e *Multi) Error() string {
    23  	if len(e.errs) == 1 {
    24  		return e.errs[0].Error()
    25  	}
    26  
    27  	return fmt.Sprintf("Errors: %d, %v", len(e.errs), e.errs)
    28  }
    29  
    30  func (e *Multi) HasError() bool {
    31  	return len(e.errs) > 0
    32  }
    33  
    34  func (e *Multi) AddError(err error) {
    35  	if err == nil {
    36  		return
    37  	}
    38  	e.Lock()
    39  	e.errs = append(e.errs, err)
    40  	e.Unlock()
    41  }