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: readable error messages #1325

Merged
merged 10 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
16 changes: 16 additions & 0 deletions core/pkg/model/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,19 @@ const (
FlagDisabledErrorCode = "FLAG_DISABLED"
InvalidContextCode = "INVALID_CONTEXT"
)

var readableErrorMessage = map[string]string{
DBlanchard88 marked this conversation as resolved.
Show resolved Hide resolved
FlagNotFoundErrorCode: "Flag not found",
ParseErrorCode: "Error parsing input",
DBlanchard88 marked this conversation as resolved.
Show resolved Hide resolved
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 "An unknown error code"
toddbaert marked this conversation as resolved.
Show resolved Hide resolved
}
9 changes: 5 additions & 4 deletions flagd/pkg/service/flag-evaluation/flag_evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 49 additions & 0 deletions flagd/pkg/service/flag-evaluation/flag_evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -986,3 +986,52 @@ func TestFlag_Evaluation_ErrorCodes(t *testing.T) {
}
}
}

func Test_Readable_ErrorMessage(t *testing.T) {
type args struct {
code string
}
tests := []struct {
name string
args args
want string
}{
{
name: "Testing flag not found error",
args: args{code: model.FlagNotFoundErrorCode},
want: "Flag not found",
},
{
name: "Testing parse error",
args: args{code: model.ParseErrorCode},
want: "Error parsing input",
},
{
name: "Testing type mismatch error",
args: args{code: model.TypeMismatchErrorCode},
want: "Type mismatch error",
},
{
name: "Testing general error",
args: args{code: model.GeneralErrorCode},
want: "General error",
},
{
name: "Testing flag disabled error",
args: args{code: model.FlagDisabledErrorCode},
want: "Flag is disabled",
},
{
name: "Testing invalid context error",
args: args{code: model.InvalidContextCode},
want: "Invalid context provided",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := model.GetErrorMessage(tt.args.code); got != tt.want {
t.Errorf("GetErrorMessage() Wanted: %v , but got: %v as a readableErrorMessage", tt.want, got)
}
})
}
}
Loading