github.com/clusterize-io/tusk@v0.6.3-0.20211001020217-cfe8a8cd0d4a/runner/errors.go (about)

     1  package runner
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  // IsFailedCondition checks if an error was because of a failed condition.
     8  func IsFailedCondition(err error) bool {
     9  	we, ok := err.(conditionFailed)
    10  	return ok && we.WhenConditionFailed()
    11  }
    12  
    13  type conditionFailed interface {
    14  	WhenConditionFailed() bool
    15  }
    16  
    17  type conditionFailedError struct {
    18  	message string
    19  }
    20  
    21  func (e *conditionFailedError) Error() string {
    22  	return e.message
    23  }
    24  
    25  func (e *conditionFailedError) WhenConditionFailed() bool {
    26  	return true
    27  }
    28  
    29  // newCondFailError returns an error indicating a condition has failed.
    30  func newCondFailError(msg string) error {
    31  	return &conditionFailedError{msg}
    32  }
    33  
    34  // newCondFailErrorf returns an error indicating a condition has failed.
    35  func newCondFailErrorf(msg string, a ...interface{}) error {
    36  	formatted := fmt.Sprintf(msg, a...)
    37  	return &conditionFailedError{formatted}
    38  }
    39  
    40  // IsUnspecifiedClause checks if an error was because a clause is not defined.
    41  func IsUnspecifiedClause(err error) bool {
    42  	we, ok := err.(unspecifiedClause)
    43  	return ok && we.WhenUnspecifiedClause()
    44  }
    45  
    46  type unspecifiedClauseError struct {
    47  	message string
    48  }
    49  
    50  type unspecifiedClause interface {
    51  	WhenUnspecifiedClause() bool
    52  }
    53  
    54  func (e *unspecifiedClauseError) Error() string {
    55  	return e.message
    56  }
    57  
    58  func (e *unspecifiedClauseError) WhenUnspecifiedClause() bool {
    59  	return true
    60  }
    61  
    62  // newUnspecifiedError returns an error for unspecified clauses.
    63  func newUnspecifiedError(clauseName string) error {
    64  	formatted := fmt.Sprintf("clause %q is not defined", clauseName)
    65  	return &unspecifiedClauseError{formatted}
    66  }