...

Source file src/edge-infra.dev/pkg/edge/api/utils/error_helper.go

Documentation: edge-infra.dev/pkg/edge/api/utils

     1  package utils
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  // ErrorWrapper struct wrapper for errors.
     9  type ErrorWrapper struct {
    10  	Errors error
    11  }
    12  
    13  // NewErrorWrapper returns a new NewErrorWrapper.
    14  func NewErrorWrapper() *ErrorWrapper {
    15  	return &ErrorWrapper{
    16  		Errors: nil,
    17  	}
    18  }
    19  
    20  // AddError appends a new error and error message.
    21  func (e *ErrorWrapper) AddError(err error, message string) *ErrorWrapper {
    22  	if e.Errors != nil {
    23  		e.Errors = fmt.Errorf("%w; %s: %s", e.Errors, message, err)
    24  	} else {
    25  		e.Errors = fmt.Errorf("%s: %s", message, err)
    26  	}
    27  	return e
    28  }
    29  
    30  // IsNotNil returns if the error is nil or not.
    31  func (e ErrorWrapper) IsNotNil() bool {
    32  	return e.Errors != nil
    33  }
    34  
    35  // GetErrors returns all appended errors.
    36  func (e ErrorWrapper) GetErrors() error {
    37  	return e.Errors
    38  }
    39  
    40  // Len returns how many errors have been appended.
    41  func (e ErrorWrapper) Len() int {
    42  	if e.Errors == nil {
    43  		return 0
    44  	}
    45  	errors := e.Errors.Error()
    46  	return len(strings.Split(errors, ";"))
    47  }
    48  

View as plain text