Skip to content

Commit

Permalink
return type mismatch error when invalid default provided
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob ODwyer committed Oct 26, 2023
1 parent 9d4c71a commit 3b99a67
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
4 changes: 3 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,8 @@ func convertDefaultValueType(value interface{}) interface{} {
}
}

var ErrInvalidDefaultValue = errors.New("the default value for variable is not of type Boolean, Number, String, or JSON")

func variableTypeFromValue(key string, value interface{}, allowNil bool) (varType string, err error) {
switch value.(type) {
case float64:
Expand All @@ -723,7 +725,7 @@ func variableTypeFromValue(key string, value interface{}, allowNil bool) (varTyp
}
}

return "", fmt.Errorf("the default value for variable %s is not of type Boolean, Number, String, or JSON", key)
return "", fmt.Errorf("%w: %s", ErrInvalidDefaultValue, key)
}

// callAPI do the request.
Expand Down
18 changes: 12 additions & 6 deletions openfeature_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ func (p DevCycleProvider) Metadata() openfeature.Metadata {

// BooleanEvaluation returns a boolean flag
func (p DevCycleProvider) BooleanEvaluation(ctx context.Context, flag string, defaultValue bool, evalCtx openfeature.FlattenedContext) openfeature.BoolResolutionDetail {

user, err := createUserFromEvaluationContext(evalCtx)
if err != nil {
return openfeature.BoolResolutionDetail{
Expand All @@ -37,7 +36,7 @@ func (p DevCycleProvider) BooleanEvaluation(ctx context.Context, flag string, de
return openfeature.BoolResolutionDetail{
Value: defaultValue,
ProviderResolutionDetail: openfeature.ProviderResolutionDetail{
ResolutionError: openfeature.NewGeneralResolutionError(err.Error()), Reason: openfeature.ErrorReason,
ResolutionError: toOpenFeatureError(err), Reason: openfeature.ErrorReason,
},
}
}
Expand Down Expand Up @@ -66,7 +65,7 @@ func (p DevCycleProvider) StringEvaluation(ctx context.Context, flag string, def
return openfeature.StringResolutionDetail{
Value: defaultValue,
ProviderResolutionDetail: openfeature.ProviderResolutionDetail{
ResolutionError: openfeature.NewGeneralResolutionError(err.Error()), Reason: openfeature.ErrorReason,
ResolutionError: toOpenFeatureError(err), Reason: openfeature.ErrorReason,
},
}
}
Expand Down Expand Up @@ -95,7 +94,7 @@ func (p DevCycleProvider) FloatEvaluation(ctx context.Context, flag string, defa
return openfeature.FloatResolutionDetail{
Value: defaultValue,
ProviderResolutionDetail: openfeature.ProviderResolutionDetail{
ResolutionError: openfeature.NewGeneralResolutionError(err.Error()), Reason: openfeature.ErrorReason,
ResolutionError: toOpenFeatureError(err), Reason: openfeature.ErrorReason,
},
}
}
Expand Down Expand Up @@ -124,7 +123,7 @@ func (p DevCycleProvider) IntEvaluation(ctx context.Context, flag string, defaul
return openfeature.IntResolutionDetail{
Value: defaultValue,
ProviderResolutionDetail: openfeature.ProviderResolutionDetail{
ResolutionError: openfeature.NewGeneralResolutionError(err.Error()), Reason: openfeature.ErrorReason,
ResolutionError: toOpenFeatureError(err), Reason: openfeature.ErrorReason,
},
}
}
Expand Down Expand Up @@ -154,7 +153,7 @@ func (p DevCycleProvider) ObjectEvaluation(ctx context.Context, flag string, def
return openfeature.InterfaceResolutionDetail{
Value: defaultValue,
ProviderResolutionDetail: openfeature.ProviderResolutionDetail{
ResolutionError: openfeature.NewGeneralResolutionError(err.Error()), Reason: openfeature.ErrorReason,
ResolutionError: toOpenFeatureError(err), Reason: openfeature.ErrorReason,
},
}
}
Expand All @@ -171,6 +170,13 @@ func (p DevCycleProvider) Hooks() []openfeature.Hook {
return []openfeature.Hook{}
}

func toOpenFeatureError(err error) openfeature.ResolutionError {
if errors.Is(err, ErrInvalidDefaultValue) {
return openfeature.NewTypeMismatchResolutionError(err.Error())
}
return openfeature.NewGeneralResolutionError(err.Error())
}

func createUserFromEvaluationContext(evalCtx openfeature.FlattenedContext) (User, error) {
userId := ""
_, exists := evalCtx["userId"]
Expand Down
2 changes: 1 addition & 1 deletion openfeature_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ func Test_ObjectEvaluation_TargetMatchBadDefault(t *testing.T) {

require.Equal(t, defaultValue, resolutionDetail.Value)
require.Equal(t, openfeature.ErrorReason, resolutionDetail.ProviderResolutionDetail.Reason)
require.Equal(t, openfeature.NewGeneralResolutionError("the default value for variable test-json-variable is not of type Boolean, Number, String, or JSON"), resolutionDetail.ProviderResolutionDetail.ResolutionError)
require.Equal(t, openfeature.NewTypeMismatchResolutionError("the default value for variable is not of type Boolean, Number, String, or JSON: test-json-variable"), resolutionDetail.ProviderResolutionDetail.ResolutionError)
}

func Test_ObjectEvaluation_TargetMatchInvalidType(t *testing.T) {
Expand Down

0 comments on commit 3b99a67

Please sign in to comment.