...

Source file src/github.com/launchdarkly/go-server-sdk-evaluation/v2/errors.go

Documentation: github.com/launchdarkly/go-server-sdk-evaluation/v2

     1  package evaluation
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/launchdarkly/go-sdk-common/v3/ldreason"
     7  )
     8  
     9  // These error types are used only internally to distinguish between reasons an evaluation might fail.
    10  // They are surfaced only in terms of the EvaluationReason/ErrorKind types.
    11  
    12  // When possible, we define these types as renames of a simple type like string or int, rather than as
    13  // a struct. This is a minor optimization to take advantage of the fact that a simple type that implements
    14  // an interface does not need to be allocated on the heap.
    15  
    16  // EvalError is an internal interface for an error that should cause evaluation to fail.
    17  type evalError interface {
    18  	error
    19  	errorKind() ldreason.EvalErrorKind
    20  }
    21  
    22  // ErrorKindForError returns the appropriate ldreason.EvalErrorKind value for an error.
    23  func errorKindForError(err error) ldreason.EvalErrorKind {
    24  	if e, ok := err.(evalError); ok {
    25  		return e.errorKind()
    26  	}
    27  	return ldreason.EvalErrorException
    28  }
    29  
    30  // BadVariationError means a variation index was out of range. The integer value is the index.
    31  type badVariationError int
    32  
    33  func (e badVariationError) Error() string {
    34  	return fmt.Sprintf("rule, fallthrough, or target referenced a nonexistent variation index %d", int(e))
    35  }
    36  
    37  func (e badVariationError) errorKind() ldreason.EvalErrorKind {
    38  	return ldreason.EvalErrorMalformedFlag
    39  }
    40  
    41  // EmptyAttrRefError means an attribute reference in a clause was undefined
    42  type emptyAttrRefError struct{}
    43  
    44  func (e emptyAttrRefError) Error() string {
    45  	return "rule clause did not specify an attribute"
    46  }
    47  
    48  func (e emptyAttrRefError) errorKind() ldreason.EvalErrorKind {
    49  	return ldreason.EvalErrorMalformedFlag
    50  }
    51  
    52  // BadAttrRefError means an attribute reference in a clause was syntactically invalid. The string value is the
    53  // attribute reference.
    54  type badAttrRefError string
    55  
    56  func (e badAttrRefError) Error() string {
    57  	return fmt.Sprintf("invalid attribute reference %q", string(e))
    58  }
    59  
    60  func (e badAttrRefError) errorKind() ldreason.EvalErrorKind {
    61  	return ldreason.EvalErrorMalformedFlag
    62  }
    63  
    64  // EmptyRolloutError means a rollout or experiment had no variations.
    65  type emptyRolloutError struct{}
    66  
    67  func (e emptyRolloutError) Error() string {
    68  	return "rollout or experiment with no variations"
    69  }
    70  
    71  func (e emptyRolloutError) errorKind() ldreason.EvalErrorKind {
    72  	return ldreason.EvalErrorMalformedFlag
    73  }
    74  
    75  // CircularPrereqReferenceError means there was a cycle in prerequisites. The string value is the key of the
    76  // prerequisite.
    77  type circularPrereqReferenceError string
    78  
    79  func (e circularPrereqReferenceError) Error() string {
    80  	return fmt.Sprintf("prerequisite relationship to %q caused a circular reference;"+
    81  		" this is probably a temporary condition due to an incomplete update", string(e))
    82  }
    83  
    84  func (e circularPrereqReferenceError) errorKind() ldreason.EvalErrorKind {
    85  	return ldreason.EvalErrorMalformedFlag
    86  }
    87  
    88  // CircularSegmentReferenceError means there was a cycle in segment rules. The string value is the key of the
    89  // segment where we detected the cycle.
    90  type circularSegmentReferenceError string
    91  
    92  func (e circularSegmentReferenceError) Error() string {
    93  	return fmt.Sprintf("segment rule referencing segment %q caused a circular reference;"+
    94  		" this is probably a temporary condition due to an incomplete update", string(e))
    95  }
    96  
    97  // MalformedSegmentError means invalid properties were found while trying to match a segment.
    98  type malformedSegmentError struct {
    99  	SegmentKey string
   100  	Err        error
   101  }
   102  
   103  func (e malformedSegmentError) Error() string {
   104  	return fmt.Sprintf("segment %q had an invalid configuration: %s", e.SegmentKey, e.Err)
   105  }
   106  
   107  func (e malformedSegmentError) errorKind() ldreason.EvalErrorKind {
   108  	return ldreason.EvalErrorMalformedFlag
   109  	// Technically it's not a malformed *flag*, but we don't have a better error code for this.
   110  }
   111  

View as plain text