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 parse error for bool/number to not return 500 #1047

Merged
merged 4 commits into from
Sep 30, 2022
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
8 changes: 4 additions & 4 deletions server/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package server
import (
"context"
"errors"
"fmt"
"hash/crc32"
"sort"
"strconv"
Expand Down Expand Up @@ -294,12 +293,13 @@ func matchesNumber(c storage.EvaluationConstraint, v string) (bool, error) {

n, err := strconv.ParseFloat(v, 64)
if err != nil {
return false, fmt.Errorf("parsing number from %q", v)
return false, errs.ErrInvalidf("parsing number from %q", v)
}

// TODO: we should consider parsing this at creation time since it doesn't change and it doesnt make sense to allow invalid constraint values
value, err := strconv.ParseFloat(c.Value, 64)
if err != nil {
return false, fmt.Errorf("parsing number from %q", c.Value)
return false, errs.ErrInvalidf("parsing number from %q", c.Value)
}

switch c.Operator {
Expand Down Expand Up @@ -335,7 +335,7 @@ func matchesBool(c storage.EvaluationConstraint, v string) (bool, error) {

value, err := strconv.ParseBool(v)
if err != nil {
return false, fmt.Errorf("parsing boolean from %q", v)
return false, errs.ErrInvalidf("parsing boolean from %q", v)
}

switch c.Operator {
Expand Down
12 changes: 8 additions & 4 deletions server/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"go.flipt.io/flipt/errors"
errs "go.flipt.io/flipt/errors"
flipt "go.flipt.io/flipt/rpc/flipt"
"go.flipt.io/flipt/storage"
"go.uber.org/zap/zaptest"
Expand Down Expand Up @@ -83,7 +83,7 @@ func TestBatchEvaluate_FlagNotFoundExcluded(t *testing.T) {
}
store.On("GetFlag", mock.Anything, "foo").Return(enabledFlag, nil)
store.On("GetFlag", mock.Anything, "bar").Return(disabled, nil)
store.On("GetFlag", mock.Anything, "NotFoundFlag").Return(&flipt.Flag{}, errors.ErrNotFoundf("flag %q", "NotFoundFlag"))
store.On("GetFlag", mock.Anything, "NotFoundFlag").Return(&flipt.Flag{}, errs.ErrNotFoundf("flag %q", "NotFoundFlag"))

store.On("GetEvaluationRules", mock.Anything, "foo").Return([]*storage.EvaluationRule{}, nil)

Expand Down Expand Up @@ -131,7 +131,7 @@ func TestBatchEvaluate_FlagNotFound(t *testing.T) {
}
store.On("GetFlag", mock.Anything, "foo").Return(enabledFlag, nil)
store.On("GetFlag", mock.Anything, "bar").Return(disabled, nil)
store.On("GetFlag", mock.Anything, "NotFoundFlag").Return(&flipt.Flag{}, errors.ErrNotFoundf("flag %q", "NotFoundFlag"))
store.On("GetFlag", mock.Anything, "NotFoundFlag").Return(&flipt.Flag{}, errs.ErrNotFoundf("flag %q", "NotFoundFlag"))

store.On("GetEvaluationRules", mock.Anything, "foo").Return([]*storage.EvaluationRule{}, nil)

Expand Down Expand Up @@ -171,7 +171,7 @@ func TestEvaluate_FlagNotFound(t *testing.T) {
}
)

store.On("GetFlag", mock.Anything, "foo").Return(&flipt.Flag{}, errors.ErrNotFoundf("flag %q", "foo"))
store.On("GetFlag", mock.Anything, "foo").Return(&flipt.Flag{}, errs.ErrNotFoundf("flag %q", "foo"))

resp, err := s.Evaluate(context.TODO(), &flipt.EvaluationRequest{
EntityId: "1",
Expand Down Expand Up @@ -1952,6 +1952,8 @@ func Test_matchesNumber(t *testing.T) {

if wantErr {
require.Error(t, err)
var ierr errs.ErrInvalid
require.ErrorAs(t, err, &ierr)
return
}

Expand Down Expand Up @@ -2074,6 +2076,8 @@ func Test_matchesBool(t *testing.T) {

if wantErr {
require.Error(t, err)
var ierr errs.ErrInvalid
require.ErrorAs(t, err, &ierr)
return
}

Expand Down
3 changes: 2 additions & 1 deletion storage/sql/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,13 @@ func TestParse(t *testing.T) {
driver = tt.driver
url = tt.dsn
wantErr = tt.wantErr
opts = tt.options
)

t.Run(tt.name, func(t *testing.T) {
d, u, err := parse(config.Config{
Database: cfg,
}, tt.options)
}, opts)

if wantErr {
require.Error(t, err)
Expand Down
10 changes: 9 additions & 1 deletion ui/src/components/Flags/DebugConsole.vue
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,15 @@ export default {
this.response = response.data;
})
.catch((error) => {
this.response = { error: error.response.data.error };
if (error.response) {
this.response = {
error: error.response.data.message,
};
} else {
this.response = {
error: error.message,
};
}
});
},
},
Expand Down