Skip to content

Commit

Permalink
fix: readable error messages (open-feature#1325)
Browse files Browse the repository at this point in the history
- Mapped the error codes to easier to read error messages
- Created unit test to check functionality

 open-feature#1100

---------

Signed-off-by: DBlanchard88 <davidblanchard88@gmail.com>
Signed-off-by: Dave <89858058+DBlanchard88@users.noreply.github.com>
Co-authored-by: Todd Baert <todd.baert@dynatrace.com>
  • Loading branch information
DBlanchard88 and toddbaert authored Jun 18, 2024
1 parent 298bd36 commit 7ff33ef
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
18 changes: 18 additions & 0 deletions core/pkg/model/error.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package model

import "fmt"

const (
FlagNotFoundErrorCode = "FLAG_NOT_FOUND"
ParseErrorCode = "PARSE_ERROR"
Expand All @@ -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)
}
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
46 changes: 46 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,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)
}
})
}
}

0 comments on commit 7ff33ef

Please sign in to comment.