diff --git a/core/pkg/model/error.go b/core/pkg/model/error.go index 6b5a3a7f7..9656f4f3c 100644 --- a/core/pkg/model/error.go +++ b/core/pkg/model/error.go @@ -1,5 +1,7 @@ package model +import "fmt" + const ( FlagNotFoundErrorCode = "FLAG_NOT_FOUND" ParseErrorCode = "PARSE_ERROR" @@ -8,3 +10,19 @@ const ( FlagDisabledErrorCode = "FLAG_DISABLED" InvalidContextCode = "INVALID_CONTEXT" ) + +var ReadableErrorMessage = map[string]string{ + FlagNotFoundErrorCode: "Flag not found", + ParseErrorCode: "Error parsing input or configuration", + TypeMismatchErrorCode: "Type mismatch error", + GeneralErrorCode: "General error", + FlagDisabledErrorCode: "Flag is disabled", + InvalidContextCode: "Invalid context provided", +} + +func GetErrorMessage(code string) string { + if msg, exists := ReadableErrorMessage[code]; exists { + return msg + } + return fmt.Sprintf("Unknown error code: %s", code) +} diff --git a/flagd/pkg/service/flag-evaluation/flag_evaluator.go b/flagd/pkg/service/flag-evaluation/flag_evaluator.go index 9e553b53a..8d1ed8def 100644 --- a/flagd/pkg/service/flag-evaluation/flag_evaluator.go +++ b/flagd/pkg/service/flag-evaluation/flag_evaluator.go @@ -327,15 +327,16 @@ func formatContextKeys(context *structpb.Struct) []string { } func errFormat(err error) error { + ReadableErrorMsg := model.GetErrorMessage(err.Error()) switch err.Error() { case model.FlagNotFoundErrorCode, model.FlagDisabledErrorCode: - return connect.NewError(connect.CodeNotFound, fmt.Errorf("%s, %s", ErrorPrefix, err.Error())) + return connect.NewError(connect.CodeNotFound, fmt.Errorf("%s", ReadableErrorMsg)) case model.TypeMismatchErrorCode: - return connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("%s, %s", ErrorPrefix, err.Error())) + return connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("%s", ReadableErrorMsg)) case model.ParseErrorCode: - return connect.NewError(connect.CodeDataLoss, fmt.Errorf("%s, %s", ErrorPrefix, err.Error())) + return connect.NewError(connect.CodeDataLoss, fmt.Errorf("%s", ReadableErrorMsg)) case model.GeneralErrorCode: - return connect.NewError(connect.CodeUnknown, fmt.Errorf("%s, %s", ErrorPrefix, err.Error())) + return connect.NewError(connect.CodeUnknown, fmt.Errorf("%s", ReadableErrorMsg)) } return err diff --git a/flagd/pkg/service/flag-evaluation/flag_evaluator_test.go b/flagd/pkg/service/flag-evaluation/flag_evaluator_test.go index a1d308e50..52c3d36f1 100644 --- a/flagd/pkg/service/flag-evaluation/flag_evaluator_test.go +++ b/flagd/pkg/service/flag-evaluation/flag_evaluator_test.go @@ -986,3 +986,49 @@ func TestFlag_Evaluation_ErrorCodes(t *testing.T) { } } } + +func Test_Readable_ErrorMessage(t *testing.T) { + tests := []struct { + name string + code string + want string + }{ + { + name: "Testing flag not found error", + code: model.FlagNotFoundErrorCode, + want: model.ReadableErrorMessage[model.FlagNotFoundErrorCode], + }, + { + name: "Testing parse error", + code: model.ParseErrorCode, + want: model.ReadableErrorMessage[model.ParseErrorCode], + }, + { + name: "Testing type mismatch error", + code: model.TypeMismatchErrorCode, + want: model.ReadableErrorMessage[model.TypeMismatchErrorCode], + }, + { + name: "Testing general error", + code: model.GeneralErrorCode, + want: model.ReadableErrorMessage[model.GeneralErrorCode], + }, + { + name: "Testing flag disabled error", + code: model.FlagDisabledErrorCode, + want: model.ReadableErrorMessage[model.FlagDisabledErrorCode], + }, + { + name: "Testing invalid context error", + code: model.InvalidContextCode, + want: model.ReadableErrorMessage[model.InvalidContextCode], + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := model.GetErrorMessage(tt.code); got != tt.want { + t.Errorf("GetErrorMessage() Wanted: %v , but got: %v as a ReadableErrorMessage", tt.want, got) + } + }) + } +}