@@ -67,6 +67,7 @@ func parseFractionalEvaluationData(values, data any, logger *logger.Logger) (str
6767 }
6868
6969 properties , _ := getFlagdProperties (dataMap )
70+ flagKey := properties .FlagKey
7071
7172 bucketBy , ok := valuesArray [0 ].(string )
7273 if ok {
@@ -79,21 +80,21 @@ func parseFractionalEvaluationData(values, data any, logger *logger.Logger) (str
7980
8081 targetingKey , ok := dataMap [targetingKeyKey ].(string )
8182 if ! ok {
82- return "" , nil , errors . New ( " bucketing value not supplied and no targetingKey in context" )
83+ return "" , nil , fmt . Errorf ( "flag %q: bucketing value not supplied and no targetingKey in context", flagKey )
8384 }
8485
8586 bucketBy = fmt .Sprintf ("%s%s" , properties .FlagKey , targetingKey )
8687 }
8788
88- feDistributions , err := parseFractionalEvaluationDistributions (valuesArray , data , logger )
89+ feDistributions , err := parseFractionalEvaluationDistributions (valuesArray , data , logger , flagKey )
8990 if err != nil {
9091 return "" , nil , err
9192 }
9293
9394 return bucketBy , feDistributions , nil
9495}
9596
96- func parseFractionalEvaluationDistributions (values []any , data any , logger * logger.Logger ) (* fractionalEvaluationDistribution , error ) {
97+ func parseFractionalEvaluationDistributions (values []any , data any , logger * logger.Logger , flagKey string ) (* fractionalEvaluationDistribution , error ) {
9798 feDistributions := & fractionalEvaluationDistribution {
9899 totalWeight : 0 ,
99100 weightedVariants : make ([]fractionalEvaluationVariant , len (values )),
@@ -107,12 +108,12 @@ func parseFractionalEvaluationDistributions(values []any, data any, logger *logg
107108 for i := 0 ; i < len (values ); i ++ {
108109 distributionArray , ok := values [i ].([]any )
109110 if ! ok {
110- return nil , errors . New ( " distribution elements aren't of type []any. " +
111- "please check your rule in flag definition" )
111+ return nil , fmt . Errorf ( "flag %q: distribution elements aren't of type []any. "+
112+ "please check your rule in flag definition" , flagKey )
112113 }
113114
114115 if len (distributionArray ) == 0 {
115- return nil , errors . New ( " distribution element needs at least one element" )
116+ return nil , fmt . Errorf ( "flag %q: distribution element needs at least one element", flagKey )
116117 }
117118
118119 // JSONLogic pre-evaluates all arguments before they reach fractional.
@@ -128,15 +129,15 @@ func parseFractionalEvaluationDistributions(values []any, data any, logger *logg
128129 case nil :
129130 variant = nil
130131 default :
131- return nil , errors . New ( " first element of distribution element must be a string, bool, number, or nil" )
132+ return nil , fmt . Errorf ( "flag %q: first element of distribution element must be a string, bool, number, or nil", flagKey )
132133 }
133134
134135 weight := int64 (1 )
135136 if len (distributionArray ) >= 2 {
136137 // parse as float64 first since that's what JSON gives us
137138 distributionWeight , ok := distributionArray [1 ].(float64 )
138139 if ! ok && distributionArray [1 ] != nil {
139- return nil , errors . New ( " weight must be a number" )
140+ return nil , fmt . Errorf ( "flag %q: weight must be a number", flagKey )
140141 }
141142 if ok {
142143 weight = int64 (distributionWeight )
@@ -147,13 +148,13 @@ func parseFractionalEvaluationDistributions(values []any, data any, logger *logg
147148 if len (distributionArray ) >= 2 {
148149 distributionWeight , ok := distributionArray [1 ].(float64 )
149150 if ok && distributionWeight != float64 (int64 (distributionWeight )) {
150- return nil , errors . New ( " weights must be integers" )
151+ return nil , fmt . Errorf ( "flag %q: weights must be integers", flagKey )
151152 }
152153 }
153154
154155 // validate individual weight doesn't exceed int32
155156 if weight > math .MaxInt32 || weight < 0 {
156- return nil , fmt .Errorf ("weight %d exceeds maximum allowed value %d" , weight , math .MaxInt32 )
157+ return nil , fmt .Errorf ("flag %q: weight %d exceeds maximum allowed value %d" , flagKey , weight , math .MaxInt32 )
157158 }
158159
159160 totalWeightInt64 += weight
@@ -165,7 +166,7 @@ func parseFractionalEvaluationDistributions(values []any, data any, logger *logg
165166
166167 // validate total weight doesn't exceed MaxInt32
167168 if totalWeightInt64 > int64 (maxWeightSum ) {
168- return nil , fmt .Errorf ("sum of all weights (%d) exceeds maximum allowed value (%d)" , totalWeightInt64 , maxWeightSum )
169+ return nil , fmt .Errorf ("flag %q: sum of all weights (%d) exceeds maximum allowed value (%d)" , flagKey , totalWeightInt64 , maxWeightSum )
169170 }
170171
171172 feDistributions .totalWeight = int32 (totalWeightInt64 )
0 commit comments