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

Empty string constraint fixes #193

Merged
merged 12 commits into from
Nov 28, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Move errors to own package
  • Loading branch information
markphelps committed Nov 26, 2019
commit 569e90bba1a1d591cc9f84863cecc0ade07bb196
55 changes: 55 additions & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package errors

import (
"errors"
"fmt"
)

// New creates a new error with errors.New
func New(s string) error {
return errors.New(s)
}

// ErrNotFound represents a not found error
type ErrNotFound string

// ErrNotFoundf creates an ErrNotFound using a custom format
func ErrNotFoundf(format string, args ...interface{}) error {
return ErrNotFound(fmt.Sprintf(format, args...))
}

func (e ErrNotFound) Error() string {
return fmt.Sprintf("%s not found", string(e))
}

// ErrInvalid represents an invalid error
type ErrInvalid string

// ErrInvalidf creates an ErrInvalid using a custom format
func ErrInvalidf(format string, args ...interface{}) error {
return ErrInvalid(fmt.Sprintf(format, args...))
}

func (e ErrInvalid) Error() string {
return string(e)
}

// ErrValidation is a validation error for a specific field and reason
type ErrValidation struct {
field string
reason string
}

func (e ErrValidation) Error() string {
return fmt.Sprintf("invalid field %s: %s", e.field, e.reason)
}

// InvalidFieldError creates an ErrInvalidField for a specific field and reason
func InvalidFieldError(field, reason string) error {
return ErrValidation{field, reason}
}

// EmptyFieldError creates an ErrInvalidField for an empty field
func EmptyFieldError(field string) error {
return InvalidFieldError(field, "must not be empty")
}
3 changes: 3 additions & 0 deletions rpc/validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package flipt


23 changes: 0 additions & 23 deletions server/errors.go

This file was deleted.

5 changes: 3 additions & 2 deletions server/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ import (
"time"

"github.com/gofrs/uuid"
"github.com/markphelps/flipt/errors"
flipt "github.com/markphelps/flipt/rpc"
)

