github.com/prebid/prebid-server@v0.275.0/errortypes/aggregate.go (about)

     1  package errortypes
     2  
     3  import (
     4  	"bytes"
     5  	"strconv"
     6  )
     7  
     8  // AggregateError represents one or more errors.
     9  type AggregateError struct {
    10  	Message string
    11  	Errors  []error
    12  }
    13  
    14  // NewAggregateError builds a AggregateError struct.
    15  func NewAggregateError(msg string, errs []error) AggregateError {
    16  	return AggregateError{
    17  		Message: msg,
    18  		Errors:  errs,
    19  	}
    20  }
    21  
    22  // Error implements the standard error interface.
    23  func (e AggregateError) Error() string {
    24  	if len(e.Errors) == 0 {
    25  		return ""
    26  	}
    27  
    28  	b := bytes.Buffer{}
    29  	b.WriteString(e.Message)
    30  
    31  	if len(e.Errors) == 1 {
    32  		b.WriteString(" (1 error):\n")
    33  	} else {
    34  		b.WriteString(" (")
    35  		b.WriteString(strconv.Itoa(len(e.Errors)))
    36  		b.WriteString(" errors):\n")
    37  	}
    38  
    39  	for i, err := range e.Errors {
    40  		b.WriteString("  ")
    41  		b.WriteString(strconv.Itoa(i + 1))
    42  		b.WriteString(": ")
    43  		b.WriteString(err.Error())
    44  		b.WriteString("\n")
    45  	}
    46  
    47  	return b.String()
    48  }