-
-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Handle errors in flagState conversion (#2455)
* Handle errors in flagState conversion * Changing the error details * Add test for coverage * Add test for value
- Loading branch information
1 parent
b5144e3
commit a680901
Showing
5 changed files
with
228 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package gofferror | ||
|
||
import "fmt" | ||
|
||
type EmptyBucketingKeyError struct { | ||
Message string | ||
} | ||
|
||
// Implement the Error() method for the custom error type | ||
func (e *EmptyBucketingKeyError) Error() string { | ||
return fmt.Sprintf("Error: %s", e.Message) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package flagstate_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/thomaspoignant/go-feature-flag/ffcontext" | ||
"github.com/thomaspoignant/go-feature-flag/internal/flag" | ||
"github.com/thomaspoignant/go-feature-flag/internal/flagstate" | ||
"github.com/thomaspoignant/go-feature-flag/testutils/testconvert" | ||
) | ||
|
||
func TestFromFlagEvaluation(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
key string | ||
evaluationCtx ffcontext.Context | ||
flagCtx flag.Context | ||
currentFlag flag.Flag | ||
expected flagstate.FlagState | ||
}{ | ||
{ | ||
name: "Flag is disabled", | ||
key: "test-key", | ||
evaluationCtx: ffcontext.NewEvaluationContext("user-key"), | ||
flagCtx: flag.Context{}, | ||
currentFlag: &flag.InternalFlag{ | ||
Disable: testconvert.Bool(true), | ||
}, | ||
expected: flagstate.FlagState{ | ||
Timestamp: time.Now().Unix(), | ||
TrackEvents: true, | ||
Failed: false, | ||
ErrorCode: "", | ||
ErrorDetails: "", | ||
Reason: flag.ReasonDisabled, | ||
Metadata: nil, | ||
}, | ||
}, | ||
{ | ||
name: "Flag evaluation error", | ||
key: "test-key", | ||
evaluationCtx: ffcontext.NewEvaluationContext(""), | ||
flagCtx: flag.Context{}, | ||
currentFlag: &flag.InternalFlag{ | ||
Disable: testconvert.Bool(false), | ||
Variations: &map[string]*interface{}{ | ||
"var1": testconvert.Interface(1), | ||
"var2": testconvert.Interface(2), | ||
}, | ||
DefaultRule: &flag.Rule{ | ||
VariationResult: testconvert.String("var1"), | ||
}, | ||
}, | ||
expected: flagstate.FlagState{ | ||
Timestamp: time.Now().Unix(), | ||
TrackEvents: true, | ||
Failed: true, | ||
ErrorCode: flag.ErrorCodeTargetingKeyMissing, | ||
ErrorDetails: "Error: Empty targeting key", | ||
Reason: flag.ReasonError, | ||
Metadata: nil, | ||
}, | ||
}, | ||
{ | ||
name: "Flag evaluation valid type", | ||
key: "test-key", | ||
evaluationCtx: ffcontext.NewEvaluationContext("my-key"), | ||
flagCtx: flag.Context{}, | ||
currentFlag: &flag.InternalFlag{ | ||
Disable: testconvert.Bool(false), | ||
Variations: &map[string]*interface{}{ | ||
"var1": testconvert.Interface(1), | ||
"var2": testconvert.Interface(2), | ||
}, | ||
DefaultRule: &flag.Rule{ | ||
VariationResult: testconvert.String("var1"), | ||
}, | ||
}, | ||
expected: flagstate.FlagState{ | ||
Value: 1, | ||
VariationType: "var1", | ||
Timestamp: time.Now().Unix(), | ||
TrackEvents: true, | ||
Failed: false, | ||
Reason: flag.ReasonStatic, | ||
Metadata: nil, | ||
}, | ||
}, | ||
{ | ||
name: "Flag evaluation invalid type", | ||
key: "test-key", | ||
evaluationCtx: ffcontext.NewEvaluationContext("my-key"), | ||
flagCtx: flag.Context{}, | ||
currentFlag: &flag.InternalFlag{ | ||
Disable: testconvert.Bool(false), | ||
Variations: &map[string]*interface{}{ | ||
"var1": testconvert.Interface(map[bool]*interface{}{true: testconvert.Interface(1)}), | ||
"var2": testconvert.Interface(map[bool]*interface{}{true: testconvert.Interface(2)}), | ||
}, | ||
DefaultRule: &flag.Rule{ | ||
VariationResult: testconvert.String("var1"), | ||
}, | ||
}, | ||
expected: flagstate.FlagState{ | ||
VariationType: "SdkDefault", | ||
Timestamp: time.Now().Unix(), | ||
TrackEvents: true, | ||
Failed: true, | ||
Reason: flag.ReasonError, | ||
ErrorCode: flag.ErrorCodeTypeMismatch, | ||
Metadata: nil, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := flagstate.FromFlagEvaluation(tt.key, tt.evaluationCtx, tt.flagCtx, tt.currentFlag) | ||
assert.Equal(t, tt.expected, got) | ||
}) | ||
} | ||
} |