// Evaluate evaluates a request for a given flag and entity
func (s *Server) Evaluate(ctx context.Context, req *flipt.EvaluationRequest) (*flipt.EvaluationResponse, error) {
if req.FlagKey == "" {
return nil, emptyFieldError("flagKey")
return nil, errors.EmptyFieldError("flagKey")
}

if req.EntityId == "" {
return nil, emptyFieldError("entityId")
return nil, errors.EmptyFieldError("entityId")
}

startTime := time.Now()
Expand Down
7 changes: 4 additions & 3 deletions server/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package server

import (
"context"
"errors"
"testing"

"github.com/markphelps/flipt/errors"

flipt "github.com/markphelps/flipt/rpc"
"github.com/markphelps/flipt/storage"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -59,7 +60,7 @@ func TestEvaluate(t *testing.T) {
EntityId: r.EntityId,
}, nil
},
wantErr: emptyFieldError("flagKey"),
wantErr: errors.EmptyFieldError("flagKey"),
},
{
name: "emptyEntityId",
Expand All @@ -74,7 +75,7 @@ func TestEvaluate(t *testing.T) {
EntityId: "",
}, nil
},
wantErr: emptyFieldError("entityId"),
wantErr: errors.EmptyFieldError("entityId"),
},
{
name: "error test",
Expand Down
28 changes: 15 additions & 13 deletions server/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ package server
import (
"context"

"github.com/markphelps/flipt/errors"

"github.com/golang/protobuf/ptypes/empty"
flipt "github.com/markphelps/flipt/rpc"
)

// GetFlag gets a flag
func (s *Server) GetFlag(ctx context.Context, req *flipt.GetFlagRequest) (*flipt.Flag, error) {
if req.Key == "" {
return nil, emptyFieldError("key")
return nil, errors.EmptyFieldError("key")
}

return s.FlagStore.GetFlag(ctx, req)
Expand All @@ -35,11 +37,11 @@ func (s *Server) ListFlags(ctx context.Context, req *flipt.ListFlagRequest) (*fl
// CreateFlag creates a flag
func (s *Server) CreateFlag(ctx context.Context, req *flipt.CreateFlagRequest) (*flipt.Flag, error) {
if req.Key == "" {
return nil, emptyFieldError("key")
return nil, errors.EmptyFieldError("key")
}

if req.Name == "" {
return nil, emptyFieldError("name")
return nil, errors.EmptyFieldError("name")
}

return s.FlagStore.CreateFlag(ctx, req)
Expand All @@ -48,11 +50,11 @@ func (s *Server) CreateFlag(ctx context.Context, req *flipt.CreateFlagRequest) (
// UpdateFlag updates an existing flag
func (s *Server) UpdateFlag(ctx context.Context, req *flipt.UpdateFlagRequest) (*flipt.Flag, error) {
if req.Key == "" {
return nil, emptyFieldError("key")
return nil, errors.EmptyFieldError("key")
}

if req.Name == "" {
return nil, emptyFieldError("name")
return nil, errors.EmptyFieldError("name")
}

return s.FlagStore.UpdateFlag(ctx, req)
Expand All @@ -61,7 +63,7 @@ func (s *Server) UpdateFlag(ctx context.Context, req *flipt.UpdateFlagRequest) (
// DeleteFlag deletes a flag
func (s *Server) DeleteFlag(ctx context.Context, req *flipt.DeleteFlagRequest) (*empty.Empty, error) {
if req.Key == "" {
return nil, emptyFieldError("key")
return nil, errors.EmptyFieldError("key")
}

if err := s.FlagStore.DeleteFlag(ctx, req); err != nil {
Expand All @@ -74,11 +76,11 @@ func (s *Server) DeleteFlag(ctx context.Context, req *flipt.DeleteFlagRequest) (
// CreateVariant creates a variant
func (s *Server) CreateVariant(ctx context.Context, req *flipt.CreateVariantRequest) (*flipt.Variant, error) {
if req.FlagKey == "" {
return nil, emptyFieldError("flagKey")
return nil, errors.EmptyFieldError("flagKey")
}

if req.Key == "" {
return nil, emptyFieldError("key")
return nil, errors.EmptyFieldError("key")
}

return s.FlagStore.CreateVariant(ctx, req)
Expand All @@ -87,15 +89,15 @@ func (s *Server) CreateVariant(ctx context.Context, req *flipt.CreateVariantRequ
// UpdateVariant updates an existing variant
func (s *Server) UpdateVariant(ctx context.Context, req *flipt.UpdateVariantRequest) (*flipt.Variant, error) {
if req.Id == "" {
return nil, emptyFieldError("id")
return nil, errors.EmptyFieldError("id")
}

if req.FlagKey == "" {
return nil, emptyFieldError("flagKey")
return nil, errors.EmptyFieldError("flagKey")
}

if req.Key == "" {
return nil, emptyFieldError("key")
return nil, errors.EmptyFieldError("key")
}

return s.FlagStore.UpdateVariant(ctx, req)
Expand All @@ -104,11 +106,11 @@ func (s *Server) UpdateVariant(ctx context.Context, req *flipt.UpdateVariantRequ
// DeleteVariant deletes a variant
func (s *Server) DeleteVariant(ctx context.Context, req *flipt.DeleteVariantRequest) (*empty.Empty, error) {
if req.Id == "" {
return nil, emptyFieldError("id")
return nil, errors.EmptyFieldError("id")
}

if req.FlagKey == "" {
return nil, emptyFieldError("flagKey")
return nil, errors.EmptyFieldError("flagKey")
}

if err := s.FlagStore.DeleteVariant(ctx, req); err != nil {
Expand Down
29 changes: 15 additions & 14 deletions server/flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package server

import (
"context"
"errors"
"testing"

"github.com/markphelps/flipt/errors"

"github.com/golang/protobuf/ptypes/empty"
flipt "github.com/markphelps/flipt/rpc"
"github.com/markphelps/flipt/storage"
Expand Down Expand Up @@ -91,7 +92,7 @@ func TestGetFlag(t *testing.T) {
}, nil
},
flag: nil,
wantErr: emptyFieldError("key"),
wantErr: errors.EmptyFieldError("key"),
},
}

Expand Down Expand Up @@ -233,7 +234,7 @@ func TestCreateFlag(t *testing.T) {
Enabled: r.Enabled,
}, nil
},
wantErr: emptyFieldError("key"),
wantErr: errors.EmptyFieldError("key"),
},
{
name: "emptyName",
Expand All @@ -257,7 +258,7 @@ func TestCreateFlag(t *testing.T) {
Enabled: r.Enabled,
}, nil
},
wantErr: emptyFieldError("name"),
wantErr: errors.EmptyFieldError("name"),
},
}

Expand Down Expand Up @@ -342,7 +343,7 @@ func TestUpdateFlag(t *testing.T) {
Enabled: r.Enabled,
}, nil
},
wantErr: emptyFieldError("key"),
wantErr: errors.EmptyFieldError("key"),
},
{
name: "emptyName",
Expand All @@ -366,7 +367,7 @@ func TestUpdateFlag(t *testing.T) {
Enabled: r.Enabled,
}, nil
},
wantErr: emptyFieldError("name"),
wantErr: errors.EmptyFieldError("name"),
},
}

Expand Down Expand Up @@ -420,7 +421,7 @@ func TestDeleteFlag(t *testing.T) {

return nil
},
wantErr: emptyFieldError("key"),
wantErr: errors.EmptyFieldError("key"),
},
{
name: "error",
Expand Down Expand Up @@ -516,7 +517,7 @@ func TestCreateVariant(t *testing.T) {
Description: r.Description,
}, nil
},
wantErr: emptyFieldError("flagKey"),
wantErr: errors.EmptyFieldError("flagKey"),
},
{
name: "emptyKey",
Expand All @@ -540,7 +541,7 @@ func TestCreateVariant(t *testing.T) {
Description: r.Description,
}, nil
},
wantErr: emptyFieldError("key"),
wantErr: errors.EmptyFieldError("key"),
},
}

Expand Down Expand Up @@ -620,7 +621,7 @@ func TestUpdateVariant(t *testing.T) {
Description: r.Description,
}, nil
},
wantErr: emptyFieldError("id"),
wantErr: errors.EmptyFieldError("id"),
},
{
name: "emptyFlagKey",
Expand All @@ -641,7 +642,7 @@ func TestUpdateVariant(t *testing.T) {
Description: r.Description,
}, nil
},
wantErr: emptyFieldError("flagKey"),
wantErr: errors.EmptyFieldError("flagKey"),
},
{
name: "emptyKey",
Expand All @@ -662,7 +663,7 @@ func TestUpdateVariant(t *testing.T) {
Description: r.Description,
}, nil
},
wantErr: emptyFieldError("key"),
wantErr: errors.EmptyFieldError("key"),
},
}

Expand Down Expand Up @@ -718,7 +719,7 @@ func TestDeleteVariant(t *testing.T) {

return nil
},
wantErr: emptyFieldError("id"),
wantErr: errors.EmptyFieldError("id"),
},
{
name: "emptyFlagKey",
Expand All @@ -730,7 +731,7 @@ func TestDeleteVariant(t *testing.T) {

return nil
},
wantErr: emptyFieldError("flagKey"),
wantErr: errors.EmptyFieldError("flagKey"),
},
{
name: "error",
Expand Down
Loading