github.com/prebid/prebid-server/v2@v2.18.0/errortypes/severity.go (about)

     1  package errortypes
     2  
     3  // Severity represents the severity level of a bid processing error.
     4  type Severity int
     5  
     6  const (
     7  	// SeverityUnknown represents an unknown severity level.
     8  	SeverityUnknown Severity = iota
     9  
    10  	// SeverityFatal represents a fatal bid processing error which prevents a bid response.
    11  	SeverityFatal
    12  
    13  	// SeverityWarning represents a non-fatal bid processing error where invalid or ambiguous
    14  	// data in the bid request was ignored.
    15  	SeverityWarning
    16  )
    17  
    18  func isFatal(err error) bool {
    19  	s, ok := err.(Coder)
    20  	return !ok || s.Severity() == SeverityFatal
    21  }
    22  
    23  // IsWarning returns true if an error is labeled with a Severity of SeverityWarning
    24  // Throughout the codebase, errors with SeverityWarning are of the type Warning
    25  // defined in this package
    26  func IsWarning(err error) bool {
    27  	s, ok := err.(Coder)
    28  	return ok && s.Severity() == SeverityWarning
    29  }
    30  
    31  // ContainsFatalError checks if the error list contains a fatal error.
    32  func ContainsFatalError(errors []error) bool {
    33  	for _, err := range errors {
    34  		if isFatal(err) {
    35  			return true
    36  		}
    37  	}
    38  
    39  	return false
    40  }
    41  
    42  // FatalOnly returns a new error list with only the fatal severity errors.
    43  func FatalOnly(errs []error) []error {
    44  	errsFatal := make([]error, 0, len(errs))
    45  
    46  	for _, err := range errs {
    47  		if isFatal(err) {
    48  			errsFatal = append(errsFatal, err)
    49  		}
    50  	}
    51  
    52  	return errsFatal
    53  }
    54  
    55  // WarningOnly returns a new error list with only the warning severity errors.
    56  func WarningOnly(errs []error) []error {
    57  	errsWarning := make([]error, 0, len(errs))
    58  
    59  	for _, err := range errs {
    60  		if IsWarning(err) {
    61  			errsWarning = append(errsWarning, err)
    62  		}
    63  	}
    64  
    65  	return errsWarning
    66  }