Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix connect error code handling for disabled flags #670

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions core/pkg/service/flag-evaluation/flag_evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,12 +318,10 @@ func formatContextKeys(context *structpb.Struct) []string {

func errFormat(err error) error {
switch err.Error() {
case model.FlagNotFoundErrorCode:
case model.FlagNotFoundErrorCode, model.FlagDisabledErrorCode:
return connect.NewError(connect.CodeNotFound, fmt.Errorf("%s, %s", ErrorPrefix, err.Error()))
case model.TypeMismatchErrorCode:
return connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("%s, %s", ErrorPrefix, err.Error()))
case model.FlagDisabledErrorCode:
return connect.NewError(connect.CodeUnavailable, fmt.Errorf("%s, %s", ErrorPrefix, err.Error()))
case model.ParseErrorCode:
return connect.NewError(connect.CodeDataLoss, fmt.Errorf("%s, %s", ErrorPrefix, err.Error()))
case model.GeneralErrorCode:
Expand Down
47 changes: 47 additions & 0 deletions core/pkg/service/flag-evaluation/flag_evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -903,3 +903,50 @@ func getMetricReader() (*telemetry.MetricsRecorder, metric.Reader) {
rs := resource.NewWithAttributes("testSchema")
return telemetry.NewOTelRecorder(exp, rs, "testSvc"), exp
}

// TestFlag_Evaluation_ErrorCodes test validate error mapping from known errors to connect.Code and avoid accidental
// changes. This is essential as SDK implementations rely on connect. Code to differentiate GRPC errors vs Flag errors.
// For any change in error codes, we must change respective SDK.
func TestFlag_Evaluation_ErrorCodes(t *testing.T) {
tests := []struct {
err error
code connect.Code
}{
{
err: errors.New(model.FlagNotFoundErrorCode),
code: connect.CodeNotFound,
},
{
err: errors.New(model.TypeMismatchErrorCode),
code: connect.CodeInvalidArgument,
},
{
err: errors.New(model.ParseErrorCode),
code: connect.CodeDataLoss,
},
{
err: errors.New(model.FlagDisabledErrorCode),
code: connect.CodeNotFound,
},
{
err: errors.New(model.GeneralErrorCode),
code: connect.CodeUnknown,
},
}

for _, test := range tests {
err := errFormat(test.err)

var connectErr *connect.Error
ok := errors.As(err, &connectErr)

if !ok {
t.Error("formatted error is not of type connect.Error")
}

if connectErr.Code() != test.code {
t.Errorf("expected code %s, but got code %s for model error %s", test.code, connectErr.Code(),
test.err.Error())
}
}
}