github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/error/temporary_error.go (about)

     1  package error
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  type TemporaryError struct {
     8  	message string
     9  }
    10  
    11  func NewTemporaryError(msg string, args ...interface{}) *TemporaryError {
    12  	return &TemporaryError{message: fmt.Sprintf(msg, args...)}
    13  }
    14  
    15  func AsTemporaryError(err error, context string, args ...interface{}) *TemporaryError {
    16  	errCtx := fmt.Sprintf(context, args...)
    17  	msg := fmt.Sprintf("%s: %s", errCtx, err.Error())
    18  
    19  	return &TemporaryError{message: msg}
    20  }
    21  
    22  func (te TemporaryError) Error() string        { return te.message }
    23  func (TemporaryError) Temporary() bool         { return true }
    24  func (TemporaryError) Reason() ErrReason       { return ErrKEBInternal }
    25  func (TemporaryError) Component() ErrComponent { return ErrKEB }
    26  
    27  func IsTemporaryError(err error) bool {
    28  	cause := UnwrapAll(err)
    29  
    30  	nfe, ok := cause.(interface {
    31  		Temporary() bool
    32  	})
    33  
    34  	return ok && nfe.Temporary()
    35  }
    36  
    37  // can be used for temporary error
    38  // but still storing the original error in case returned to Execute
    39  type WrapTemporaryError struct {
    40  	err error
    41  }
    42  
    43  func WrapAsTemporaryError(err error, msg string, args ...interface{}) *WrapTemporaryError {
    44  	return &WrapTemporaryError{err: fmt.Errorf(fmt.Sprintf(msg, args...)+" :%w", err)}
    45  }
    46  
    47  func WrapNewTemporaryError(err error) *WrapTemporaryError {
    48  	return &WrapTemporaryError{err: err}
    49  }
    50  
    51  func (te WrapTemporaryError) Error() string { return te.err.Error() }
    52  func (WrapTemporaryError) Temporary() bool  { return true }
    53  
    54  func (wte WrapTemporaryError) Reason() ErrReason {
    55  	return ReasonForError(wte.err).Reason()
    56  }
    57  
    58  func (wte WrapTemporaryError) Component() ErrComponent {
    59  	return ReasonForError(wte.err).Component()
    60  }